Skip to content

Commit

Permalink
fixed alexandrtovmach#101 : infinite calls in silent login
Browse files Browse the repository at this point in the history
  • Loading branch information
ankush-zipteams committed Oct 13, 2022
1 parent e3633e4 commit 488cd43
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ React component for a simple login with Microsoft services, based on [Official M
| debug | boolean | | Boolean flag to enable detailed logs of authorization process. |
| className | string | | Additional class name string. |
| children | ReactComponent | | Alternative way to provide custom button element as a children prop instead of [Official Microsoft brand design](https://docs.microsoft.com/en-us/azure/active-directory/develop/howto-add-branding-in-azure-ad-apps) |
| attemptSilentLogin | boolean | `false` | Set whether silent login should be attempted |

### Sign out

Expand Down
13 changes: 11 additions & 2 deletions src/MicrosoftLogin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ interface MicrosoftLoginProps {
* https://docs.microsoft.com/en-us/azure/active-directory/develop/msal-js-sso
*/
useLocalStorageCache?: boolean;

/**
* Set whether silent login should be attempted
*/
attemptSilentLogin?: boolean;
}

const MicrosoftLogin: React.FunctionComponent<MicrosoftLoginProps> = ({
Expand All @@ -103,6 +108,7 @@ const MicrosoftLogin: React.FunctionComponent<MicrosoftLoginProps> = ({
prompt,
debug,
useLocalStorageCache,
attemptSilentLogin = false,
}) => {
const msalInstance = getUserAgentApp({
clientId,
Expand Down Expand Up @@ -137,12 +143,15 @@ const MicrosoftLogin: React.FunctionComponent<MicrosoftLoginProps> = ({
// attempt silent login
// return msalInstance to user login handler on reload if token is present
useEffect(() => {
if (!attemptSilentLogin) return;

const clientToken = useLocalStorageCache
? localStorage.getItem("msal.idtoken")
: sessionStorage.getItem("msal.idtoken");

clientToken && getGraphAPITokenAndUser(forceRedirectStrategy || checkToIE());
}, [msalInstance]);
clientToken &&
getGraphAPITokenAndUser(forceRedirectStrategy || checkToIE());
}, []);

const login = () => {
log("Login STARTED");
Expand Down

0 comments on commit 488cd43

Please sign in to comment.