From f7a8c246f5723aae42a38bb88631893bb1b24d6e Mon Sep 17 00:00:00 2001 From: Fuwn Date: Sat, 13 Jan 2024 18:58:34 -0800 Subject: refactor(utility): move helpers to utility --- src/lib/oauth.ts | 52 ---------------------------------------------------- 1 file changed, 52 deletions(-) delete mode 100644 src/lib/oauth.ts (limited to 'src/lib/oauth.ts') 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 ?? '/'); -}; -- cgit v1.2.3