Skip to content

Commit

Permalink
feat: generateOrg object to grab from access token and id token
Browse files Browse the repository at this point in the history
  • Loading branch information
peterphanouvong committed Oct 15, 2024
1 parent d450673 commit e724b47
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 45 deletions.
45 changes: 0 additions & 45 deletions src/session/getOrganization.js

This file was deleted.

34 changes: 34 additions & 0 deletions src/session/getOrganization.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import jwtDecode from 'jwt-decode';
import {KindeAccessToken, KindeIdToken} from '../../types';
import {config} from '../config/index';
import {generateOrganizationObject} from '../utils/generateOrganizationObject';
import {sessionManager} from './sessionManager';
/**
* @callback getOrganization
* @returns {Promise<import('../../types').KindeOrganization | null>}
*/

/**
*
* @param {import('next').NextApiRequest} [req]
* @param {import('next').NextApiResponse} [res]
* @returns {getOrganization}
*/
export const getOrganizationFactory = (req, res) => async () => {
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 generateOrganizationObject(idToken, accessToken);
} catch (error) {
if (config.isDebugMode) {
console.error(error);
}
return null;
}
};
31 changes: 31 additions & 0 deletions src/utils/generateOrganizationObject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {KindeAccessToken, KindeIdToken} from '../../types';

export const generateOrganizationObject = (
idToken: KindeIdToken,
accessToken: KindeAccessToken
) => {
return {
orgCode: accessToken.org_code,
orgName: accessToken.org_name,
properties: {
city:
idToken.organization_properties?.kp_org_city?.v ||
accessToken.organization_properties?.kp_org_city?.v,
industry:
idToken.organization_properties?.kp_org_industry?.v ||
accessToken.organization_properties?.kp_org_industry?.v,
postcode:
idToken.organization_properties?.kp_org_postcode?.v ||
accessToken.organization_properties?.kp_org_postcode?.v,
state_region:
idToken.organization_properties?.kp_org_state_region?.v ||
accessToken.organization_properties?.kp_org_state_region?.v,
street_address:
idToken.organization_properties?.kp_org_street_address?.v ||
accessToken.organization_properties?.kp_org_street_address?.v,
street_address_2:
idToken.organization_properties?.kp_org_street_address_2?.v ||
accessToken.organization_properties?.kp_org_street_address_2?.v
}
};
};

0 comments on commit e724b47

Please sign in to comment.