Skip to content

Commit 72d819b

Browse files
committed
frontend: try to fix login loop
1 parent 7d12853 commit 72d819b

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

frontend/src/context/AuthContext.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { createContext, useState, useEffect, ReactNode } from 'react';
1+
import React, { createContext, useState, useEffect, useCallback, ReactNode } from 'react';
22
import { authApi } from '../services/api';
33
import { UserInfo } from '../types';
44

@@ -26,7 +26,7 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
2626
const [user, setUser] = useState<UserInfo | null>(null);
2727
const [error, setError] = useState<string | null>(null);
2828

29-
const checkAuth = async () => {
29+
const checkAuth = useCallback(async () => {
3030
try {
3131
setIsLoading(true);
3232
setError(null);
@@ -40,13 +40,13 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
4040
} finally {
4141
setIsLoading(false);
4242
}
43-
};
43+
}, []);
4444

45-
const login = () => {
45+
const login = useCallback(() => {
4646
authApi.login();
47-
};
47+
}, []);
4848

49-
const logout = async () => {
49+
const logout = useCallback(async () => {
5050
try {
5151
await authApi.logout();
5252
setIsAuthenticated(false);
@@ -55,11 +55,11 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
5555
} catch (err) {
5656
setError('Failed to logout');
5757
}
58-
};
58+
}, []);
5959

6060
useEffect(() => {
6161
checkAuth();
62-
}, []);
62+
}, [checkAuth]);
6363

6464
const value: AuthContextType = {
6565
isAuthenticated,

frontend/src/pages/Login.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@ import { useNavigate } from 'react-router-dom';
33
import { useAuth } from '../hooks/useAuth';
44

55
export const Login: React.FC = () => {
6-
const { isAuthenticated, login } = useAuth();
6+
const { isAuthenticated, login, checkAuth } = useAuth();
77
const navigate = useNavigate();
88

9+
// Re-check auth status when landing on login page to clear stale state
10+
useEffect(() => {
11+
checkAuth();
12+
}, [checkAuth]);
13+
914
useEffect(() => {
1015
if (isAuthenticated) {
1116
navigate('/', { replace: true });

0 commit comments

Comments
 (0)