diff options
| author | Fuwn <[email protected]> | 2024-01-13 18:58:34 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2024-01-13 18:58:34 -0800 |
| commit | f7a8c246f5723aae42a38bb88631893bb1b24d6e (patch) | |
| tree | f7915250f57fde62fd72a351ffd6e79f0692292e /src/lib/oauth.ts | |
| parent | refactor(utility): move loading (diff) | |
| download | due.moe-f7a8c246f5723aae42a38bb88631893bb1b24d6e.tar.xz due.moe-f7a8c246f5723aae42a38bb88631893bb1b24d6e.zip | |
refactor(utility): move helpers to utility
Diffstat (limited to 'src/lib/oauth.ts')
| -rw-r--r-- | src/lib/oauth.ts | 52 |
1 files changed, 0 insertions, 52 deletions
diff --git a/src/lib/oauth.ts b/src/lib/oauth.ts deleted file mode 100644 index 1c35223d..00000000 --- a/src/lib/oauth.ts +++ /dev/null @@ -1,52 +0,0 @@ -import { dev } from '$app/environment'; -import { redirect, type Cookies } from '@sveltejs/kit'; - -export interface ClientOptions { - id: string; - secret: string; - redirectURI: string; -} - -export interface CallbackOptions { - url: URL; - cookies: Cookies; - cookie: string; - authorise: string; - redirect?: string; - client: ClientOptions; - verifier?: string; -} - -export const callback = async (options: CallbackOptions) => { - const { url, cookies, cookie, authorise, client } = options; - const formData = new FormData(); - - formData.append('grant_type', 'authorization_code'); - formData.append('client_id', client.id); - formData.append('client_secret', client.secret); - formData.append('redirect_uri', client.redirectURI); - formData.append('code', url.searchParams.get('code') || 'null'); - - if (options.verifier) formData.append('code_verifier', options.verifier); - - cookies.set( - cookie, - JSON.stringify( - await ( - await fetch(authorise, { - method: 'POST', - body: formData - }) - ).json() - ), - { - path: '/', - maxAge: 60 * 60 * 24 * 7, - httpOnly: true, - sameSite: 'lax', - secure: !dev - } - ); - - throw redirect(303, options.redirect ?? '/'); -}; |