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 cookie saving in middleware/routeHandler #536

Merged
merged 1 commit into from
May 10, 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
5 changes: 5 additions & 0 deletions .changeset/eighty-chefs-protect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@supabase/auth-helpers-nextjs': patch
---

fix cookie saving in middleware/routeHandler
13 changes: 12 additions & 1 deletion packages/nextjs/src/middlewareClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
SupabaseClientOptionsWithoutAuth
} from '@supabase/auth-helpers-shared';
import { NextRequest, NextResponse } from 'next/server';
import { splitCookiesString } from 'set-cookie-parser';

class NextMiddlewareAuthStorageAdapter extends CookieAuthStorageAdapter {
constructor(
Expand All @@ -18,6 +19,16 @@ class NextMiddlewareAuthStorageAdapter extends CookieAuthStorageAdapter {
}

protected getCookie(name: string): string | null | undefined {
const setCookie = splitCookiesString(
this.context.res.headers.get('set-cookie')?.toString() ?? ''
)
.map((c) => parseCookies(c)[name])
.find((c) => !!c);

if (setCookie) {
return setCookie;
}

const cookies = parseCookies(this.context.req.headers.get('cookie') ?? '');
return cookies[name];
}
Expand All @@ -38,7 +49,7 @@ class NextMiddlewareAuthStorageAdapter extends CookieAuthStorageAdapter {
httpOnly: false
});

this.context.res.headers.set('set-cookie', newSessionStr);
this.context.res.headers.append('set-cookie', newSessionStr);
}
}

Expand Down
5 changes: 2 additions & 3 deletions packages/nextjs/src/routeHandlerClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { createServerComponentSupabaseClient } from './serverComponentClient';
import { createMiddlewareSupabaseClient } from './middlewareClient';

export const createRouteHandlerSupabaseClient =
createServerComponentSupabaseClient;
export const createRouteHandlerSupabaseClient = createMiddlewareSupabaseClient;
2 changes: 0 additions & 2 deletions packages/nextjs/src/serverClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ class NextServerAuthStorageAdapter extends CookieAuthStorageAdapter {
.map((c) => parseCookies(c)[name])
.find((c) => !!c);

// const setCookie = setCookies.find((c) => c.name === name);
const value = setCookie ?? this.context.req.cookies[name];

return value;
}
protected setCookie(name: string, value: string): void {
Expand Down