Skip to content

Commit f6f07e5

Browse files
Jeremias Santosclaude
authored andcommitted
fix(pav-5): add refreshUser to AuthContext for OAuth callback
AuthCallback now re-fetches /auth/me after OAuth redirect to pick up the newly created session, instead of relying on the initial mount fetch which runs before the session exists. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 9187d1e commit f6f07e5

2 files changed

Lines changed: 25 additions & 4 deletions

File tree

frontend/src/context/AuthContext.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ interface AuthContextData {
1818
isLoading: boolean;
1919
login: (credentials: { email: string; password: string }) => Promise<void>;
2020
logout: () => Promise<void>;
21+
refreshUser: () => Promise<void>;
2122
}
2223

2324
const AuthContext = createContext<AuthContextData>({} as AuthContextData);
@@ -50,8 +51,20 @@ export function AuthProvider({ children }: { children: ReactNode }) {
5051
setUser(null);
5152
}
5253

54+
async function refreshUser() {
55+
setIsLoading(true);
56+
try {
57+
const res = await api.get("/auth/me");
58+
setUser(res.data.user ?? { id: res.data.userId, email: "" });
59+
} catch {
60+
setUser(null);
61+
} finally {
62+
setIsLoading(false);
63+
}
64+
}
65+
5366
return (
54-
<AuthContext.Provider value={{ user, isLoading, login, logout }}>
67+
<AuthContext.Provider value={{ user, isLoading, login, logout, refreshUser }}>
5568
{children}
5669
</AuthContext.Provider>
5770
);

frontend/src/pages/auth/AuthCallback.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1-
import { useEffect } from "react";
1+
import { useEffect, useRef } from "react";
22
import { useNavigate } from "react-router-dom";
33
import { useAuth } from "@/context/AuthContext";
44
import Loading from "@/Loading";
55

66
export default function AuthCallback() {
7-
const { user, isLoading } = useAuth();
7+
const { user, isLoading, refreshUser } = useAuth();
88
const navigate = useNavigate();
9+
const hasRefreshed = useRef(false);
910

1011
useEffect(() => {
11-
if (isLoading) return;
12+
if (!hasRefreshed.current) {
13+
hasRefreshed.current = true;
14+
refreshUser();
15+
}
16+
}, [refreshUser]);
17+
18+
useEffect(() => {
19+
if (isLoading || !hasRefreshed.current) return;
1220

1321
if (user) {
1422
navigate("/app", { replace: true });

0 commit comments

Comments
 (0)