complete project using prettier!
This commit is contained in:
@@ -32,14 +32,15 @@ export const AuthProvider: React.FC<{ children: ReactNode }> = ({ children }) =>
|
||||
logger.info('[AuthProvider-Effect] Found auth token. Validating...');
|
||||
try {
|
||||
const fetchedProfile = await checkTokenApi();
|
||||
|
||||
|
||||
if (isMounted && fetchedProfile) {
|
||||
logger.info('[AuthProvider-Effect] Profile received, setting state to AUTHENTICATED.');
|
||||
setUserProfile(fetchedProfile);
|
||||
setAuthStatus('AUTHENTICATED');
|
||||
} else if (isMounted) {
|
||||
logger.warn('[AuthProvider-Effect] Token was present but validation returned no profile. Signing out.');
|
||||
logger.warn(
|
||||
'[AuthProvider-Effect] Token was present but validation returned no profile. Signing out.',
|
||||
);
|
||||
localStorage.removeItem('authToken');
|
||||
setUserProfile(null);
|
||||
setAuthStatus('SIGNED_OUT');
|
||||
@@ -61,7 +62,9 @@ export const AuthProvider: React.FC<{ children: ReactNode }> = ({ children }) =>
|
||||
}
|
||||
|
||||
if (isMounted) {
|
||||
logger.info('[AuthProvider-Effect] Initial auth check finished. Setting isLoading to false.');
|
||||
logger.info(
|
||||
'[AuthProvider-Effect] Initial auth check finished. Setting isLoading to false.',
|
||||
);
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
@@ -69,7 +72,7 @@ export const AuthProvider: React.FC<{ children: ReactNode }> = ({ children }) =>
|
||||
checkAuthToken();
|
||||
|
||||
return () => {
|
||||
logger.info('[AuthProvider-Effect] Component unmounting, cleaning up.');
|
||||
logger.info('[AuthProvider-Effect] Component unmounting, cleaning up.');
|
||||
isMounted = false;
|
||||
};
|
||||
}, [checkTokenApi]);
|
||||
@@ -81,53 +84,63 @@ export const AuthProvider: React.FC<{ children: ReactNode }> = ({ children }) =>
|
||||
setAuthStatus('SIGNED_OUT');
|
||||
}, []);
|
||||
|
||||
const login = useCallback(async (token: string, profileData?: UserProfile) => {
|
||||
logger.info(`[AuthProvider-Login] Attempting login.`);
|
||||
localStorage.setItem('authToken', token);
|
||||
const login = useCallback(
|
||||
async (token: string, profileData?: UserProfile) => {
|
||||
logger.info(`[AuthProvider-Login] Attempting login.`);
|
||||
localStorage.setItem('authToken', token);
|
||||
|
||||
if (profileData) {
|
||||
// If profile is provided (e.g., from credential login), use it directly.
|
||||
logger.info('[AuthProvider-Login] Profile data received directly.');
|
||||
setUserProfile(profileData);
|
||||
setAuthStatus('AUTHENTICATED');
|
||||
logger.info('[AuthProvider-Login] Login successful. State set to AUTHENTICATED.', { user: profileData.user });
|
||||
} else {
|
||||
// If no profile is provided (e.g., from OAuth or token refresh), fetch it.
|
||||
logger.info('[AuthProvider-Login] Auth token set in storage. Fetching profile...');
|
||||
try {
|
||||
const fetchedProfile = await fetchProfileApi();
|
||||
if (!fetchedProfile) {
|
||||
throw new Error('Received null or undefined profile from API.');
|
||||
}
|
||||
setUserProfile(fetchedProfile);
|
||||
if (profileData) {
|
||||
// If profile is provided (e.g., from credential login), use it directly.
|
||||
logger.info('[AuthProvider-Login] Profile data received directly.');
|
||||
setUserProfile(profileData);
|
||||
setAuthStatus('AUTHENTICATED');
|
||||
logger.info('[AuthProvider-Login] Profile fetch successful. State set to AUTHENTICATED.');
|
||||
} catch (e) {
|
||||
const errorMessage = e instanceof Error ? e.message : String(e);
|
||||
logger.error('Failed to fetch user data after login. Rolling back auth state.', { error: errorMessage });
|
||||
logout(); // Log the user out to prevent an inconsistent state.
|
||||
// Re-throw the error so the calling component can handle it (e.g., show a notification)
|
||||
throw new Error(`Login succeeded, but failed to fetch your data: ${errorMessage}`);
|
||||
logger.info('[AuthProvider-Login] Login successful. State set to AUTHENTICATED.', {
|
||||
user: profileData.user,
|
||||
});
|
||||
} else {
|
||||
// If no profile is provided (e.g., from OAuth or token refresh), fetch it.
|
||||
logger.info('[AuthProvider-Login] Auth token set in storage. Fetching profile...');
|
||||
try {
|
||||
const fetchedProfile = await fetchProfileApi();
|
||||
if (!fetchedProfile) {
|
||||
throw new Error('Received null or undefined profile from API.');
|
||||
}
|
||||
setUserProfile(fetchedProfile);
|
||||
setAuthStatus('AUTHENTICATED');
|
||||
logger.info('[AuthProvider-Login] Profile fetch successful. State set to AUTHENTICATED.');
|
||||
} catch (e) {
|
||||
const errorMessage = e instanceof Error ? e.message : String(e);
|
||||
logger.error('Failed to fetch user data after login. Rolling back auth state.', {
|
||||
error: errorMessage,
|
||||
});
|
||||
logout(); // Log the user out to prevent an inconsistent state.
|
||||
// Re-throw the error so the calling component can handle it (e.g., show a notification)
|
||||
throw new Error(`Login succeeded, but failed to fetch your data: ${errorMessage}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}, [fetchProfileApi, logout]);
|
||||
},
|
||||
[fetchProfileApi, logout],
|
||||
);
|
||||
|
||||
const updateProfile = useCallback((updatedProfileData: Partial<UserProfile>) => {
|
||||
logger.info('[AuthProvider-UpdateProfile] Updating profile state.', { updatedProfileData });
|
||||
setUserProfile(prevProfile => {
|
||||
setUserProfile((prevProfile) => {
|
||||
if (!prevProfile) return null;
|
||||
return { ...prevProfile, ...updatedProfileData };
|
||||
});
|
||||
}, []);
|
||||
|
||||
const value = useMemo(() => ({
|
||||
userProfile,
|
||||
authStatus,
|
||||
isLoading,
|
||||
login,
|
||||
logout,
|
||||
updateProfile
|
||||
}), [userProfile, authStatus, isLoading, login, logout, updateProfile]);
|
||||
const value = useMemo(
|
||||
() => ({
|
||||
userProfile,
|
||||
authStatus,
|
||||
isLoading,
|
||||
login,
|
||||
logout,
|
||||
updateProfile,
|
||||
}),
|
||||
[userProfile, authStatus, isLoading, login, logout, updateProfile],
|
||||
);
|
||||
|
||||
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user