Skip to content

Commit

Permalink
Merge pull request #226 from kinde-oss/feat/next15
Browse files Browse the repository at this point in the history
Next 15 support
  • Loading branch information
peterphanouvong authored Oct 23, 2024
2 parents 8eed0fa + 7d5ab3e commit 62e54af
Show file tree
Hide file tree
Showing 17 changed files with 49 additions and 32 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@
"typescript": "^5.1.3"
},
"peerDependencies": {
"next": "^12.2.5 || ^13 || ^14",
"react": "^18.1.0",
"react-dom": "^18.1.0"
"next": "^12.2.5 || ^13 || ^14 || ^15",
"react": "^18 || ^19",
"react-dom": "^18 || ^19"
},
"dependencies": {
"@babel/preset-env": "^7.25.4",
Expand Down
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/getAccessTokenRaw.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ import {sessionManager} from './sessionManager';
*/
// @ts-ignore
export const getAccessTokenRawFactory = (req, res) => async () => {
return await sessionManager(req, res).getSessionItem('access_token');
return await (await sessionManager(req, res)).getSessionItem('access_token');
};
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
6 changes: 4 additions & 2 deletions src/session/getUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ export const getUserFactory =
(req: NextApiRequest, res: NextApiResponse) =>
async <T = Record<string, any>>(): Promise<KindeUser<T>> => {
try {
const session = await sessionManager(req, res);

const idToken = jwtDecoder<KindeIdToken>(
(await sessionManager(req, res).getSessionItem('id_token')) as string
(await session.getSessionItem('id_token')) as string
);

const accessToken = jwtDecoder<KindeAccessToken>(
(await sessionManager(req, res).getSessionItem(
(await session.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
18 changes: 12 additions & 6 deletions src/session/sessionManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,19 @@ 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());
return isAppRouter(req)
? appRouterSessionManager(cookies())
: pageRouterSessionManager(req, res);
export const sessionManager = async (req, res) => {
try{
const cookieStore = await cookies();
if (!req) return appRouterSessionManager(cookieStore);
return isAppRouter(req)
? appRouterSessionManager(cookieStore)
: pageRouterSessionManager(req, res);
} catch (error) {
console.error('Failed to initialize session manager:', error);
throw error;
}
};

/**
Expand Down
4 changes: 2 additions & 2 deletions src/utils/appRouter/setVerifierCookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import {config} from '../../config/index';
import {cookies} from 'next/headers';
import {GLOBAL_COOKIE_OPTIONS} from '../../session/sessionManager';

export const setVerifierCookie = (state, code_verifier, options) => {
cookies().set({
export const setVerifierCookie = async (state, code_verifier, options) => {
(await cookies()).set({
name: `${config.SESSION_PREFIX}-${state}`,
value: JSON.stringify({code_verifier, options}),
maxAge: 60 * 15,
Expand Down

0 comments on commit 62e54af

Please sign in to comment.