feat: remember path before login
This commit is contained in:
@@ -121,6 +121,7 @@ export const KEY_USERS_VERSION = "VOCECHAT_USERS_VERSION";
|
|||||||
export const KEY_AFTER_MID = "VOCECHAT_AFTER_MID";
|
export const KEY_AFTER_MID = "VOCECHAT_AFTER_MID";
|
||||||
export const KEY_PWA_INSTALLED = "VOCECHAT_PWA_INSTALLED";
|
export const KEY_PWA_INSTALLED = "VOCECHAT_PWA_INSTALLED";
|
||||||
export const KEY_LOCAL_MAGIC_TOKEN = "VOCECHAT_LOCAL_MAGIC_TOKEN";
|
export const KEY_LOCAL_MAGIC_TOKEN = "VOCECHAT_LOCAL_MAGIC_TOKEN";
|
||||||
|
export const KEY_LOCAL_TRY_PATH = "VOCECHAT_TRY_PATH";
|
||||||
export const Emojis = ["👍", "❤️", "😄", "👀", "👎", "🎉", "🙁", "🚀"];
|
export const Emojis = ["👍", "❤️", "😄", "👀", "👎", "🎉", "🙁", "🚀"];
|
||||||
|
|
||||||
export default BASE_URL;
|
export default BASE_URL;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { FC, ReactElement } from "react";
|
import { FC, ReactElement } from "react";
|
||||||
import { Navigate, useLocation, matchRoutes } from "react-router-dom";
|
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 { useGetInitializedQuery } from "../../app/services/auth";
|
||||||
import { useGetLoginConfigQuery } from "../../app/services/server";
|
import { useGetLoginConfigQuery } from "../../app/services/server";
|
||||||
import { useAppSelector } from "../../app/store";
|
import { useAppSelector } from "../../app/store";
|
||||||
@@ -31,7 +31,18 @@ const RequireAuth: FC<Props> = ({ children, redirectTo = "/login" }) => {
|
|||||||
// 登陆者是guest,并且不允许访问
|
// 登陆者是guest,并且不允许访问
|
||||||
if (token && guest && !allowGuest) return <Navigate to={"/"} replace />;
|
if (token && guest && !allowGuest) return <Navigate to={"/"} replace />;
|
||||||
// console.log("authhhhh", allowGuest, token, guest);
|
// console.log("authhhhh", allowGuest, token, guest);
|
||||||
return token ? children : <Navigate to={redirectTo} replace />;
|
if (!token) {
|
||||||
|
// 记录下当前的路径,登录后跳转回来
|
||||||
|
const ignorePath = `/setting/my_account`;
|
||||||
|
localStorage.setItem(KEY_LOCAL_TRY_PATH, ignorePath == location.pathname ? "/" : `${location.pathname}${location.search}`);
|
||||||
|
return <Navigate to={redirectTo} replace />;
|
||||||
|
}
|
||||||
|
const tryPath = localStorage.getItem(KEY_LOCAL_TRY_PATH);
|
||||||
|
if (tryPath) {
|
||||||
|
localStorage.removeItem(KEY_LOCAL_TRY_PATH);
|
||||||
|
return <Navigate to={tryPath} replace />;
|
||||||
|
}
|
||||||
|
return children;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default RequireAuth;
|
export default RequireAuth;
|
||||||
|
|||||||
Reference in New Issue
Block a user