diff options
Diffstat (limited to 'src/routes/oauth/callback/+server.ts')
| -rw-r--r-- | src/routes/oauth/callback/+server.ts | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/routes/oauth/callback/+server.ts b/src/routes/oauth/callback/+server.ts new file mode 100644 index 00000000..4b0b6739 --- /dev/null +++ b/src/routes/oauth/callback/+server.ts @@ -0,0 +1,34 @@ +import { dev } from '$app/environment'; +import { ANILIST_CLIENT_SECRET } from '$env/static/private'; +import { PUBLIC_ANILIST_CLIENT_ID, PUBLIC_ANILIST_REDIRECT_URI } from '$env/static/public'; +import { redirect } from '@sveltejs/kit'; + +export const GET = async ({ url, cookies }) => { + const formData = new FormData(); + + formData.append('grant_type', 'authorization_code'); + formData.append('client_id', PUBLIC_ANILIST_CLIENT_ID); + formData.append('client_secret', ANILIST_CLIENT_SECRET); + formData.append('redirect_uri', PUBLIC_ANILIST_REDIRECT_URI); + formData.append('code', url.searchParams.get('code') || "null"); + 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: 'strict', + secure: !dev + } + ); + + throw redirect(303, '/'); +}; |