Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/nextjs/src/middleware/FronteggApiMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const middlewarePromise = (req: NextApiRequest, res: NextApiResponse, options?:
}
const headers: Record<string, string> = {};
if (process.env['FRONTEGG_SECURE_JWT_ENABLED'] === 'true') {
const session = await getSession(req);
const session = await getSession(req, res);
if (session?.accessToken) {
headers['authorization'] = 'Bearer ' + session.accessToken;
}
Expand Down
13 changes: 12 additions & 1 deletion packages/nextjs/src/pages/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,18 @@ import { hasSetSessionCookie } from '../utils/refreshAccessTokenIfNeeded/helpers
import { NextResponse } from 'next/server';
import { ServerResponse } from 'http';

export const getSession = (req: RequestType, res?: ServerResponse) => {
/**
* Retrieves the Frontegg session from Next.js request and response objects.
*
* @param req - The Next.js request object containing incoming HTTP request data
* @param res - The Next.js response object (or undefined). This parameter is required and must be explicitly passed.
* It will be non-null in cases where there are sequential redirects or multiple getInitialProps
* executions within a single page visit — for example, when middlewares or getServerSideProps
* functions are concatenated.
*
* @returns The Frontegg session if available, or undefined if no session exists
*/
export const getSession = (req: RequestType, res: ServerResponse | undefined) => {
if (res && hasSetSessionCookie(res.getHeader('set-cookie'))) {
const cookies = CookieManager.getSessionCookieFromRedirectedResponse(res);
return createSession(cookies, encryption);
Expand Down
Loading