Skip to content

Commit 8eaf28b

Browse files
authored
FR-22443 Enhance getSession function to require response object (#436)
1 parent c0d8d3d commit 8eaf28b

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

packages/nextjs/src/middleware/FronteggApiMiddleware.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const middlewarePromise = (req: NextApiRequest, res: NextApiResponse, options?:
4949
}
5050
const headers: Record<string, string> = {};
5151
if (process.env['FRONTEGG_SECURE_JWT_ENABLED'] === 'true') {
52-
const session = await getSession(req);
52+
const session = await getSession(req, res);
5353
if (session?.accessToken) {
5454
headers['authorization'] = 'Bearer ' + session.accessToken;
5555
}

packages/nextjs/src/pages/helpers.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,18 @@ import { hasSetSessionCookie } from '../utils/refreshAccessTokenIfNeeded/helpers
1010
import { NextResponse } from 'next/server';
1111
import { ServerResponse } from 'http';
1212

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

0 commit comments

Comments
 (0)