Skip to content

Commit

Permalink
feat: isLoading state (#577)
Browse files Browse the repository at this point in the history
Co-authored-by: Patrick Ammann <patrick.ammann@siemens.com>
  • Loading branch information
pamapa and Patrick Ammann committed Apr 10, 2021
1 parent 57536f2 commit b24e9ff
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/AuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export const AuthProvider: FC<AuthProviderProps> = ({
location = window.location,
...props
}) => {
const [isLoading, setIsLoading] = useState(true);
const [userData, setUserData] = useState<User | null>(null);

const userManager = initUserManager(props);
Expand Down Expand Up @@ -109,6 +110,7 @@ export const AuthProvider: FC<AuthProviderProps> = ({
if (hasCodeInUrl(location)) {
const user = await userManager.signinCallback();
setUserData(user);
setIsLoading(false);
onSignIn && onSignIn(user);
return;
}
Expand All @@ -117,8 +119,9 @@ export const AuthProvider: FC<AuthProviderProps> = ({
if ((!user || user.expired) && autoSignIn) {
onBeforeSignIn && onBeforeSignIn();
userManager.signinRedirect();
} else {
isMountedRef.current && setUserData(user);
} else if (isMountedRef.current) {
setUserData(user);
setIsLoading(false);
}
return;
};
Expand Down Expand Up @@ -156,6 +159,7 @@ export const AuthProvider: FC<AuthProviderProps> = ({
},
userManager,
userData,
isLoading,
}}
>
{children}
Expand Down
5 changes: 4 additions & 1 deletion src/AuthContextInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ export interface AuthProviderProps {
}

export interface AuthContextProps {

/**
* Alias for userManager.signInRedirect
*/
Expand All @@ -142,4 +141,8 @@ export interface AuthContextProps {
* See [User](https://github.com/IdentityModel/oidc-client-js/wiki#user) for more details.
*/
userData?: User | null;
/**
* Auth state: True until the library has been initialized.
*/
isLoading: boolean;
}

0 comments on commit b24e9ff

Please sign in to comment.