代码拉取完成,页面将自动刷新
// Create the main myMSALObj instance
// configuration parameters are located at authConfig.js
const myMSALObj = new msal.PublicClientApplication(msalConfig);
let accountId = "";
let username = "";
function setAccount(account) {
accountId = account.homeAccountId;
username = account.username;
welcomeUser(username);
}
function selectAccount() {
/**
* See here for more info on account retrieval:
* https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-common/docs/Accounts.md
*/
const currentAccounts = myMSALObj.getAllAccounts();
if (currentAccounts.length < 1) {
return;
} else if (currentAccounts.length > 1) {
/**
* Due to the way MSAL caches account objects, the auth response from initiating a user-flow
* is cached as a new account, which results in more than one account in the cache. Here we make
* sure we are selecting the account with homeAccountId that contains the sign-up/sign-in user-flow,
* as this is the default flow the user initially signed-in with.
*/
const accounts = currentAccounts.filter(account =>
account.homeAccountId.toUpperCase().includes(b2cPolicies.names.signUpSignIn.toUpperCase())
&&
account.idTokenClaims.iss.toUpperCase().includes(b2cPolicies.authorityDomain.toUpperCase())
&&
account.idTokenClaims.aud === msalConfig.auth.clientId
);
if (accounts.length > 1) {
// localAccountId identifies the entity for which the token asserts information.
if (accounts.every(account => account.localAccountId === accounts[0].localAccountId)) {
// All accounts belong to the same user
setAccount(accounts[0]);
} else {
// Multiple users detected. Logout all to be safe.
signOut();
};
} else if (accounts.length === 1) {
setAccount(accounts[0]);
}
} else if (currentAccounts.length === 1) {
setAccount(currentAccounts[0]);
}
}
// in case of page refresh
selectAccount();
function handleResponse(response) {
/**
* To see the full list of response object properties, visit:
* https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-browser/docs/request-response-object.md#response
*/
if (response !== null) {
setAccount(response.account);
} else {
selectAccount();
}
}
function signIn() {
/**
* You can pass a custom request object below. This will override the initial configuration. For more information, visit:
* https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-browser/docs/request-response-object.md#request
*/
myMSALObj.loginPopup(loginRequest)
.then(handleResponse)
.catch(error => {
console.log(error);
});
}
function signOut() {
/**
* You can pass a custom request object below. This will override the initial configuration. For more information, visit:
* https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-browser/docs/request-response-object.md#request
*/
const logoutRequest = {
postLogoutRedirectUri: msalConfig.auth.redirectUri,
mainWindowRedirectUri: msalConfig.auth.redirectUri
};
myMSALObj.logoutPopup(logoutRequest);
}
function getTokenPopup(request) {
/**
* See here for more information on account retrieval:
* https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-common/docs/Accounts.md
*/
request.account = myMSALObj.getAccountByHomeId(accountId);
/**
*
*/
return myMSALObj.acquireTokenSilent(request)
.then((response) => {
// In case the response from B2C server has an empty accessToken field
// throw an error to initiate token acquisition
if (!response.accessToken || response.accessToken === "") {
throw new msal.InteractionRequiredAuthError;
}
return response;
})
.catch(error => {
console.log("Silent token acquisition fails. Acquiring token using popup. \n", error);
if (error instanceof msal.InteractionRequiredAuthError) {
// fallback to interaction when silent call fails
return myMSALObj.acquireTokenPopup(request)
.then(response => {
console.log(response);
return response;
}).catch(error => {
console.log(error);
});
} else {
console.log(error);
}
});
}
function passTokenToApi() {
getTokenPopup(tokenRequest)
.then(response => {
if (response) {
console.log("access_token acquired at: " + new Date().toString());
try {
callApi(apiConfig.webApi, response.accessToken);
} catch (error) {
console.log(error);
}
}
});
}
/**
* To initiate a B2C user-flow, simply make a login request using
* the full authority string of that user-flow e.g.
* https://fabrikamb2c.b2clogin.com/fabrikamb2c.onmicrosoft.com/B2C_1_edit_profile_v2
*/
function editProfile() {
const editProfileRequest = b2cPolicies.authorities.editProfile;
editProfileRequest.loginHint = myMSALObj.getAccountByHomeId(accountId).username;
myMSALObj.loginPopup(editProfileRequest)
.catch(error => {
console.log(error);
});
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。