feat: Introduce Nginx development configuration and enhance authentication state management by refining logout flag handling and session restoration conditions.
This commit is contained in:
@@ -15,19 +15,15 @@ export default function LoginPage() {
|
|||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
const [userType, setUserType] = useState<'USER' | 'ADMIN'>('USER');
|
const [userType, setUserType] = useState<'USER' | 'ADMIN'>('USER');
|
||||||
|
|
||||||
// Safety net: clear logout flags when login page loads.
|
|
||||||
// This resets both the localStorage flag and the module-level flag in api.ts
|
|
||||||
// so the API interceptor stops blocking requests.
|
|
||||||
if (typeof window !== 'undefined') {
|
|
||||||
localStorage.removeItem('isLoggingOut');
|
|
||||||
resetLogoutState();
|
|
||||||
}
|
|
||||||
|
|
||||||
const handleSubmit = async (e: React.FormEvent) => {
|
const handleSubmit = async (e: React.FormEvent) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
setError('');
|
setError('');
|
||||||
|
|
||||||
|
// Clear logout flags now that user is actively logging in
|
||||||
|
localStorage.removeItem('isLoggingOut');
|
||||||
|
resetLogoutState();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// First, validate credentials with backend to get proper error messages
|
// First, validate credentials with backend to get proper error messages
|
||||||
const validateRes = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/auth/login`, {
|
const validateRes = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/auth/login`, {
|
||||||
|
|||||||
@@ -9,11 +9,13 @@ function TokenSync() {
|
|||||||
const hasInitialized = useRef(false);
|
const hasInitialized = useRef(false);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// Never restore tokens if a logout is in progress or we're on the logout page
|
// Never restore tokens if a logout is in progress or we're on auth/logout pages
|
||||||
const loggingOut = localStorage.getItem("isLoggingOut") === "true";
|
const loggingOut = localStorage.getItem("isLoggingOut") === "true";
|
||||||
const onLogoutPage = window.location.pathname === "/logout";
|
const path = window.location.pathname;
|
||||||
|
const onAuthPage = path === "/login" || path === "/signup" || path === "/logout"
|
||||||
|
|| path.startsWith("/forgot-password") || path.startsWith("/reset-password");
|
||||||
|
|
||||||
if (status === "authenticated" && session?.user && !loggingOut && !onLogoutPage) {
|
if (status === "authenticated" && session?.user && !loggingOut && !onAuthPage) {
|
||||||
const user = session.user as { accessToken?: string; refreshToken?: string };
|
const user = session.user as { accessToken?: string; refreshToken?: string };
|
||||||
|
|
||||||
// Only sync tokens from NextAuth to localStorage if:
|
// Only sync tokens from NextAuth to localStorage if:
|
||||||
|
|||||||
@@ -76,7 +76,8 @@ const api: AxiosInstance = axios.create({
|
|||||||
api.interceptors.request.use(
|
api.interceptors.request.use(
|
||||||
(config: InternalAxiosRequestConfig) => {
|
(config: InternalAxiosRequestConfig) => {
|
||||||
// Skip API calls during logout to prevent refresh loops
|
// Skip API calls during logout to prevent refresh loops
|
||||||
if (isLoggingOut) {
|
// Check both module flag and localStorage flag (logout page sets localStorage directly)
|
||||||
|
if (isLoggingOut || (typeof window !== 'undefined' && localStorage.getItem('isLoggingOut') === 'true')) {
|
||||||
return Promise.reject(new axios.Cancel('Logging out'));
|
return Promise.reject(new axios.Cancel('Logging out'));
|
||||||
}
|
}
|
||||||
// Add auth token if available
|
// Add auth token if available
|
||||||
|
|||||||
Reference in New Issue
Block a user