feat: redirect to same url on login/logout
This commit is contained in:
parent
f066b4e5e4
commit
7a5f43d799
@ -29,7 +29,13 @@ export const menus: Record<string, Menu> = {
|
|||||||
title: "Login",
|
title: "Login",
|
||||||
icon: "IconLogin",
|
icon: "IconLogin",
|
||||||
cb: () => {
|
cb: () => {
|
||||||
window.location.pathname = "/api/auth/login";
|
const url = new URL(window.location.href);
|
||||||
|
url.pathname = "/api/auth/login";
|
||||||
|
url.searchParams.set(
|
||||||
|
"redirect",
|
||||||
|
encodeURIComponent(window.location.pathname),
|
||||||
|
);
|
||||||
|
window.location.href = url.href;
|
||||||
},
|
},
|
||||||
visible: () => {
|
visible: () => {
|
||||||
return !getCookie("session_cookie");
|
return !getCookie("session_cookie");
|
||||||
@ -49,7 +55,13 @@ export const menus: Record<string, Menu> = {
|
|||||||
title: "Logout",
|
title: "Logout",
|
||||||
icon: "IconLogout",
|
icon: "IconLogout",
|
||||||
cb: () => {
|
cb: () => {
|
||||||
window.location.pathname = "/api/auth/logout";
|
const url = new URL(window.location.href);
|
||||||
|
url.pathname = "/api/auth/logout";
|
||||||
|
url.searchParams.set(
|
||||||
|
"redirect",
|
||||||
|
encodeURIComponent(window.location.pathname),
|
||||||
|
);
|
||||||
|
window.location.href = url.href;
|
||||||
},
|
},
|
||||||
visible: () => {
|
visible: () => {
|
||||||
return !!getCookie("session_cookie");
|
return !!getCookie("session_cookie");
|
||||||
|
@ -4,7 +4,7 @@ import { IconGhost, IconLoader2, IconSearch } from "@components/icons.tsx";
|
|||||||
import { useEventListener } from "@lib/hooks/useEventListener.ts";
|
import { useEventListener } from "@lib/hooks/useEventListener.ts";
|
||||||
import { SearchResult } from "@lib/types.ts";
|
import { SearchResult } from "@lib/types.ts";
|
||||||
import { resources } from "@lib/resources.ts";
|
import { resources } from "@lib/resources.ts";
|
||||||
import { isLocalImage } from "@lib/string.ts";
|
import { getCookie, isLocalImage } from "@lib/string.ts";
|
||||||
import { IS_BROWSER } from "$fresh/runtime.ts";
|
import { IS_BROWSER } from "$fresh/runtime.ts";
|
||||||
import Checkbox from "@components/Checkbox.tsx";
|
import Checkbox from "@components/Checkbox.tsx";
|
||||||
import { Rating } from "@components/Rating.tsx";
|
import { Rating } from "@components/Rating.tsx";
|
||||||
@ -13,6 +13,8 @@ import Image from "@components/Image.tsx";
|
|||||||
import { Emoji } from "@components/Emoji.tsx";
|
import { Emoji } from "@components/Emoji.tsx";
|
||||||
|
|
||||||
export const RedirectSearchHandler = () => {
|
export const RedirectSearchHandler = () => {
|
||||||
|
if (!getCookie("session_cookie")) return;
|
||||||
|
|
||||||
useEventListener("keydown", (e: KeyboardEvent) => {
|
useEventListener("keydown", (e: KeyboardEvent) => {
|
||||||
if (e?.target?.nodeName == "INPUT") return;
|
if (e?.target?.nodeName == "INPUT") return;
|
||||||
if (
|
if (
|
||||||
|
@ -1,10 +1,7 @@
|
|||||||
import { Handlers } from "$fresh/server.ts";
|
import { Handlers } from "$fresh/server.ts";
|
||||||
import { create, getNumericDate } from "https://deno.land/x/djwt@v2.2/mod.ts";
|
import { create, getNumericDate } from "https://deno.land/x/djwt@v2.2/mod.ts";
|
||||||
import { oauth2Client } from "@lib/auth.ts";
|
import { oauth2Client } from "@lib/auth.ts";
|
||||||
import {
|
import { getCookies, setCookie } from "$std/http/cookie.ts";
|
||||||
getCookies,
|
|
||||||
setCookie,
|
|
||||||
} from "https://deno.land/std@0.197.0/http/cookie.ts";
|
|
||||||
import { codeChallengeMap } from "./login.ts";
|
import { codeChallengeMap } from "./login.ts";
|
||||||
import { GITEA_SERVER, JWT_SECRET, SESSION_DURATION } from "@lib/env.ts";
|
import { GITEA_SERVER, JWT_SECRET, SESSION_DURATION } from "@lib/env.ts";
|
||||||
import { userDB } from "@lib/db.ts";
|
import { userDB } from "@lib/db.ts";
|
||||||
@ -20,7 +17,12 @@ export const handler: Handlers = {
|
|||||||
// Exchange the authorization code for an access token
|
// Exchange the authorization code for an access token
|
||||||
const cookies = getCookies(request.headers);
|
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, {
|
const tokens = await oauth2Client.code.getToken(request.url, {
|
||||||
codeVerifier,
|
codeVerifier,
|
||||||
@ -53,8 +55,10 @@ export const handler: Handlers = {
|
|||||||
exp: getNumericDate(SESSION_DURATION),
|
exp: getNumericDate(SESSION_DURATION),
|
||||||
}, JWT_SECRET);
|
}, JWT_SECRET);
|
||||||
|
|
||||||
|
console.log({ redirect });
|
||||||
|
|
||||||
const headers = new Headers({
|
const headers = new Headers({
|
||||||
location: "/",
|
location: redirect || "/",
|
||||||
});
|
});
|
||||||
|
|
||||||
setCookie(headers, {
|
setCookie(headers, {
|
||||||
|
@ -1,18 +1,25 @@
|
|||||||
import { Handlers } from "$fresh/server.ts";
|
import { Handlers } from "$fresh/server.ts";
|
||||||
import { oauth2Client } from "@lib/auth.ts";
|
import { oauth2Client } from "@lib/auth.ts";
|
||||||
import { sha256 } from "@lib/string.ts";
|
import { setCookie } from "$std/http/cookie.ts";
|
||||||
import { setCookie } from "https://deno.land/std@0.197.0/http/cookie.ts";
|
|
||||||
|
|
||||||
export const codeChallengeMap = new Map();
|
export const codeChallengeMap = new Map<
|
||||||
|
string,
|
||||||
|
{ codeVerifier: string; redirect?: string }
|
||||||
|
>();
|
||||||
|
|
||||||
export const handler: Handlers = {
|
export const handler: Handlers = {
|
||||||
async GET() {
|
async GET(req) {
|
||||||
|
const url = new URL(req.url);
|
||||||
|
|
||||||
const { codeVerifier, uri } = await oauth2Client.code.getAuthorizationUri();
|
const { codeVerifier, uri } = await oauth2Client.code.getAuthorizationUri();
|
||||||
|
|
||||||
const codeChallenge = uri.searchParams.get("code_challenge");
|
const codeChallenge = uri.searchParams.get("code_challenge");
|
||||||
if (!codeChallenge) return new Response();
|
if (!codeChallenge) return new Response();
|
||||||
|
|
||||||
codeChallengeMap.set(codeChallenge, codeVerifier);
|
codeChallengeMap.set(codeChallenge, {
|
||||||
|
codeVerifier,
|
||||||
|
redirect: decodeURIComponent(url.searchParams.get("redirect") || ""),
|
||||||
|
});
|
||||||
|
|
||||||
const headers = new Headers();
|
const headers = new Headers();
|
||||||
setCookie(headers, {
|
setCookie(headers, {
|
||||||
|
@ -2,9 +2,14 @@ import { deleteCookie } from "https://deno.land/std@0.197.0/http/cookie.ts";
|
|||||||
import { Handlers } from "$fresh/server.ts";
|
import { Handlers } from "$fresh/server.ts";
|
||||||
|
|
||||||
export const handler: Handlers = {
|
export const handler: Handlers = {
|
||||||
GET() {
|
GET(req) {
|
||||||
|
const url = new URL(req.url);
|
||||||
|
|
||||||
|
const redirect = decodeURIComponent(url.searchParams.get("redirect") || "");
|
||||||
|
|
||||||
const headers = new Headers();
|
const headers = new Headers();
|
||||||
headers.append("location", "/");
|
headers.append("location", redirect || "/");
|
||||||
|
|
||||||
deleteCookie(headers, "session_cookie", {
|
deleteCookie(headers, "session_cookie", {
|
||||||
path: "/",
|
path: "/",
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user