feat: redirect to same url on login/logout

This commit is contained in:
2023-08-09 15:58:36 +02:00
parent f066b4e5e4
commit 7a5f43d799
5 changed files with 46 additions and 16 deletions

View File

@ -1,10 +1,7 @@
import { Handlers } from "$fresh/server.ts";
import { create, getNumericDate } from "https://deno.land/x/djwt@v2.2/mod.ts";
import { oauth2Client } from "@lib/auth.ts";
import {
getCookies,
setCookie,
} from "https://deno.land/std@0.197.0/http/cookie.ts";
import { getCookies, setCookie } from "$std/http/cookie.ts";
import { codeChallengeMap } from "./login.ts";
import { GITEA_SERVER, JWT_SECRET, SESSION_DURATION } from "@lib/env.ts";
import { userDB } from "@lib/db.ts";
@ -20,7 +17,12 @@ export const handler: Handlers = {
// Exchange the authorization code for an access token
const cookies = getCookies(request.headers);
const codeVerifier = codeChallengeMap.get(cookies["code_challenge"]);
const stored = codeChallengeMap.get(cookies["code_challenge"]);
if (!stored) {
throw new BadRequestError();
}
const { codeVerifier, redirect } = stored;
const tokens = await oauth2Client.code.getToken(request.url, {
codeVerifier,
@ -53,8 +55,10 @@ export const handler: Handlers = {
exp: getNumericDate(SESSION_DURATION),
}, JWT_SECRET);
console.log({ redirect });
const headers = new Headers({
location: "/",
location: redirect || "/",
});
setCookie(headers, {