Skip to content

Commit

Permalink
feat: nextjs 15 support, update cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielRivers committed Oct 22, 2024
1 parent 55ccdd9 commit 544f3d7
Show file tree
Hide file tree
Showing 14 changed files with 33 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/api-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`, {
Expand Down
14 changes: 9 additions & 5 deletions src/handlers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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});
}

};

/**
Expand Down
6 changes: 5 additions & 1 deletion src/routerClients/AppRouterClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/session/getAccessToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/session/getClaim.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
2 changes: 1 addition & 1 deletion src/session/getFlag.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions src/session/getOrganization.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);

Expand Down
2 changes: 1 addition & 1 deletion src/session/getPermission.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/session/getPermissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/session/getRoles.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/session/getUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ export const getUserFactory =
async <T = Record<string, any>>(): Promise<KindeUser<T>> => {
try {
const idToken = jwtDecoder<KindeIdToken>(
(await sessionManager(req, res).getSessionItem('id_token')) as string
(await (await sessionManager(req, res)).getSessionItem('id_token')) as string
);

const accessToken = jwtDecoder<KindeAccessToken>(
(await sessionManager(req, res).getSessionItem(
(await (await sessionManager(req, res)).getSessionItem(
'access_token'
)) as string
);
Expand Down
2 changes: 1 addition & 1 deletion src/session/getUserOrganizations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const getUserOrganizationsFactory =
(req?: NextApiRequest, res?: NextApiResponse) =>
async (): Promise<KindeOrganizations | null> => {
try {
const session = sessionManager(req, res);
const session = await sessionManager(req, res);
const userOrgs = await kindeClient.getUserOrganizations(session);
const orgNames = (await kindeClient.getClaimValue(
session,
Expand Down
2 changes: 1 addition & 1 deletion src/session/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions src/session/sessionManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<import('@kinde-oss/kinde-typescript-sdk').SessionManager>}
*/
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);
};

Expand Down

0 comments on commit 544f3d7

Please sign in to comment.