function setTokenRedirectToLocalStorage(req) { auth.acquireTokenSilent(req) .then(function(response) { window.localStorage.setItem("token", response.accessToken); }) .catch(function(error) { // }); }
myMSALObj.loginPopup(["user.read"]).then(idToken => { const user = myMSALObj.getUser(); this.setState({ authenticated: true, user: user, wait: false }); navigate("/dashboard"); // if you want to direct the user to a page after login });
// Login popup flow provider // Returns IdToken const signIn = () => { return userAgentApplication.loginPopup(applicationConfig.graphScopes) }
const signOut = () => { userAgentApplication.logout() }
export function getAccount() { return auth.getAccount(); }
// This function can be removed if you do not need to support IE const acquireTokenRedirectAndCallMSGraph = () => { //Call acquireTokenSilent (iframe) to obtain a token for Microsoft Graph userAgentApplication.acquireTokenSilent(applicationConfig.graphScopes).then(function (accessToken) { callMSGraph(applicationConfig.graphEndpoint, accessToken, graphAPICallback); }, function (error) { console.log(error) //Call acquireTokenRedirect in case of acquireToken Failure if (error.indexOf("consent_required") !== -1 || error.indexOf("interaction_required") !== -1 || error.indexOf("login_required") !== -1) { userAgentApplication.acquireTokenRedirect(applicationConfig.graphScopes) } }); }
const login = () => auth.loginRedirect(GRAPH_REQUESTS.LOGIN)
export function registerRedirectionCallback(errorCallback) { return auth.handleRedirectCallback((error, response) => { if (getAccount()) { setTokenRedirectToLocalStorage(GRAPH_REQUESTS.LOGIN); } else if (response.tokenType === "access_token") { // } else { // } if (error && errorCallback) { const errorMessage = error.errorMessage ? error.errorMessage : "Unable to acquire access token."; errorCallback(error); } }); }
const getAccessToken = async () => { // Get the access token silently // If the cache contains a non-expired token, this function // will just return the cached token. Otherwise, it will // make a request to the Azure OAuth endpoint to get a token try { const token = window.localStorage.getItem("token"); if (!token) { const response = await auth.acquireTokenSilent(GRAPH_REQUESTS.LOGIN); return response.accessToken; } return token; } catch (error) { if (requiresInteraction(error.errorCode)) { return auth.acquireTokenRedirect(GRAPH_REQUESTS.LOGIN); } else { console.error("Non-interactive error:", error.errorCode); } } }
// Sample API call const acquireTokenPopupAndCallMSGraph = () => { //Call acquireTokenSilent (iframe) to obtain a token for Microsoft Graph userAgentApplication.acquireTokenSilent(applicationConfig.graphScopes).then(function (accessToken) { callMSGraph(applicationConfig.graphEndpoint, accessToken, graphAPICallback); }, function (error) { console.log(error); // Call acquireTokenPopup (popup window) in case of acquireTokenSilent failure due to consent or interaction required ONLY if (error.indexOf("consent_required") !== -1 || error.indexOf("interaction_required") !== -1 || error.indexOf("login_required") !== -1) { userAgentApplication.acquireTokenPopup(applicationConfig.graphScopes).then(function (accessToken) { callMSGraph(applicationConfig.graphEndpoint, accessToken, graphAPICallback); }, function (error) { console.log(error); }); } }); }
const logout = () => auth.logout()