Skip to content
This repository has been archived by the owner. It is now read-only.

Commit a823d43

Browse files
author
derisen
committed
forgot password policy
1 parent 95fae76 commit a823d43

File tree

5 files changed

+41
-20
lines changed

5 files changed

+41
-20
lines changed

JavaScriptSPA/authConfig.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
const msalConfig = {
88
auth: {
99
clientId: "e760cab2-b9a1-4c0d-86fb-ff7084abd902",
10-
authority: b2cPolicies.signInSignUp.authority,
10+
authority: b2cPolicies.authorities.signUpSignIn.authority,
1111
validateAuthority: false
1212
},
1313
cache: {

JavaScriptSPA/authPopup.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ function signIn() {
1515
}).catch(function (error) {
1616
console.log(error);
1717

18-
// error handling
18+
// Error handling
1919
if (error.errorMessage) {
20-
// check for forgot password error
21-
// learn more about AAD error codes at https://docs.microsoft.com/en-us/azure/active-directory/develop/reference-aadsts-error-codes
20+
// Check for forgot password error
21+
// Learn more about AAD error codes at https://docs.microsoft.com/en-us/azure/active-directory/develop/reference-aadsts-error-codes
2222
if (error.errorMessage.indexOf("AADB2C90118") > -1) {
23-
myMSALObj.loginPopup(b2cPolicies.forgotPassword)
23+
myMSALObj.loginPopup(b2cPolicies.authorities.forgotPassword)
2424
.then(loginResponse => {
2525
console.log(loginResponse);
2626
window.alert("Password has been reset successfully. \nPlease sign-in with your new password.");
@@ -30,7 +30,7 @@ function signIn() {
3030
});
3131
}
3232

33-
// sign-out the user
33+
// Sign-out the user
3434
function logout() {
3535
// Removes all sessions, need to call AAD endpoint to do full logout
3636
myMSALObj.logout();

JavaScriptSPA/authRedirect.js

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,33 @@ let accessToken;
88
myMSALObj.handleRedirectCallback(authRedirectCallBack);
99

1010
function authRedirectCallBack(error, response) {
11+
// Error handling
1112
if (error) {
1213
console.log(error);
14+
15+
// Check for forgot password error
16+
// Learn more about AAD error codes at https://docs.microsoft.com/en-us/azure/active-directory/develop/reference-aadsts-error-codes
17+
if (error.errorMessage.indexOf("AADB2C90118") > -1) {
18+
try {
19+
// Password reset policy/authority
20+
myMSALObj.loginRedirect(b2cPolicies.authorities.forgotPassword);
21+
} catch(err) {
22+
console.log(err);
23+
}
24+
}
1325
} else {
14-
if (response.tokenType === "id_token") {
26+
// We need to reject id tokens that were not issued with the default sign-in policy.
27+
// To learn more about b2c tokens, visit https://docs.microsoft.com/en-us/azure/active-directory-b2c/tokens-overview
28+
if (response.tokenType === "id_token" && response.idToken.claims['acr'] !== b2cPolicies.names.signUpSignIn) {
29+
myMSALObj.logout();
30+
window.alert("Password has been reset successfully. \nPlease sign-in with your new password.");
31+
} else if (response.tokenType === "id_token" && response.idToken.claims['acr'] === b2cPolicies.names.signUpSignIn) {
1532
console.log("id_token acquired at: " + new Date().toString());
16-
myMSALObj.getAccount();
17-
getTokenRedirect(tokenRequest);
33+
34+
if (myMSALObj.getAccount()) {
35+
updateUI();
36+
}
37+
1838
} else if (response.tokenType === "access_token") {
1939
console.log("access_token acquired at: " + new Date().toString());
2040
accessToken = response.accessToken;
@@ -32,11 +52,6 @@ function authRedirectCallBack(error, response) {
3252
}
3353
}
3454

35-
// Redirect: once login is successful and redirects with tokens, update UI
36-
if (myMSALObj.getAccount()) {
37-
updateUI();
38-
}
39-
4055
function signIn() {
4156
myMSALObj.loginRedirect(loginRequest);
4257
}

JavaScriptSPA/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
<title>AAD B2C | MSAL.JS Vanilla JavaScript SPA</title>
77

88
<!-- msal.min.js can be used in the place of msal.js; included msal.js to make debug easy -->
9-
<script type="text/javascript" src="https://alcdn.msauth.net/lib/1.2.2/js/msal.js" integrity="sha384-n2/wxR+doMGeL8Lmj4kdPRfZBUg2d/OAVqaKCoHPWOfOs1HUFU3laBvp3gv/HBOu" crossorigin="anonymous"></script>
9+
<script type="text/javascript" src="https://alcdn.msauth.net/lib/1.3.0/js/msal.js" integrity="sha384-xeOjp8/l8VazdeNFRbrC9LWPR1InyrS8E1Na/0lv6V2r09iwX6vJC47FXlczokMi" crossorigin="anonymous"></script>
1010
<!-- msal.js with a fallback to backup CDN -->
1111
<script type="text/javascript">
12-
if(typeof Msal === 'undefined')document.write(unescape("%3Cscript src='https://alcdn.msftauth.net/lib/1.2.2/js/msal.js' type='text/javascript' integrity='sha384-n2/wxR+doMGeL8Lmj4kdPRfZBUg2d/OAVqaKCoHPWOfOs1HUFU3laBvp3gv/HBOu' crossorigin='anonymous'%3E%3C/script%3E"));
12+
if(typeof Msal === 'undefined')document.write(unescape("%3Cscript src='https://alcdn.msftauth.net/lib/1.3.0/js/msal.js' type='text/javascript' integrity='sha384-xeOjp8/l8VazdeNFRbrC9LWPR1InyrS8E1Na/0lv6V2r09iwX6vJC47FXlczokMi' crossorigin='anonymous'%3E%3C/script%3E"));
1313
</script>
1414

1515
<!-- adding Bootstrap 4 for UI components -->

JavaScriptSPA/policies.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@
33
// To learn more about custom policies, visit https://docs.microsoft.com/en-us/azure/active-directory-b2c/custom-policy-overview
44

55
const b2cPolicies = {
6-
signInSignUp: {
7-
authority: "https://fabrikamb2c.b2clogin.com/fabrikamb2c.onmicrosoft.com/b2c_1_susi",
6+
names: {
7+
signUpSignIn: "b2c_1_susi",
8+
forgotPassword: "b2c_1_reset"
89
},
9-
forgotPassword: {
10-
authority: "https://fabrikamb2c.b2clogin.com/fabrikamb2c.onmicrosoft.com/b2c_1_reset",
10+
authorities: {
11+
signUpSignIn: {
12+
authority: "https://fabrikamb2c.b2clogin.com/fabrikamb2c.onmicrosoft.com/b2c_1_susi",
13+
},
14+
forgotPassword: {
15+
authority: "https://fabrikamb2c.b2clogin.com/fabrikamb2c.onmicrosoft.com/b2c_1_reset",
16+
},
1117
},
1218
}

0 commit comments

Comments
 (0)