From 930607f0f2fe8cb2cc6fdbc42f0e9519671a5cfb Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Mon, 29 Jan 2024 18:33:11 +0800 Subject: [PATCH] :sparkles: Security guard --- view/src/layouts/RootLayout.tsx | 29 ++++++++++++++++++++--------- view/src/pages/auth/login.tsx | 18 ++++++++++++++++-- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/view/src/layouts/RootLayout.tsx b/view/src/layouts/RootLayout.tsx index 0ae85bf..563b686 100644 --- a/view/src/layouts/RootLayout.tsx +++ b/view/src/layouts/RootLayout.tsx @@ -1,25 +1,36 @@ import Navbar from "./shared/Navbar.tsx"; -import { readProfiles } from "../stores/userinfo.tsx"; -import { createSignal, Show } from "solid-js"; +import { readProfiles, useUserinfo } from "../stores/userinfo.tsx"; +import { createEffect, createSignal, Show } from "solid-js"; import { readWellKnown } from "../stores/wellKnown.tsx"; -import { BeforeLeaveEventArgs, useBeforeLeave, useNavigate } from "@solidjs/router"; +import { BeforeLeaveEventArgs, useBeforeLeave, useLocation, useNavigate } from "@solidjs/router"; export default function RootLayout(props: any) { const [ready, setReady] = createSignal(false); Promise.all([readWellKnown(), readProfiles()]).then(() => setReady(true)); - const navigate = useNavigate() + const navigate = useNavigate(); + const userinfo = useUserinfo(); - useBeforeLeave((e: BeforeLeaveEventArgs) => { - const whitelist = ["/auth/login", "/auth/register", "/users/me/confirm"] + const location = useLocation(); - if (!whitelist.includes(e.to.toString()) && !e.defaultPrevented) { - e.preventDefault(); - navigate(`/auth/login?redirect_uri=${e.to.toString()}`) + createEffect(() => { + if (ready()) { + keepGate(location.pathname); } }); + function keepGate(path: string, e?: BeforeLeaveEventArgs) { + const whitelist = ["/auth/login", "/auth/register", "/users/me/confirm"]; + + if (!userinfo?.isLoggedIn && !whitelist.includes(path)) { + if (!e?.defaultPrevented) e?.preventDefault(); + navigate(`/auth/login?redirect_uri=${path}`); + } + } + + useBeforeLeave((e: BeforeLeaveEventArgs) => keepGate(e.to.toString().split("?")[0], e)); + return ( diff --git a/view/src/pages/auth/login.tsx b/view/src/pages/auth/login.tsx index 0613367..753f6a8 100644 --- a/view/src/pages/auth/login.tsx +++ b/view/src/pages/auth/login.tsx @@ -15,7 +15,7 @@ export default function LoginPage() { const [challenge, setChallenge] = createSignal(); const [stage, setStage] = createSignal("starting"); - const[searchParams] = useSearchParams() + const [searchParams] = useSearchParams(); const navigate = useNavigate(); @@ -212,8 +212,22 @@ export default function LoginPage() { + +
+ +
+
+