Skip to content

Commit

Permalink
chore: updating KindeUser type
Browse files Browse the repository at this point in the history
  • Loading branch information
peterphanouvong committed Aug 22, 2024
1 parent 1fd9858 commit abdf68c
Show file tree
Hide file tree
Showing 9 changed files with 235 additions and 119 deletions.
41 changes: 0 additions & 41 deletions src/session/getUser.js

This file was deleted.

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

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<T>;

return generateUserObject(idToken);

const user = await kindeClient.getUser(sessionManager(req, res));
const userProperties = await kindeClient.getClaimValue(
sessionManager(req, res),
'user_properties'
);
const phone_number = await kindeClient.getClaimValue(
sessionManager(req, res),
'phone_number',
'id_token'
);
const username = await kindeClient.getClaimValue(
sessionManager(req, res),
'preferred_username',
'id_token'
);
return generateUserObject(user, userProperties, phone_number, username);
} catch (error) {
if (config.isDebugMode) {
console.debug('getUser', 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
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import {createKindeServerClient} from '@kinde-oss/kinde-typescript-sdk';
import {config} from '../config/index';

/** @type {import('../../types').KindeClient} */
// @ts-ignore
export const kindeClient = createKindeServerClient(
config.grantType,
config.clientOptions
Expand Down
53 changes: 0 additions & 53 deletions src/utils/generateUserObject.js

This file was deleted.

111 changes: 111 additions & 0 deletions src/utils/generateUserObject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import {
KindeAccessToken,
KindeIdToken,
KindeUser,
KindeUserProperties
} from '../../types';

const getUserProperties = <T>(
properties: Record<string, {v: string}>
): KindeUserProperties<
T,
{
kp_usr_city: 'city';
kp_usr_industry: 'industry';
kp_usr_job_title: 'job_title';
kp_usr_middle_name: 'middle_name';
kp_usr_postcode: 'postcode';
kp_usr_salutation: 'salutation';
kp_usr_state_region: 'state_region';
kp_usr_street_address: 'street_address';
kp_usr_street_address_2: 'street_address_2';
}
> => {
const userProperties: KindeUserProperties<
T,
{
kp_usr_city: 'city';
kp_usr_industry: 'industry';
kp_usr_job_title: 'job_title';
kp_usr_middle_name: 'middle_name';
kp_usr_postcode: 'postcode';
kp_usr_salutation: 'salutation';
kp_usr_state_region: 'state_region';
kp_usr_street_address: 'street_address';
kp_usr_street_address_2: 'street_address_2';
}
> = Object.keys(properties).reduce((acc, key) => {
if (key.startsWith('kp_usr_')) {
acc[key.substring(7)] = properties[key]?.v;
}
acc[key] = properties[key]?.v;
return acc;
}, {});

return userProperties;
};

export const generateUserObject = (idToken: KindeIdToken): KindeUser => {
const user = {
id: idToken.sub,
email: idToken.email,
family_name: idToken.family_name,
given_name: idToken.given_name,
name: idToken.name,
picture: idToken.picture,
username: idToken.preferred_username,
phone_number: idToken.phone_number
};

const properties = idToken.properties;

if (properties) {
}

let res = user;
if (userProperties) {
const {
kp_usr_city: cityObj,
kp_usr_industry: industryObj,
kp_usr_job_title: jobTitleObj,
kp_usr_middle_name: middleNameObj,
kp_usr_postcode: postcodeObj,
kp_usr_salutation: salutationObj,
kp_usr_state_region: stateRegionObj,
kp_usr_street_address: streetAddressObj,
kp_usr_street_address_2: streetAddress2Obj,
...rest
} = userProperties;

const sanitizedRest = Object.keys(rest).reduce((acc, key) => {
acc[key] = rest[key]?.v;
return acc;
}, {});

res = {
...res,
properties: {
city: cityObj?.v,
industry: industryObj?.v,
job_title: jobTitleObj?.v,
middle_name: middleNameObj?.v,
postcode: postcodeObj?.v,
salutation: salutationObj?.v,
state_region: stateRegionObj?.v,
street_address: streetAddressObj?.v,
street_address_2: streetAddress2Obj?.v,
...sanitizedRest
}
};
}

if (phone_number || username) {
res = {
...res,
...(phone_number && {phone_number}),
...(username && {username})
};
}

return res;
};
5 changes: 5 additions & 0 deletions src/utils/getUserProperties.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {KindeTokenUserProperties} from '../../types';

export const getUserProperties = (
rawProperties: KindeTokenUserProperties
) => {};
2 changes: 1 addition & 1 deletion src/utils/version.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit abdf68c

Please sign in to comment.