diff options
| author | Fuwn <[email protected]> | 2024-01-06 16:43:50 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2024-01-06 16:43:50 -0800 |
| commit | 4353723e7e51d0c8f21f555ee713199c230b0a90 (patch) | |
| tree | 3f9e7581489cd03d9a3ba590d4e410b282d7ffac /src/routes/api | |
| parent | feat(static): privacy.txt (diff) | |
| download | due.moe-4353723e7e51d0c8f21f555ee713199c230b0a90.tar.xz due.moe-4353723e7e51d0c8f21f555ee713199c230b0a90.zip | |
refactor(api): simple oauth handler
Diffstat (limited to 'src/routes/api')
| -rw-r--r-- | src/routes/api/oauth/animeschedule/callback/+server.ts | 43 | ||||
| -rw-r--r-- | src/routes/api/oauth/callback/+server.ts | 42 |
2 files changed, 25 insertions, 60 deletions
diff --git a/src/routes/api/oauth/animeschedule/callback/+server.ts b/src/routes/api/oauth/animeschedule/callback/+server.ts index 450c74cf..a5afa303 100644 --- a/src/routes/api/oauth/animeschedule/callback/+server.ts +++ b/src/routes/api/oauth/animeschedule/callback/+server.ts @@ -1,34 +1,17 @@ -import { dev } from '$app/environment'; +import { callback } from '$lib/oauth.js'; import { env } from '$env/dynamic/private'; import { env as env2 } from '$env/dynamic/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', env2.PUBLIC_ANIMESCHEDULE_CLIENT_ID as string); - formData.append('client_secret', env.ANIMESCHEDULE_CLIENT_SECRET as string); - formData.append('redirect_uri', env2.PUBLIC_ANIMESCHEDULE_REDIRECT_URI as string); - formData.append('code', url.searchParams.get('code') || 'null'); - cookies.set( - 'animeschedule', - JSON.stringify( - await ( - await fetch('https://animeschedule.net/api/v3/oauth2/token', { - method: 'POST', - body: formData - }) - ).json() - ), - { - path: '/', - maxAge: 60 * 60 * 24 * 7, - httpOnly: true, - sameSite: 'lax', - secure: !dev +export const GET = async ({ url, cookies }) => + callback({ + url, + cookies, + cookie: 'animeschedule', + authorise: 'https://animeschedule.net/api/v3/oauth2/token', + redirect: '/settings', + client: { + id: env2.PUBLIC_ANIMESCHEDULE_CLIENT_ID, + secret: env.ANIMESCHEDULE_CLIENT_SECRET, + redirectURI: env2.PUBLIC_ANIMESCHEDULE_REDIRECT_URI } - ); - - throw redirect(303, '/settings'); -}; + }); diff --git a/src/routes/api/oauth/callback/+server.ts b/src/routes/api/oauth/callback/+server.ts index 8df76c50..0705df39 100644 --- a/src/routes/api/oauth/callback/+server.ts +++ b/src/routes/api/oauth/callback/+server.ts @@ -1,34 +1,16 @@ -import { dev } from '$app/environment'; +import { callback } from '$lib/oauth.js'; import { env } from '$env/dynamic/private'; import { env as env2 } from '$env/dynamic/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', env2.PUBLIC_ANILIST_CLIENT_ID as string); - formData.append('client_secret', env.ANILIST_CLIENT_SECRET as string); - formData.append('redirect_uri', env2.PUBLIC_ANILIST_REDIRECT_URI as string); - 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: 'lax', - secure: !dev +export const GET = async ({ url, cookies }) => + callback({ + url, + cookies, + cookie: 'user', + authorise: 'https://anilist.co/api/v2/oauth/token', + client: { + id: env2.PUBLIC_ANILIST_CLIENT_ID, + secret: env.ANILIST_CLIENT_SECRET, + redirectURI: env2.PUBLIC_ANILIST_REDIRECT_URI } - ); - - throw redirect(303, '/'); -}; + }); |