Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for setting custom cookies in withMiddlewareAuthRequired #1263

Merged
merged 3 commits into from
Jun 26, 2023
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
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