feat(auth): R4 auth reliability — fix PKCE race condition + RequireAuth loading skeleton
This commit is contained in:
@@ -18,6 +18,7 @@ export interface AuthContextType {
|
||||
signOut: () => Promise<void>;
|
||||
clearError: () => void;
|
||||
bootstrapData: BootstrapResponse | null;
|
||||
isAuthResolving: boolean;
|
||||
}
|
||||
|
||||
export const AuthContext = createContext<AuthContextType>({
|
||||
@@ -29,7 +30,8 @@ export const AuthContext = createContext<AuthContextType>({
|
||||
signIn: async () => {},
|
||||
signOut: async () => {},
|
||||
clearError: () => {},
|
||||
bootstrapData: null
|
||||
bootstrapData: null,
|
||||
isAuthResolving: false
|
||||
});
|
||||
|
||||
export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
@@ -40,6 +42,7 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<Error | null>(null);
|
||||
const [bootstrapData, setBootstrapData] = useState<BootstrapResponse | null>(null);
|
||||
const [isAuthResolving, setIsAuthResolving] = useState(false);
|
||||
|
||||
|
||||
const persistSession = useCallback((session: Session | null) => {
|
||||
@@ -92,6 +95,7 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
|
||||
if (event === 'SIGNED_IN') {
|
||||
persistSession(session ?? null);
|
||||
setIsAuthResolving(!!session?.user);
|
||||
if (session?.user) {
|
||||
try {
|
||||
const { user: resolvedUser, role } = await buildUserFromSupabase(session.user);
|
||||
@@ -112,11 +116,13 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
setAccessToken(null);
|
||||
}
|
||||
setLoading(false);
|
||||
setIsAuthResolving(false);
|
||||
return;
|
||||
}
|
||||
|
||||
if (event === 'INITIAL_SESSION' || event === 'TOKEN_REFRESHED') {
|
||||
persistSession(session ?? null);
|
||||
setIsAuthResolving(!!session?.user);
|
||||
if (session?.user) {
|
||||
try {
|
||||
const { user: resolvedUser, role } = await buildUserFromSupabase(session.user);
|
||||
@@ -137,11 +143,13 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
setAccessToken(null);
|
||||
}
|
||||
setLoading(false);
|
||||
setIsAuthResolving(false);
|
||||
return;
|
||||
}
|
||||
|
||||
if (event === 'SIGNED_OUT') {
|
||||
persistSession(null);
|
||||
setIsAuthResolving(false);
|
||||
setUser(null);
|
||||
setUserRole(null);
|
||||
setAccessToken(null);
|
||||
@@ -214,7 +222,8 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
signIn,
|
||||
signOut,
|
||||
clearError,
|
||||
bootstrapData
|
||||
bootstrapData,
|
||||
isAuthResolving
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
|
||||
Reference in New Issue
Block a user