feat: add authentication

This commit is contained in:
2023-08-04 22:35:25 +02:00
parent f9638c35fc
commit 469db6525d
33 changed files with 492 additions and 100 deletions

29
routes/api/auth/login.ts Normal file
View File

@@ -0,0 +1,29 @@
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,
});
},
};