Skip to content

Commit

Permalink
feat(auth): migrate serialized auth cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
seferturan authored and vladjerca committed Jan 10, 2025
1 parent 1e97b23 commit 1013056
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion projects/client/src/lib/features/auth/handle.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Handle } from '@sveltejs/kit';
import { type Handle, type RequestEvent } from '@sveltejs/kit';
import { AuthEndpoint } from './AuthEndpoint.ts';
import { key } from './environment.ts';
import type {
Expand All @@ -10,6 +10,15 @@ import { encrypt } from './utils/encrypt.ts';

const AUTH_COOKIE_NAME = 'trakt-auth';

function getLegacyAuthCookie(event: RequestEvent) {
try {
const serializedToken = event.cookies.get(AUTH_COOKIE_NAME) ?? '';
return JSON.parse(serializedToken) as SerializedAuthResponse;
} catch (error) {
return null;
}
}

export const handle: Handle = async ({ event, resolve }) => {
const setAuth = (auth: SerializedAuthResponse | Nil) => {
event.locals.auth = auth;
Expand Down Expand Up @@ -70,6 +79,25 @@ export const handle: Handle = async ({ event, resolve }) => {
});
}

//TODO: remove this migration after March 1st 2025
const legacyAuthCookie = getLegacyAuthCookie(event);
if (legacyAuthCookie != null) {
setAuth(legacyAuthCookie);

event.cookies.set(
AUTH_COOKIE_NAME,
await encrypt(key, legacyAuthCookie),
{
httpOnly: true,
secure: true,
maxAge: legacyAuthCookie.expiresAt ?? 0,
path: '/',
},
);

return await resolve(event);
}

/**
* TODO: refresh exchange flow here
* https://trakt.docs.apiary.io/#reference/authentication-oauth/get-token/exchange-refresh_token-for-access_token
Expand Down

0 comments on commit 1013056

Please sign in to comment.