diff options
| author | Fuwn <[email protected]> | 2023-09-17 17:34:13 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2023-09-17 17:34:13 -0700 |
| commit | 8b9f283ae527a13b26bcbb02fef4dd9d09a89661 (patch) | |
| tree | 42707cf8947e7f49ca6f9094e868c0e59ef9cd2b | |
| parent | feat(html): add precomposed icon (diff) | |
| download | due.moe-8b9f283ae527a13b26bcbb02fef4dd9d09a89661.tar.xz due.moe-8b9f283ae527a13b26bcbb02fef4dd9d09a89661.zip | |
feat(oauth): refresh token route
| -rw-r--r-- | src/routes/api/oauth/callback/+server.ts (renamed from src/routes/api/oauth-callback/+server.ts) | 0 | ||||
| -rw-r--r-- | src/routes/api/oauth/refresh/+server.ts | 36 |
2 files changed, 36 insertions, 0 deletions
diff --git a/src/routes/api/oauth-callback/+server.ts b/src/routes/api/oauth/callback/+server.ts index 33ba6de9..33ba6de9 100644 --- a/src/routes/api/oauth-callback/+server.ts +++ b/src/routes/api/oauth/callback/+server.ts diff --git a/src/routes/api/oauth/refresh/+server.ts b/src/routes/api/oauth/refresh/+server.ts new file mode 100644 index 00000000..43e9136c --- /dev/null +++ b/src/routes/api/oauth/refresh/+server.ts @@ -0,0 +1,36 @@ +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 }) => { + const formData = new FormData(); + + formData.append('grant_type', 'refresh_token'); + formData.append('client_id', env2.PUBLIC_ANILIST_CLIENT_ID); + formData.append('client_secret', env.ANILIST_CLIENT_SECRET); + 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 + } + ); + + throw redirect(303, '/'); +}; |