Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/LoginForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ const LoginForm: React.FC = () => {
}

setSuccess(true);
login(data.accessToken, data.user);
await login(data.accessToken, data.user);
setTimeout(() => {
if (deviceCode) {
// Check onboarding status before redirecting
Expand Down
4 changes: 2 additions & 2 deletions src/components/OAuthCallback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ const OAuthCallback: React.FC = () => {
});

if (result.success && result.data) {
// Update auth state
login(result.data.accessToken, result.data.user);
// Update auth state (hydrates from /api/auth/me for full user fields)
await login(result.data.accessToken, result.data.user);

Comment on lines 68 to 71
Copy link

Copilot AI Mar 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handleOAuthCallback is typed to return data?: any (see src/utils/oauth.ts), so result.data can exist without accessToken and user. Calling login(result.data.accessToken, result.data.user) in that case can store the string "undefined" in localStorage and set an invalid auth state. Add explicit runtime validation for accessToken and user (and treat missing fields as an error) before calling login.

Copilot uses AI. Check for mistakes.
setStatus('success');

Expand Down
17 changes: 15 additions & 2 deletions src/hooks/useAuth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ interface AuthContextType {
isAuthenticated: boolean;
loading: boolean;
loggingOut: boolean;
login: (token: string, user: User) => void;
login: (token: string, user: User) => Promise<void>;
logout: () => Promise<void>;
updateUser: (user: User) => void;
completeOnboarding: (
Expand Down Expand Up @@ -134,8 +134,21 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({
checkAuth();
}, []);

const login = useCallback((token: string, userData: User) => {
const login = useCallback(async (token: string, userData: User) => {
localStorage.setItem('accessToken', token);
const apiBaseUrl = getApiBaseUrl();
try {
const response = await fetch(`${apiBaseUrl}/api/auth/me`, {
headers: { Authorization: `Bearer ${token}` },
});
if (response.ok) {
const data = await response.json();
setUser(data.user);
return;
}
} catch {
/* fall back to login / OAuth payload */
}
setUser(userData);
}, []);

Expand Down
Loading