@@ -73,4 +78,4 @@ const DisplayIdToken =() => {
}
-export const SecureProfile = () =>
;
\ No newline at end of file
+export const SecureProfile = () =>
;
diff --git a/packages/context/src/oidc/ReactOidc.tsx b/packages/context/src/oidc/ReactOidc.tsx
index 4b42ced32..f972d3419 100644
--- a/packages/context/src/oidc/ReactOidc.tsx
+++ b/packages/context/src/oidc/ReactOidc.tsx
@@ -4,21 +4,21 @@ import Oidc from "./vanilla/oidc";
const defaultConfigurationName = "default";
export const useOidc =(configurationName=defaultConfigurationName) =>{
const getOidc = Oidc.get;
-
+
const login = (callbackPath=undefined) => {
return getOidc(configurationName).loginAsync(callbackPath);
};
const logout = () => {
return getOidc(configurationName).logoutAsync();
};
-
- let isLogged = false;
+
+ let isAuthenticated = false;
const oidc = getOidc(configurationName);
if(oidc){
- isLogged = getOidc(configurationName).tokens != null;
+ isAuthenticated = getOidc(configurationName).tokens != null;
}
-
- return {login, logout, isLogged };
+
+ return { login, logout, isAuthenticated };
}
const accessTokenInitialState = {accessToken:null, accessTokenPayload:null};
@@ -37,7 +37,7 @@ export const useOidcAccessToken =(configurationName=defaultConfigurationName) =>
const getOidc = Oidc.get;
const [state, setAccessToken] = useState
(initTokens(configurationName));
const [subscriptionId, setSubscriptionId] = useState(null);
-
+
useEffect(() => {
let isMounted = true;
const oidc = getOidc(configurationName);
@@ -60,7 +60,7 @@ export const useOidcAccessToken =(configurationName=defaultConfigurationName) =>
if(isMounted){
setSubscriptionId(newSubscriptionId);
}
- return () => {
+ return () => {
isMounted = false;
oidc.removeEventSubscription(subscriptionId);
};
@@ -84,14 +84,14 @@ export const useOidcIdToken =(configurationName= defaultConfigurationName) =>{
const getOidc = Oidc.get;
const [state, setIDToken] = useState(idTokenInitialState);
const [subscriptionId, setSubscriptionId] = useState(initIdToken(configurationName));
-
+
useEffect(() => {
let isMounted = true;
const oidc = getOidc(configurationName);
if(oidc.tokens) {
const tokens = oidc.tokens;
setIDToken({idToken: tokens.idToken, idTokenPayload:tokens.idTokenPayload});
- }
+ }
if(subscriptionId){
oidc.removeEventSubscription(subscriptionId);
}
@@ -113,4 +113,4 @@ export const useOidcIdToken =(configurationName= defaultConfigurationName) =>{
};
}, [configurationName]);
return state;
-}
\ No newline at end of file
+}
diff --git a/packages/context/src/oidc/User.ts b/packages/context/src/oidc/User.ts
index e3c022dc4..e1de5466d 100644
--- a/packages/context/src/oidc/User.ts
+++ b/packages/context/src/oidc/User.ts
@@ -1,31 +1,38 @@
import { useEffect, useState} from "react";
import Oidc from "./vanilla/oidc";
-export const useOidcUser =(configurationName="default") => {
- const [oidcUser, setOidcUser] = useState(null);
- const [isOidcUserLoading, setIsOidcUserLoading] = useState(false);
- const getOidc = Oidc.get;
+export enum UserStatus {
+ Unauthenticated= 'Unauthenticated',
+ Loading = 'Loading user',
+ Loaded = 'User loaded',
+ LoadingError = 'Error loading user'
+}
+
+type OidcUser = {
+ user: any,
+ status: UserStatus
+}
+
+export const useOidcUser = (configurationName="default") => {
+ const [oidcUser, setOidcUser] = useState({user: null, status: UserStatus.Unauthenticated});
+
+ const oidc = Oidc.get(configurationName);
useEffect(() => {
let isMounted = true;
- const oidc = getOidc(configurationName);
+
if(oidc && oidc.tokens) {
- setIsOidcUserLoading(true);
- getOidc().userInfoAsync()
+ setOidcUser({...oidcUser, status: UserStatus.Loading});
+ oidc.userInfoAsync()
.then((info) => {
if (isMounted) {
- setOidcUser(info);
- setIsOidcUserLoading(false);
+ setOidcUser({user: info, status: UserStatus.Loaded});
}
})
+ .catch(() => setOidcUser({...oidcUser, status: UserStatus.LoadingError}));
}
+
return () => { isMounted = false };
- }, [])
-
- let isLogged = false;
- const oidc = getOidc(configurationName);
- if(oidc){
- isLogged = oidc.tokens != null;
- }
-
- return {oidcUser, isOidcUserLoading, isLogged: isLogged}
-}
\ No newline at end of file
+ }, []);
+
+ return {oidcUser: oidcUser.user, oidcUserLoadingState: oidcUser.status}
+}