Skip to content

Commit

Permalink
Fix for setting custom cookies in withMiddlewareAuthRequired (#1263)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamjmcgrath committed Jun 26, 2023
2 parents 309581c + 5c7d8cf commit a2d6454
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
10 changes: 6 additions & 4 deletions src/helpers/with-middleware-auth-required.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { NextMiddleware, NextRequest, NextResponse } from 'next/server';
import { SessionCache } from '../session';
import { splitCookiesString } from '../utils/middleware-cookies';

/**
* Protect your pages with Next.js Middleware. For example:
Expand Down Expand Up @@ -77,10 +78,11 @@ export default function withMiddlewareAuthRequiredFactory(

if (res) {
const headers = new Headers(res.headers);
const cookies = headers.get('set-cookie')?.split(', ') || [];
const authCookies = authRes.headers.get('set-cookie')?.split(', ') || [];
if (cookies.length || authCookies.length) {
headers.set('set-cookie', [...authCookies, ...cookies].join(', '));
const authCookies = splitCookiesString(authRes.headers.get('set-cookie')!);
if (authCookies.length) {
for (const cookie of authCookies) {
headers.append('set-cookie', cookie);
}
}
return NextResponse.next({ ...res, status: res.status, headers });
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/middleware-cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default class MiddlewareCookies extends Cookies {
* Handle cookies with commas, eg `foo=; Expires=Thu, 01 Jan 1970 00:00:00 GMT`
* @source https://github.com/vercel/edge-runtime/blob/90160abc42e6139c41494c5d2e98f09e9a5fa514/packages/cookies/src/response-cookies.ts#L128
*/
function splitCookiesString(cookiesString: string) {
export function splitCookiesString(cookiesString: string) {
if (!cookiesString) return [];
const cookiesStrings = [];
let pos = 0;
Expand Down
11 changes: 10 additions & 1 deletion tests/helpers/with-middleware-auth-required.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,16 @@ describe('with-middleware-auth-required', () => {
};
const res = await setup({ user: { name: 'dave' }, middleware });
expect(res.status).toEqual(200);
expect(res.headers.get('set-cookie')).toMatch(/^appSession=.+, foo=bar;/);
expect(res.headers.get('set-cookie')).toMatch(/appSession=/);
expect(res.headers.get('set-cookie')).toMatch(/foo=bar;/);
});

test('should set status from custom middleware', async () => {
const middleware = () => {
return new NextResponse(null, { status: 400 });
};
const res = await setup({ user: { name: 'dave' }, middleware });
expect(res.status).toEqual(400);
});

test('should set just a custom cookie when session is not rolling', async () => {
Expand Down

0 comments on commit a2d6454

Please sign in to comment.