parent
137939d91c
commit
40f99f14f5
@ -0,0 +1,34 @@
|
||||
'use client';
|
||||
|
||||
import { useEffect } from 'react';
|
||||
|
||||
import { usePathname, useSearchParams } from 'next/navigation';
|
||||
import { parseCookies } from 'nookies';
|
||||
import { useShallow } from 'zustand/react/shallow';
|
||||
|
||||
import useUserStore from '@/store/user';
|
||||
|
||||
export default function RedirectCheck() {
|
||||
const pathName = usePathname();
|
||||
const searchParams = useSearchParams();
|
||||
const { token } = parseCookies();
|
||||
const { setShowLogin } = useUserStore(
|
||||
useShallow((state) => ({
|
||||
setShowLogin: state.setShowLogin,
|
||||
})),
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (typeof window === 'undefined') return; // 不在浏览器环境
|
||||
|
||||
const serach = new URLSearchParams(window.location.search);
|
||||
const redirect = serach.get('redirect');
|
||||
|
||||
if (redirect && !token) {
|
||||
setShowLogin(true);
|
||||
return;
|
||||
}
|
||||
}, [pathName, searchParams, typeof window !== 'undefined' ? window.location.search : '']);
|
||||
|
||||
return <></>;
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
import { setCookie } from 'nookies';
|
||||
|
||||
export function setAccessToken(token: string) {
|
||||
setCookie(null, 'token', token, {
|
||||
maxAge: 7 * 24 * 60 * 60,
|
||||
path: '/',
|
||||
});
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
import { useSearchParams } from 'next/navigation';
|
||||
|
||||
export const useLoginRedirect = () => {
|
||||
const searchParams = useSearchParams();
|
||||
const redirect = searchParams.get('redirect');
|
||||
if (!redirect) return '/';
|
||||
return redirect;
|
||||
};
|
Loading…
Reference in new issue