Something went wrong. Try again.
Monorepo for Tangled forked from tangled.org/core
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344import type { RequestHandler } from "./$types";import { authorizeUrl, githubCookies, githubEnv, hasInstallations, installUrl, landingFrom, landingHref, newVerifier, pkceChallenge, redirectTo, requestOrigin, sealState, sessionToken} from "$lib/server/github";import { CURRENT_DID_KEY } from "$lib/auth/accounts";
export const GET: RequestHandler = async ({ url, request, cookies, platform, fetch }) => { const landing = landingFrom(url.searchParams.get("return_to")); const failure = (message: string) => redirectTo(landingHref(landing, message)); const did = cookies.get(CURRENT_DID_KEY); if (!did) return failure("Sign in before connecting GitHub"); const env = githubEnv(platform); if (!env.clientId || !env.clientSecret || !env.secret) return failure("GitHub is not configured"); const verifier = newVerifier(); const state = await sealState(env.secret, { landing, did, verifier }); githubCookies(cookies, url, request.headers).state.write(state);
const token = await sessionToken(cookies, platform); const installed = token ? await hasInstallations(env, token, fetch).catch(() => false) : false; return redirectTo( env.appSlug && token && !installed ? installUrl(env, state) : authorizeUrl( env, requestOrigin(url, request.headers), state, await pkceChallenge(verifier) ) );};