my-kaiqiuwang/src/hooks/useAutoLogin.ts
kyuuseiryuu de05ca2ecf feat: fallback to auto-login on token failure and improve redirect logic
- 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.
2026-03-16 12:51:39 +09:00

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;