(input: APIEvent)
| 6 | import { localeFromRequest, route } from "~/lib/language" |
| 7 | |
| 8 | export async function GET(input: APIEvent) { |
| 9 | const url = new URL(input.request.url) |
| 10 | const locale = localeFromRequest(input.request) |
| 11 | const dict = i18n(locale) |
| 12 | |
| 13 | try { |
| 14 | const code = url.searchParams.get("code") |
| 15 | if (!code) throw new Error(dict["auth.callback.error.codeMissing"]) |
| 16 | const result = await AuthClient.exchange(code, `${url.origin}${url.pathname}`) |
| 17 | if (result.err) throw new Error(result.err.message) |
| 18 | const decoded = AuthClient.decode(result.tokens.access, {} as any) |
| 19 | if (decoded.err) throw new Error(decoded.err.message) |
| 20 | const session = await useAuthSession() |
| 21 | const id = decoded.subject.properties.accountID |
| 22 | await session.update((value) => { |
| 23 | return { |
| 24 | ...value, |
| 25 | account: { |
| 26 | ...value.account, |
| 27 | [id]: { |
| 28 | id, |
| 29 | email: decoded.subject.properties.email, |
| 30 | }, |
| 31 | }, |
| 32 | current: id, |
| 33 | } |
| 34 | }) |
| 35 | const next = url.pathname === "/auth/callback" ? "/auth" : url.pathname.replace("/auth/callback", "") |
| 36 | return redirect(route(locale, next)) |
| 37 | } catch (e: any) { |
| 38 | return new Response( |
| 39 | JSON.stringify({ |
| 40 | error: e.message, |
| 41 | cause: Object.fromEntries(url.searchParams.entries()), |
| 42 | }), |
| 43 | { status: 500 }, |
| 44 | ) |
| 45 | } |
| 46 | } |
nothing calls this directly
no test coverage detected