|
| 1 | +import getStore from '../../../../lib/store'; |
| 2 | +import { randomBytes } from 'crypto'; |
| 3 | + |
| 4 | +import type { PageServerLoad } from './$types' |
| 5 | +import { redirect, error } from '@sveltejs/kit'; |
| 6 | + |
| 7 | +const createId = (bytes: number): Promise<string> => new Promise((res, rej) => { |
| 8 | + randomBytes(bytes, (err, buf) => { |
| 9 | + if (err) return rej(err) |
| 10 | + |
| 11 | + res(buf.toString('base64url')) |
| 12 | + }) |
| 13 | +}) |
| 14 | + |
| 15 | +export const load: PageServerLoad = async ({ params, cookies, url }) => { |
| 16 | + const { providerName } = params; |
| 17 | + const { |
| 18 | + connection: connectionStore, |
| 19 | + authMethod: authMethodStore, |
| 20 | + user: userStore, |
| 21 | + session: sessionStore, |
| 22 | + authProvider: authProviderStore |
| 23 | + } = await getStore() |
| 24 | + |
| 25 | + const authProvider = await authProviderStore.get(providerName) |
| 26 | + |
| 27 | + if (!authProvider) { |
| 28 | + throw error(403, 'forbidden') |
| 29 | + } |
| 30 | + |
| 31 | + const accessTokenResponse = await fetch('https://github.com/login/oauth/access_token', { |
| 32 | + method: 'POST', |
| 33 | + headers: { |
| 34 | + 'Accept': 'application/json', |
| 35 | + ['Content-Type']: 'application/json', |
| 36 | + }, |
| 37 | + body: JSON.stringify({ |
| 38 | + client_id: authProvider.clientId, |
| 39 | + client_secret: authProvider.clientSecret, |
| 40 | + code: url.searchParams.get('code'), |
| 41 | + response_type: 'code', |
| 42 | + grant_type: 'authorization_code', |
| 43 | + redirect_uri: authProvider.callbackUrl |
| 44 | + }) |
| 45 | + }) |
| 46 | + |
| 47 | + |
| 48 | + const accessTokenResponsePayload = await accessTokenResponse.json(); |
| 49 | + const token = accessTokenResponsePayload.access_token; |
| 50 | + |
| 51 | + const ghUserResponse = await fetch('https://api.github.com/user', { |
| 52 | + method: 'GET', |
| 53 | + headers: { |
| 54 | + Authorization: `Bearer ${token}` |
| 55 | + } |
| 56 | + }) |
| 57 | + const userData = await ghUserResponse.json() |
| 58 | + |
| 59 | + const authMethods = await authMethodStore.list(); |
| 60 | + const authMethod = authMethods |
| 61 | + .filter(authMethod => authMethod.authProviderType === providerName) |
| 62 | + .find(authMethod => authMethod.identifier === userData.id) |
| 63 | + |
| 64 | + if (authMethod) { |
| 65 | + // login flow |
| 66 | + const sessionId = await createId(24) |
| 67 | + |
| 68 | + await sessionStore.set(sessionId, { |
| 69 | + userId: authMethod.userId, |
| 70 | + sessionId |
| 71 | + }) |
| 72 | + |
| 73 | + cookies.set('imtala_session_id', sessionId, { path: '/' }) |
| 74 | + |
| 75 | + throw redirect(303, '/') |
| 76 | + } |
| 77 | + |
| 78 | + // signup & create connection flow |
| 79 | + |
| 80 | + const connectionId = await createId(24) |
| 81 | + const userId = await createId(24) |
| 82 | + const authMethodId = await createId(24) |
| 83 | + const sessionId = await createId(24) |
| 84 | + |
| 85 | + |
| 86 | + await authMethodStore.set(userData.id, { |
| 87 | + identifier: userData.id, |
| 88 | + authProviderType: 'github', |
| 89 | + userId: userId, |
| 90 | + token |
| 91 | + }) |
| 92 | + |
| 93 | + await connectionStore.set(connectionId, { |
| 94 | + name: providerName, |
| 95 | + connectionId, |
| 96 | + userId, |
| 97 | + authMethodId |
| 98 | + }) |
| 99 | + |
| 100 | + await userStore.set(userId, { |
| 101 | + fullName: userData.name, |
| 102 | + userId |
| 103 | + }) |
| 104 | + |
| 105 | + await sessionStore.set(sessionId, { |
| 106 | + userId: userId, |
| 107 | + sessionId |
| 108 | + }) |
| 109 | + |
| 110 | + cookies.set('imtala_session_id', sessionId, { path: '/' }) |
| 111 | + |
| 112 | + throw redirect(303, '/') |
| 113 | + |
| 114 | +} |
0 commit comments