memorium/routes/api/auth/login.ts

30 lines
804 B
TypeScript

import { Handlers } from "$fresh/server.ts";
import { oauth2Client } from "@lib/auth.ts";
import { sha256 } from "@lib/string.ts";
import { setCookie } from "https://deno.land/std@0.197.0/http/cookie.ts";
export const codeChallengeMap = new Map();
export const handler: Handlers = {
async GET() {
const { codeVerifier, uri } = await oauth2Client.code.getAuthorizationUri();
const codeChallenge = uri.searchParams.get("code_challenge");
if (!codeChallenge) return new Response();
codeChallengeMap.set(codeChallenge, codeVerifier);
const headers = new Headers();
setCookie(headers, {
name: "code_challenge",
value: codeChallenge,
});
headers.append("location", uri.href);
return new Response(null, {
headers,
status: 302,
});
},
};