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

@ -29,7 +29,13 @@ export const menus: Record<string, Menu> = {
title: "Login",
icon: "IconLogin",
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: () => {
return !getCookie("session_cookie");
@ -49,7 +55,13 @@ export const menus: Record<string, Menu> = {
title: "Logout",
icon: "IconLogout",
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: () => {
return !!getCookie("session_cookie");

View File

@ -4,7 +4,7 @@ import { IconGhost, IconLoader2, IconSearch } from "@components/icons.tsx";
import { useEventListener } from "@lib/hooks/useEventListener.ts";
import { SearchResult } from "@lib/types.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 Checkbox from "@components/Checkbox.tsx";
import { Rating } from "@components/Rating.tsx";
@ -13,6 +13,8 @@ import Image from "@components/Image.tsx";
import { Emoji } from "@components/Emoji.tsx";
export const RedirectSearchHandler = () => {
if (!getCookie("session_cookie")) return;
useEventListener("keydown", (e: KeyboardEvent) => {
if (e?.target?.nodeName == "INPUT") return;
if (