-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(hook): rename isLogged to isAuthenticated + refactor useOidc…
…User hook (#743) BREAKING * feat(isauthenticated-separate): Adding separate hook to check if user is authenticated. The "useOidc()" hook is more noisy - it re-renders more because of the three state it has to manage. We could decouple these. * feat(isauthenticated-separate): Updating readme. * fix(isauthenticated-separate): Removing unnecessary hook and optimizing 'getOidcUser()'. This commit contains breaking changes: "isLogged" is switched to "isAuthenticated", per request. * fix(isauthenticated-separate): Removing unnecessary component in demo. * fix(isauthenticated-separate): Object property refactor and adding new user state.
- Loading branch information
1 parent
1e6e64d
commit cee3d5c
Showing
7 changed files
with
77 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<OidcUser>({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} | ||
} | ||
}, []); | ||
|
||
return {oidcUser: oidcUser.user, oidcUserLoadingState: oidcUser.status} | ||
} |