diff options
| author | Fuwn <[email protected]> | 2023-12-15 23:54:53 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2023-12-15 23:54:53 -0800 |
| commit | f056a1e19470967198b1dab61cf6133047f07afd (patch) | |
| tree | ea3ac4647037c257d1866aff7ff23cf64d9f9f3b /src/routes/api/oauth/refresh/+server.ts | |
| parent | feat(media): multiple dueincludes (diff) | |
| download | due.moe-f056a1e19470967198b1dab61cf6133047f07afd.tar.xz due.moe-f056a1e19470967198b1dab61cf6133047f07afd.zip | |
feat(notifications): refresh token
Diffstat (limited to 'src/routes/api/oauth/refresh/+server.ts')
| -rw-r--r-- | src/routes/api/oauth/refresh/+server.ts | 43 |
1 files changed, 18 insertions, 25 deletions
diff --git a/src/routes/api/oauth/refresh/+server.ts b/src/routes/api/oauth/refresh/+server.ts index 0e6eef8c..ceb2566d 100644 --- a/src/routes/api/oauth/refresh/+server.ts +++ b/src/routes/api/oauth/refresh/+server.ts @@ -1,36 +1,29 @@ import { dev } from '$app/environment'; import { env } from '$env/dynamic/private'; import { env as env2 } from '$env/dynamic/public'; -import { redirect } from '@sveltejs/kit'; -export const GET = async ({ cookies }) => { +export const GET = async ({ url, cookies }) => { const formData = new FormData(); formData.append('grant_type', 'refresh_token'); formData.append('client_id', env2.PUBLIC_ANILIST_CLIENT_ID as string); formData.append('client_secret', env.ANILIST_CLIENT_SECRET as string); - formData.append( - 'refresh_token', - JSON.parse(cookies.get('user') || '{ refresh_token: null }')['refresh_token'] - ); - cookies.set( - 'user', - JSON.stringify( - await ( - await fetch('https://anilist.co/api/v2/oauth/token', { - method: 'POST', - body: formData - }) - ).json() - ), - { - path: '/', - maxAge: 60 * 60 * 24 * 7, - httpOnly: true, - sameSite: 'lax', - secure: !dev - } - ); + formData.append('refresh_token', url.searchParams.get('token') || ''); - throw redirect(303, '/'); + const newUser = await ( + await fetch('https://anilist.co/api/v2/oauth/token', { + method: 'POST', + body: formData + }) + ).json(); + + cookies.set('user', JSON.stringify(newUser), { + path: '/', + maxAge: 60 * 60 * 24 * 7, + httpOnly: true, + sameSite: 'lax', + secure: !dev + }); + + return Response.json(newUser); }; |