Skip to content

Commit d1324bd

Browse files
committed
fix password reset
1 parent b5f0f00 commit d1324bd

1 file changed

Lines changed: 37 additions & 15 deletions

File tree

1-Authentication/2-sign-in-b2c/App/utils/identity.js

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,23 +72,41 @@ let nonce;
7272
// ============ MIDDLEWARE =============
7373

7474
exports.signIn = (req, res, next) => {
75-
if (authCodeRequest.state === APP_STATES.passwordReset) {
75+
let state = null;
7676

77-
nonce = generateGuid();
77+
if (authCodeRequest.state) {
78+
state = JSON.parse(base64DecodeUrl(authCodeRequest.state))
79+
}
7880

79-
const state = base64EncodeUrl(JSON.stringify({
80-
stage: APP_STATES.passwordReset,
81-
path: req.route.path,
82-
nonce: nonce
83-
}));
81+
if (state) {
82+
if (state.stage === APP_STATES.passwordReset) {
8483

85-
// if coming for password reset, set the authority to password reset
86-
getAuthCode(auth.policies.authorities.resetPassword.authority, OIDC_SCOPES, state, res);
84+
nonce = generateGuid();
85+
86+
const state = base64EncodeUrl(JSON.stringify({
87+
stage: APP_STATES.passwordReset,
88+
path: req.route.path,
89+
nonce: nonce
90+
}));
91+
92+
// if coming for password reset, set the authority to password reset
93+
getAuthCode(auth.policies.authorities.resetPassword.authority, OIDC_SCOPES, state, res);
94+
} else {
95+
// else, login as usual
96+
97+
nonce = generateGuid();
98+
99+
const state = base64EncodeUrl(JSON.stringify({
100+
stage: APP_STATES.login,
101+
path: req.route.path,
102+
nonce: nonce
103+
}))
104+
105+
getAuthCode(auth.policies.authorities.signUpSignIn.authority, OIDC_SCOPES, state, res);
106+
}
87107
} else {
88-
// else, login as usual
89-
90108
nonce = generateGuid();
91-
109+
92110
const state = base64EncodeUrl(JSON.stringify({
93111
stage: APP_STATES.login,
94112
path: req.route.path,
@@ -135,15 +153,17 @@ exports.handleRedirect = async(req, res, next) => {
135153
}
136154
}).catch((error) => {
137155
console.log(req.query.error)
156+
console.log(req.query.error_description)
157+
console.log('hey1')
138158
if (req.query.error) {
139-
159+
console.log('hey2')
140160
/**
141161
* When the user selects "forgot my password" on the sign-in page, B2C service will throw an error.
142162
* We are to catch this error and redirect the user to login again with the resetPassword authority.
143163
* For more information, visit: https://docs.microsoft.com/azure/active-directory-b2c/user-flow-overview#linking-user-flows
144164
*/
145165
if (JSON.stringify(req.query.error_description).includes("AADB2C90118")) {
146-
166+
console.log('hey3')
147167
nonce = generateGuid();
148168

149169
const state = base64EncodeUrl(JSON.stringify({
@@ -171,6 +191,7 @@ exports.handleRedirect = async(req, res, next) => {
171191
}))
172192

173193
authCodeRequest.state = state;
194+
174195
res.redirect('/signin');
175196
} else {
176197
res.status(500).send("unknown");
@@ -204,14 +225,15 @@ exports.isAuthenticated = (req, res, next) => {
204225
* This method is used to generate an auth code request
205226
* @param {string} authority: the authority to request the auth code from
206227
* @param {array} scopes: scopes to request the auth code for
207-
* @param {string} state: state of the application
228+
* @param {object} state: state of the application
208229
* @param {object} res: express middleware response object
209230
*/
210231
const getAuthCode = (authority, scopes, state, res) => {
211232

212233
// prepare the request
213234
authCodeRequest.authority = authority;
214235
authCodeRequest.scopes = scopes;
236+
console.log(state);
215237
authCodeRequest.state = state;
216238

217239
tokenRequest.authority = authority;

0 commit comments

Comments
 (0)