diff --git a/src/session/getUserOrganizations.js b/src/session/getUserOrganizations.js deleted file mode 100644 index 44aeada..0000000 --- a/src/session/getUserOrganizations.js +++ /dev/null @@ -1,39 +0,0 @@ -import {sessionManager} from './sessionManager'; -import {kindeClient} from './kindeServerClient'; -import {config} from '../config/index'; -/** - * @callback getUserOrganizations - * @returns {Promise} - */ - -/** - * - * @param {import('next').NextApiRequest} [req] - * @param {import('next').NextApiResponse} [res] - * @returns {getUserOrganizations} - */ -export const getUserOrganizationsFactory = (req, res) => async () => { - try { - const userOrgs = await kindeClient.getUserOrganizations( - sessionManager(req, res) - ); - const orgNames = await kindeClient.getClaimValue( - sessionManager(req, res), - 'organizations', - 'id_token' - ); - - return { - orgCodes: userOrgs.orgCodes, - orgs: orgNames?.map((org) => ({ - code: org?.id, - name: org?.name - })) - }; - } catch (error) { - if (config.isDebugMode) { - console.debug('getUser', error); - } - return null; - } -}; diff --git a/src/session/getUserOrganizations.ts b/src/session/getUserOrganizations.ts new file mode 100644 index 0000000..9109bb7 --- /dev/null +++ b/src/session/getUserOrganizations.ts @@ -0,0 +1,45 @@ +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 => { + try { + const userOrgs = await kindeClient.getUserOrganizations( + sessionManager(req, res) + ); + const orgNames = (await kindeClient.getClaimValue( + sessionManager(req, res), + 'organizations', + 'id_token' + )) as {id: string; name: string}[]; + + const hasuraOrgCodes = (await kindeClient.getClaimValue( + sessionManager(req, res), + 'x-hasura-org-codes', + 'id_token' + )) as string[]; + + const hasuraOrganizations = (await kindeClient.getClaimValue( + sessionManager(req, res), + '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('getUser', error); + } + return null; + } + }; diff --git a/src/session/kindeServerClient.ts b/src/session/kindeServerClient.ts index 39c4241..82bd56b 100644 --- a/src/session/kindeServerClient.ts +++ b/src/session/kindeServerClient.ts @@ -1,7 +1,10 @@ -import {createKindeServerClient} from '@kinde-oss/kinde-typescript-sdk'; +import { + createKindeServerClient, + GrantType +} from '@kinde-oss/kinde-typescript-sdk'; import {config} from '../config/index'; export const kindeClient = createKindeServerClient( - config.grantType, + GrantType.AUTHORIZATION_CODE, config.clientOptions );