Skip to content

Commit

Permalink
chore: simplified AuthProvider
Browse files Browse the repository at this point in the history
Simplified code and added e2e test capability
  • Loading branch information
paulushcgcj committed Nov 4, 2024
1 parent 9fb6c8c commit 4738e2b
Showing 1 changed file with 7 additions and 18 deletions.
25 changes: 7 additions & 18 deletions frontend/src/contexts/AuthProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ interface AuthContextType {
isLoading: boolean;
login: (provider: string) => void;
logout: () => void;
userDetails: () => Promise<FamLoginUser | undefined>

}

Expand All @@ -38,12 +37,13 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
useEffect(() => {
const checkUser = async () => {
try{
const session = await fetchAuthSession();
setIsLoggedIn(!!session.tokens);
const idToken = await loadUserToken();
console.log('idToken', idToken,env.NODE_ENV,env.VITE_USER_POOLS_WEB_CLIENT_ID);
setIsLoggedIn(!!idToken);
setIsLoading(false);
if(session.tokens){
setUser(parseToken(session.tokens.idToken));
setUserRoles(extractGroups(session.tokens?.idToken?.payload));
if(idToken){
setUser(parseToken(idToken));
setUserRoles(extractGroups(idToken?.payload));
}
}catch(error){
setIsLoggedIn(false);
Expand All @@ -54,16 +54,6 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
checkUser();
}, []);


const userDetails = async (): Promise<FamLoginUser | undefined> => {
const idToken = await loadUserToken();

if(idToken){
return Promise.resolve(parseToken(idToken));
}
return Promise.reject(new Error('No user details found'));
};

const login = async (provider: string) => {
const envProvider = (provider.localeCompare('idir') === 0)
?`${(appEnv).toLocaleUpperCase()}-IDIR`
Expand All @@ -86,8 +76,7 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
isLoggedIn,
isLoading,
login,
logout,
userDetails
logout
}), [user, userRoles, isLoggedIn, isLoading]);

return (
Expand Down

0 comments on commit 4738e2b

Please sign in to comment.