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

Commit 419965e

Browse files
author
derisen
committed
password reset policy
1 parent 77867c7 commit 419965e

File tree

5 files changed

+57
-29
lines changed

5 files changed

+57
-29
lines changed

JavaScriptSPA/authConfig.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11

2-
// Config object to be passed to Msal on creation.
3-
// For a full list of msal.js configuration parameters,
4-
// visit https://azuread.github.io/microsoft-authentication-library-for-js/docs/msal/modules/_configuration_.html
2+
/**
3+
* Config object to be passed to Msal on creation.
4+
* For a full list of msal.js configuration parameters,
5+
* visit https://azuread.github.io/microsoft-authentication-library-for-js/docs/msal/modules/_configuration_.html
6+
* */
57
const msalConfig = {
68
auth: {
79
clientId: "e760cab2-b9a1-4c0d-86fb-ff7084abd902",
8-
authority: "https://fabrikamb2c.b2clogin.com/fabrikamb2c.onmicrosoft.com/b2c_1_susi",
10+
authority: b2cPolicies.signInSignUp.authority,
911
validateAuthority: false
1012
},
1113
cache: {
@@ -14,9 +16,10 @@ const msalConfig = {
1416
}
1517
};
1618

17-
// Add here scopes for id token to be used at the MS Identity Platform endpoint
18-
// For a full list of available authentication parameters,
19-
// visit https://azuread.github.io/microsoft-authentication-library-for-js/docs/msal/modules/_authenticationparameters_.html
19+
/**
20+
* Scopes you enter here will be consented once you authenticate. For a full list of available authentication parameters,
21+
* visit https://azuread.github.io/microsoft-authentication-library-for-js/docs/msal/modules/_authenticationparameters_.html
22+
*/
2023
const loginRequest = {
2124
scopes: ["openid", "profile"],
2225
};

JavaScriptSPA/authPopup.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const myMSALObj = new Msal.UserAgentApplication(msalConfig);
55
function signIn() {
66
myMSALObj.loginPopup(loginRequest)
77
.then(loginResponse => {
8-
console.log('id_token acquired at: ' + new Date().toString());
8+
console.log("Id_token acquired at: " + new Date().toString());
99
console.log(loginResponse);
1010

1111
if (myMSALObj.getAccount()) {
@@ -14,6 +14,19 @@ function signIn() {
1414

1515
}).catch(function (error) {
1616
console.log(error);
17+
18+
// error handling
19+
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
22+
if (error.errorMessage.indexOf("AADB2C90118") > -1) {
23+
myMSALObj.loginPopup(b2cPolicies.forgotPassword)
24+
.then(loginResponse => {
25+
console.log(loginResponse);
26+
window.alert("Password has been reset successfully. \nPlease sign-in with your new password.");
27+
})
28+
}
29+
}
1730
});
1831
}
1932

@@ -26,12 +39,12 @@ function logout() {
2639
function getTokenPopup(request) {
2740
return myMSALObj.acquireTokenSilent(request)
2841
.catch(error => {
29-
console.log("silent token acquisition fails. acquiring token using popup");
42+
console.log("Silent token acquisition fails. Acquiring token using popup");
3043
console.log(error);
3144
// fallback to interaction when silent call fails
3245
return myMSALObj.acquireTokenPopup(request)
3346
.then(tokenResponse => {
34-
console.log('access_token acquired at: ' + new Date().toString());
47+
console.log("access_token acquired at: " + new Date().toString());
3548
return tokenResponse;
3649
}).catch(error => {
3750
console.log(error);
@@ -43,7 +56,7 @@ function getTokenPopup(request) {
4356
function passTokenToApi() {
4457
getTokenPopup(tokenRequest)
4558
.then(tokenResponse => {
46-
console.log('access_token acquired at: ' + new Date().toString());
59+
console.log("access_token acquired at: " + new Date().toString());
4760
try {
4861
logMessage("Request made to Web API:")
4962
callApiWithAccessToken(apiConfig.webApi, tokenResponse.accessToken);

JavaScriptSPA/authRedirect.js

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@ function authRedirectCallBack(error, response) {
1212
console.log(error);
1313
} else {
1414
if (response.tokenType === "id_token") {
15-
console.log('id_token acquired at: ' + new Date().toString());
15+
console.log("id_token acquired at: " + new Date().toString());
1616
myMSALObj.getAccount();
1717
getTokenRedirect(tokenRequest);
1818
} else if (response.tokenType === "access_token") {
19-
console.log('access_token acquired at: ' + new Date().toString());
19+
console.log("access_token acquired at: " + new Date().toString());
2020
accessToken = response.accessToken;
21-
logMessage("Request made to Web API:")
22-
if (accessToken === null || accessToken === undefined) {
21+
logMessage("Request made to Web API:");
22+
if (accessToken) {
2323
try {
24-
callApiWithAccessToken(apiConfig.webApi, accessToken)
24+
callApiWithAccessToken(apiConfig.webApi, accessToken);
2525
} catch (err) {
2626
console.log(err);
2727
}
2828
}
2929
} else {
30-
console.log("token type is: " + response.tokenType);
30+
console.log("Token type is: " + response.tokenType);
3131
}
3232
}
3333
}
@@ -38,51 +38,50 @@ if (myMSALObj.getAccount()) {
3838
}
3939

4040
function signIn() {
41-
myMSALObj.loginRedirect(loginRequest)
41+
myMSALObj.loginRedirect(loginRequest);
4242
}
4343

44-
4544
// sign-out the user
4645
function logout() {
4746
// Removes all sessions, need to call AAD endpoint to do full logout
4847
myMSALObj.logout();
4948
}
5049

51-
// This function can be removed if you do not need to support IE
50+
// main method to get token with redirect flow
5251
function getTokenRedirect(request) {
5352
return myMSALObj.acquireTokenSilent(request)
5453
.then((response) => {
5554
if (response.accessToken) {
56-
accessToken = response.accessToken
57-
logMessage("Request made to Web API:")
55+
accessToken = response.accessToken;
56+
logMessage("Request made to Web API:");
5857

59-
if (accessToken === null || accessToken === undefined) {
58+
if (accessToken) {
6059
try {
61-
callApiWithAccessToken(apiConfig.webApi, accessToken)
60+
callApiWithAccessToken(apiConfig.webApi, accessToken);
6261
} catch (err) {
6362
console.log(err);
6463
}
6564
}
6665
}
6766
}).catch(error => {
68-
console.log("silent token acquisition fails. acquiring token using redirect");
67+
console.log("Silent token acquisition fails. Acquiring token using redirect");
6968
console.log(error);
7069
// fallback to interaction when silent call fails
71-
return myMSALObj.acquireTokenRedirect(request)
70+
return myMSALObj.acquireTokenRedirect(request);
7271
});
7372
}
7473

7574

7675
// calls the resource API with the token
7776
function passTokenToApi() {
78-
if (accessToken === null || accessToken === undefined) {
77+
if (!accessToken) {
7978
getTokenRedirect(tokenRequest);
8079
} else {
8180
logMessage("Request made to Web API:")
8281
try {
83-
callApiWithAccessToken(apiConfig.webApi, accessToken)
82+
callApiWithAccessToken(apiConfig.webApi, accessToken);
8483
} catch (err) {
8584
console.log(err);
8685
}
8786
}
88-
}
87+
}

JavaScriptSPA/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ <h5 id="label" class="card-title">Sign-in with Microsoft Azure AD B2C</h5>
4343
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
4444

4545
<!-- importing app scripts -->
46+
<script type="text/javascript" src="./policies.js"></script>
4647
<script type="text/javascript" src="./apiConfig.js"></script>
4748
<script type="text/javascript" src="./authConfig.js"></script>
4849
<script type="text/javascript" src="./ui.js"></script>

JavaScriptSPA/policies.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Enter here the user flows and custom policies for your B2C application
2+
// To learn more about user flows, visit https://docs.microsoft.com/en-us/azure/active-directory-b2c/user-flow-overview
3+
// To learn more about custom policies, visit https://docs.microsoft.com/en-us/azure/active-directory-b2c/custom-policy-overview
4+
5+
const b2cPolicies = {
6+
signInSignUp: {
7+
authority: "https://fabrikamb2c.b2clogin.com/fabrikamb2c.onmicrosoft.com/b2c_1_susi",
8+
},
9+
forgotPassword: {
10+
authority: "https://fabrikamb2c.b2clogin.com/fabrikamb2c.onmicrosoft.com/b2c_1_reset",
11+
},
12+
}

0 commit comments

Comments
 (0)