Files
memorium/routes/api/auth/login.ts
2026-01-10 13:03:29 +01:00

38 lines
954 B
TypeScript

import { oauth2Client } from "@lib/auth.ts";
import { setCookie } from "@std/http/cookie";
import { define } from "../../../utils.ts";
export const codeChallengeMap = new Map<
string,
{ codeVerifier: string; redirect?: string }
>();
export const handler = define.handlers({
async GET(ctx) {
const req = ctx.req;
const url = new URL(req.url);
const { codeVerifier, uri } = await oauth2Client.code.getAuthorizationUri();
const codeChallenge = uri.searchParams.get("code_challenge");
if (!codeChallenge) return new Response();
codeChallengeMap.set(codeChallenge, {
codeVerifier,
redirect: decodeURIComponent(url.searchParams.get("redirect") || ""),
});
const headers = new Headers();
setCookie(headers, {
name: "code_challenge",
value: codeChallenge,
});
headers.append("location", uri.href);
return new Response(null, {
headers,
status: 302,
});
},
});