-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: generateOrg object to grab from access token and id token
- Loading branch information
1 parent
d450673
commit e724b47
Showing
3 changed files
with
65 additions
and
45 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
}; | ||
}; |