diff --git a/src/api-client.js b/src/api-client.js index 7934b6e4..99ecc57d 100644 --- a/src/api-client.js +++ b/src/api-client.js @@ -28,7 +28,7 @@ import {isTokenValid} from './utils/pageRouter/isTokenValid'; export const createKindeManagementAPIClient = async (req, res) => { let apiToken = null; - const store = sessionManager(req, res); + const store = await sessionManager(req, res); store.removeSessionItem('kinde_api_access_token'); const response = await fetch(`${config.issuerURL}/oauth2/token`, { diff --git a/src/handlers/auth.js b/src/handlers/auth.js index dcb8543b..a6a7b7e2 100644 --- a/src/handlers/auth.js +++ b/src/handlers/auth.js @@ -87,14 +87,18 @@ export default (request, endpoint, options) => { */ const appRouterHandler = async (req, res, options) => { const {params} = res; - let endpoint = params.kindeAuth; + let endpoint = (await params).kindeAuth; endpoint = Array.isArray(endpoint) ? endpoint[0] : endpoint; const route = getRoute(endpoint); - return route - ? // @ts-ignore - await route(new AppRouterClient(req, res, options)) - : new Response('This page could not be found.', {status: 404}); + if (route) { + const routerClient = new AppRouterClient(req, res, options) + await routerClient.createStore(); + return await route(routerClient); + } else { + return new Response('This page could not be found.', {status: 404}); + } + }; /** diff --git a/src/routerClients/AppRouterClient.js b/src/routerClients/AppRouterClient.js index 615a5cf1..0d9d5d76 100644 --- a/src/routerClients/AppRouterClient.js +++ b/src/routerClients/AppRouterClient.js @@ -36,12 +36,16 @@ export default class AppRouterClient extends RouterClient { this.clientConfig ); this.url = new URL(req.url); - this.sessionManager = appRouterSessionManager(cookies()); + this.req = req; this.searchParams = req.nextUrl.searchParams; this.onErrorCallback = options?.onError; } + async createStore () { + this.sessionManager = appRouterSessionManager(await cookies()); + } + /** * * @param {string} url diff --git a/src/session/getAccessToken.js b/src/session/getAccessToken.js index 82f76741..faae5808 100644 --- a/src/session/getAccessToken.js +++ b/src/session/getAccessToken.js @@ -18,7 +18,7 @@ import {jwtDecoder} from '@kinde/jwt-decoder'; export const getAccessTokenFactory = (req, res) => async () => { try { return jwtDecoder( - await sessionManager(req, res).getSessionItem('access_token') + await (await sessionManager(req, res)).getSessionItem('access_token') ); } catch (err) { if (config.isDebugMode) { diff --git a/src/session/getClaim.js b/src/session/getClaim.js index 8b033ac8..12d8d4ae 100644 --- a/src/session/getClaim.js +++ b/src/session/getClaim.js @@ -18,7 +18,7 @@ import {config} from '../config/index'; export const getClaimFactory = (req, res) => async (claim, type) => { try { const kindeClaim = await kindeClient.getClaim( - sessionManager(req, res), + await sessionManager(req, res), claim, type ); diff --git a/src/session/getFlag.js b/src/session/getFlag.js index c5486f8c..5a0ee76d 100644 --- a/src/session/getFlag.js +++ b/src/session/getFlag.js @@ -19,7 +19,7 @@ export const getFlagFactory = (req, res) => async (code, defaultValue, flagType) => { try { const flag = await kindeClient.getFlag( - sessionManager(req, res), + await sessionManager(req, res), code, defaultValue, flagType diff --git a/src/session/getOrganization.js b/src/session/getOrganization.js index cd50756b..837e3054 100644 --- a/src/session/getOrganization.js +++ b/src/session/getOrganization.js @@ -14,13 +14,14 @@ import {config} from '../config/index'; */ export const getOrganizationFactory = (req, res) => async () => { try { - const org = await kindeClient.getOrganization(sessionManager(req, res)); + const session = await sessionManager(req, res) + const org = await kindeClient.getOrganization(session); const orgName = await kindeClient.getClaimValue( - sessionManager(req, res), + session, 'org_name' ); const orgProperties = await kindeClient.getClaimValue( - sessionManager(req, res), + session, 'organization_properties' ); diff --git a/src/session/getPermission.js b/src/session/getPermission.js index eff16352..1281442c 100644 --- a/src/session/getPermission.js +++ b/src/session/getPermission.js @@ -17,7 +17,7 @@ import {config} from '../config/index'; export const getPermissionFactory = (req, res) => async (name) => { try { const permission = await kindeClient.getPermission( - sessionManager(req, res), + await sessionManager(req, res), name ); return permission; diff --git a/src/session/getPermissions.js b/src/session/getPermissions.js index 10362d2e..b740a4f1 100644 --- a/src/session/getPermissions.js +++ b/src/session/getPermissions.js @@ -15,7 +15,7 @@ import {config} from '../config/index'; export const getPermissionsFactory = (req, res) => async () => { try { const permissions = await kindeClient.getPermissions( - sessionManager(req, res) + await sessionManager(req, res) ); return permissions; } catch (error) { diff --git a/src/session/getRoles.js b/src/session/getRoles.js index fbd0f9f4..147aa7cb 100644 --- a/src/session/getRoles.js +++ b/src/session/getRoles.js @@ -15,7 +15,7 @@ import {config} from '../config/index'; export const getRolesFactory = (req, res) => async () => { try { const roles = await kindeClient.getClaimValue( - sessionManager(req, res), + await sessionManager(req, res), 'roles' ); return roles; diff --git a/src/session/getUser.ts b/src/session/getUser.ts index 271dc720..0f63c458 100644 --- a/src/session/getUser.ts +++ b/src/session/getUser.ts @@ -10,11 +10,11 @@ export const getUserFactory = async >(): Promise> => { try { const idToken = jwtDecoder( - (await sessionManager(req, res).getSessionItem('id_token')) as string + (await (await sessionManager(req, res)).getSessionItem('id_token')) as string ); const accessToken = jwtDecoder( - (await sessionManager(req, res).getSessionItem( + (await (await sessionManager(req, res)).getSessionItem( 'access_token' )) as string ); diff --git a/src/session/getUserOrganizations.ts b/src/session/getUserOrganizations.ts index a61d3b99..2c46bb2c 100644 --- a/src/session/getUserOrganizations.ts +++ b/src/session/getUserOrganizations.ts @@ -8,7 +8,7 @@ export const getUserOrganizationsFactory = (req?: NextApiRequest, res?: NextApiResponse) => async (): Promise => { try { - const session = sessionManager(req, res); + const session = await sessionManager(req, res); const userOrgs = await kindeClient.getUserOrganizations(session); const orgNames = (await kindeClient.getClaimValue( session, diff --git a/src/session/index.ts b/src/session/index.ts index 1f61d723..441b47d4 100644 --- a/src/session/index.ts +++ b/src/session/index.ts @@ -25,7 +25,7 @@ export default function (req?: NextApiRequest, res?: NextApiResponse) { try { // @ts-ignore const response = await kindeClient.refreshTokens( - sessionManager(req, res) + await sessionManager(req, res) ); return response; } catch (error) { diff --git a/src/session/sessionManager.js b/src/session/sessionManager.js index fb496e6c..0f257db8 100644 --- a/src/session/sessionManager.js +++ b/src/session/sessionManager.js @@ -27,12 +27,12 @@ const COOKIE_LIST = [ * * @param {import('next').NextApiRequest} [req] * @param {import('next').NextApiResponse} [res] - * @returns {import('@kinde-oss/kinde-typescript-sdk').SessionManager} + * @returns {Promise} */ -export const sessionManager = (req, res) => { - if (!req) return appRouterSessionManager(cookies()); +export const sessionManager = async (req, res) => { + if (!req) return appRouterSessionManager(await cookies()); return isAppRouter(req) - ? appRouterSessionManager(cookies()) + ? appRouterSessionManager(await cookies()) : pageRouterSessionManager(req, res); };