- In `useAuthHeaders`, import `useAutoLogin` and trigger login if `getAccessToken` returns a falsy value. This handles edge cases where the token is expired or missing during an API call. - In `useAutoLogin`, set the default redirect URL to `window.location.pathname` to ensure users return to the page they were on before logging in.
16 lines
401 B
TypeScript
16 lines
401 B
TypeScript
import { useCallback } from "react"
|
|
import { useNavigate } from "react-router";
|
|
|
|
const useAutoLogin = () => {
|
|
const navigate = useNavigate();
|
|
const login = useCallback((redirect = window.location.pathname) => {
|
|
if (redirect) {
|
|
sessionStorage.setItem('redirect', redirect);
|
|
}
|
|
navigate('/user-center?autoSignIn=true');
|
|
}, []);
|
|
return { login };
|
|
}
|
|
|
|
export default useAutoLogin;
|