From 0e77e90664c37def688b24e5bf5897440bfcde33 Mon Sep 17 00:00:00 2001 From: Tristan Yang Date: Tue, 18 Apr 2023 21:41:39 +0800 Subject: [PATCH] feat: remember path before login --- src/app/config.ts | 1 + src/common/component/RequireAuth.tsx | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/app/config.ts b/src/app/config.ts index b619216c..47f22513 100644 --- a/src/app/config.ts +++ b/src/app/config.ts @@ -121,6 +121,7 @@ export const KEY_USERS_VERSION = "VOCECHAT_USERS_VERSION"; export const KEY_AFTER_MID = "VOCECHAT_AFTER_MID"; export const KEY_PWA_INSTALLED = "VOCECHAT_PWA_INSTALLED"; export const KEY_LOCAL_MAGIC_TOKEN = "VOCECHAT_LOCAL_MAGIC_TOKEN"; +export const KEY_LOCAL_TRY_PATH = "VOCECHAT_TRY_PATH"; export const Emojis = ["👍", "❤️", "😄", "👀", "👎", "🎉", "🙁", "🚀"]; export default BASE_URL; diff --git a/src/common/component/RequireAuth.tsx b/src/common/component/RequireAuth.tsx index d35ffa6e..b5299c16 100644 --- a/src/common/component/RequireAuth.tsx +++ b/src/common/component/RequireAuth.tsx @@ -1,6 +1,6 @@ import { FC, ReactElement } from "react"; import { Navigate, useLocation, matchRoutes } from "react-router-dom"; -import { GuestRoutes } from "../../app/config"; +import { GuestRoutes, KEY_LOCAL_TRY_PATH } from "../../app/config"; import { useGetInitializedQuery } from "../../app/services/auth"; import { useGetLoginConfigQuery } from "../../app/services/server"; import { useAppSelector } from "../../app/store"; @@ -31,7 +31,18 @@ const RequireAuth: FC = ({ children, redirectTo = "/login" }) => { // 登陆者是guest,并且不允许访问 if (token && guest && !allowGuest) return ; // console.log("authhhhh", allowGuest, token, guest); - return token ? children : ; + if (!token) { + // 记录下当前的路径,登录后跳转回来 + const ignorePath = `/setting/my_account`; + localStorage.setItem(KEY_LOCAL_TRY_PATH, ignorePath == location.pathname ? "/" : `${location.pathname}${location.search}`); + return ; + } + const tryPath = localStorage.getItem(KEY_LOCAL_TRY_PATH); + if (tryPath) { + localStorage.removeItem(KEY_LOCAL_TRY_PATH); + return ; + } + return children; }; export default RequireAuth;