Skip to content

Commit

Permalink
Merge pull request #200 from kinde-oss/peter/feat/custom-user-props
Browse files Browse the repository at this point in the history
feat: user object to contain custom properties
  • Loading branch information
peterphanouvong authored Aug 27, 2024
2 parents cb93131 + f37a21b commit b400744
Show file tree
Hide file tree
Showing 20 changed files with 692 additions and 176 deletions.
3 changes: 2 additions & 1 deletion clientConfig/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ const packageJson = require('./package.json');

import babel from '@rollup/plugin-babel';
import {terser} from 'rollup-plugin-terser';
import tsPlugin from '@rollup/plugin-typescript';

export default {
plugins: [babel({babelHelpers: 'bundled'}), terser()],
plugins: [babel({babelHelpers: 'bundled'}), terser(), tsPlugin()],
input: './src/index.js',
output: [
{
Expand Down
3 changes: 2 additions & 1 deletion componentConfig/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import babel from '@rollup/plugin-babel';
import {terser} from 'rollup-plugin-terser';
import tsPlugin from '@rollup/plugin-typescript';

export default {
plugins: [babel({babelHelpers: 'bundled'}), terser()],
plugins: [babel({babelHelpers: 'bundled'}), terser(), tsPlugin()],
input: 'src/components/index.js',
output: [
{
Expand Down
3 changes: 2 additions & 1 deletion middlewareConfig/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import babel from '@rollup/plugin-babel';
import {terser} from 'rollup-plugin-terser';
import tsPlugin from '@rollup/plugin-typescript';

export default {
plugins: [babel({babelHelpers: 'bundled'}), terser()],
plugins: [babel({babelHelpers: 'bundled'}), terser(), tsPlugin()],
input: 'src/middleware/index.js',
output: [
{
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"@babel/preset-react": "^7.17.12",
"@babel/preset-typescript": "^7.23.3",
"@rollup/plugin-babel": "^5.3.1",
"@rollup/plugin-typescript": "^11.1.5",
"@rollup/plugin-typescript": "^11.1.6",
"@testing-library/react": "^14.2.1",
"@types/cookie": "^0.5.3",
"@types/jest": "^29.5.12",
Expand Down
3 changes: 2 additions & 1 deletion serverConfig/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import babel from '@rollup/plugin-babel';
import {terser} from 'rollup-plugin-terser';
import tsPlugin from '@rollup/plugin-typescript';

export default {
plugins: [babel({babelHelpers: 'bundled'}), terser()],
plugins: [babel({babelHelpers: 'bundled'}), terser(), tsPlugin()],
input: 'src/server/index.js',
output: [
{
Expand Down
3 changes: 1 addition & 2 deletions src/config/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {GrantType} from '@kinde-oss/kinde-typescript-sdk';
import {version} from '../utils/version';
import {removeTrailingSlash} from '../utils/removeTrailingSlash';

Expand Down Expand Up @@ -93,5 +92,5 @@ export const config = {
frameworkVersion: version,
scope: KINDE_SCOPE
},
grantType: GrantType.AUTHORIZATION_CODE
grantType: 'AUTHORIZATION_CODE'
};
46 changes: 30 additions & 16 deletions src/handlers/setup.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import jwtDecode from 'jwt-decode';
import RouterClient from '../routerClients/RouterClient';
import {config} from '../config/index';
import {generateUserObject} from '../utils/generateUserObject';

/**
*
Expand Down Expand Up @@ -61,6 +62,17 @@ export const setup = async (routerClient) => {
'user_properties'
);

const phone_number = await routerClient.kindeClient.getClaimValue(
routerClient.sessionManager,
'phone_number',
'id_token'
);
const username = await routerClient.kindeClient.getClaimValue(
routerClient.sessionManager,
'preferred_username',
'id_token'
);

const orgNames = await routerClient.kindeClient.getClaimValue(
routerClient.sessionManager,
'organizations',
Expand All @@ -74,24 +86,12 @@ export const setup = async (routerClient) => {
idToken,
idTokenRaw: idTokenEncoded,
idTokenEncoded,
user: {
...user,
properties: {
city: userProperties?.kp_usr_city?.v,
industry: userProperties?.kp_usr_industry?.v,
job_title: userProperties?.kp_usr_job_title?.v,
middle_name: userProperties?.kp_usr_middle_name?.v,
postcode: userProperties?.kp_usr_postcode?.v,
salutation: userProperties?.kp_usr_salutation?.v,
state_region: userProperties?.kp_usr_state_region?.v,
street_address: userProperties?.kp_usr_street_address?.v,
street_address_2: userProperties?.kp_usr_street_address_2?.v
}
},
user: generateUserObject(user, userProperties, phone_number, username),
permissions: {
permissions,
orgCode: organization
},
needsRefresh: false,
organization: {
orgCode: organization,
orgName,
Expand All @@ -115,8 +115,22 @@ export const setup = async (routerClient) => {
});
} catch (error) {
if (config.isDebugMode) {
console.error(error);
console.debug(error);
}

if (error.code == 'ERR_JWT_EXPIRED') {
return routerClient.json(
{
needsRefresh: true
},
{status: 200}
);
}
return routerClient.json(
{
error
},
{status: 500}
);
}
return routerClient.json({error: 'Log in with Kinde'}, {status: 401});
};
2 changes: 1 addition & 1 deletion src/server/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export {default as getKindeServerSession} from '../session/index';
export {default as getKindeServerSession} from '../session/index.ts';
export {authMiddleware, withAuth} from '../authMiddleware/authMiddleware';
export {
LoginLink,
Expand Down
55 changes: 0 additions & 55 deletions src/session/getUser.js

This file was deleted.

28 changes: 28 additions & 0 deletions src/session/getUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import jwtDecode from 'jwt-decode';
import {NextApiRequest, NextApiResponse} from 'next';
import {KindeAccessToken, KindeIdToken, KindeUser} from '../../types';
import {config} from '../config/index';
import {generateUserObject} from '../utils/generateUserObject';
import {sessionManager} from './sessionManager';

export const getUserFactory =
(req: NextApiRequest, res: NextApiResponse) =>
async <T = Record<string, any>>(): Promise<KindeUser<T>> => {
try {
const idToken = jwtDecode(
(await sessionManager(req, res).getSessionItem('id_token')) as string
) as KindeIdToken;

const accessToken = jwtDecode(
(await sessionManager(req, res).getSessionItem(
'access_token'
)) as string
) as KindeAccessToken;
return generateUserObject(idToken, accessToken) as KindeUser<T>;
} catch (error) {
if (config.isDebugMode) {
console.debug('getUser', error);
}
return null;
}
};
36 changes: 0 additions & 36 deletions src/session/getUserOrganizations.js

This file was deleted.

44 changes: 44 additions & 0 deletions src/session/getUserOrganizations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {sessionManager} from './sessionManager';
import {kindeClient} from './kindeServerClient';
import {config} from '../config/index';
import {NextApiRequest, NextApiResponse} from 'next';
import {KindeOrganizations} from '../../types';

export const getUserOrganizationsFactory =
(req?: NextApiRequest, res?: NextApiResponse) =>
async (): Promise<KindeOrganizations | null> => {
try {
const session = sessionManager(req, res);
const userOrgs = await kindeClient.getUserOrganizations(session);
const orgNames = (await kindeClient.getClaimValue(
session,
'organizations',
'id_token'
)) as {id: string; name: string}[];

const hasuraOrgCodes = (await kindeClient.getClaimValue(
session,
'x-hasura-org-codes',
'id_token'
)) as string[];

const hasuraOrganizations = (await kindeClient.getClaimValue(
session,
'x-hasura-organizations',
'id_token'
)) as {id: string; name: string}[];

return {
orgCodes: [...userOrgs.orgCodes, ...hasuraOrgCodes],
orgs: [...orgNames, ...hasuraOrganizations].map((org) => ({
code: org?.id,
name: org?.name
}))
};
} catch (error) {
if (config.isDebugMode) {
console.debug('getUserOrganization error:', error);
}
return null;
}
};
10 changes: 3 additions & 7 deletions src/session/index.js → src/session/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,13 @@ import {sessionManager} from './sessionManager';
import {getRolesFactory} from './getRoles';
import {getClaimFactory} from './getClaim';
import {config} from '../config/index';
import {NextApiRequest, NextApiResponse} from 'next';

/**
*
* @param {import('next').NextApiRequest | Request} [req]
* @param {import('next').NextApiResponse | Response} [res]
* @returns
*/
export default function (req, res) {
export default function (req?: NextApiRequest, res?: NextApiResponse) {
return {
refreshTokens: async () => {
try {
// @ts-ignore
const response = await kindeClient.refreshTokens(
sessionManager(req, res)
);
Expand Down
9 changes: 0 additions & 9 deletions src/session/kindeServerClient.js

This file was deleted.

10 changes: 10 additions & 0 deletions src/session/kindeServerClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {
createKindeServerClient,
GrantType
} from '@kinde-oss/kinde-typescript-sdk';
import {config} from '../config/index';

export const kindeClient = createKindeServerClient(
GrantType.AUTHORIZATION_CODE,
config.clientOptions
);
Loading

0 comments on commit b400744

Please sign in to comment.