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

feat: base64-encode session cookie value #698

Closed
wants to merge 1 commit into from
Closed
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/eight-moose-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@supabase/auth-helpers-shared': minor
---

Encode session cookies with base64
20 changes: 11 additions & 9 deletions packages/shared/src/utils/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export function parseSupabaseCookie(str: string | null | undefined): Partial<Ses
}

try {
const session = JSON.parse(str);
const decoder = new TextDecoder();
const session = JSON.parse(decoder.decode(base64url.decode(str)));
if (!session) {
return null;
}
Expand All @@ -42,7 +43,6 @@ export function parseSupabaseCookie(str: string | null | undefined): Partial<Ses

const [_header, payloadStr, _signature] = session[0].split('.');
const payload = base64url.decode(payloadStr);
const decoder = new TextDecoder();

const { exp, sub, ...user } = JSON.parse(decoder.decode(payload));

Expand All @@ -67,11 +67,13 @@ export function parseSupabaseCookie(str: string | null | undefined): Partial<Ses
}

export function stringifySupabaseSession(session: Session): string {
return JSON.stringify([
session.access_token,
session.refresh_token,
session.provider_token,
session.provider_refresh_token,
session.user?.factors ?? null
]);
return base64url.encode(
JSON.stringify([
session.access_token,
session.refresh_token,
session.provider_token,
session.provider_refresh_token,
session.user?.factors ?? null
])
);
}