Skip to content

Commit

Permalink
Allow admin reset password
Browse files Browse the repository at this point in the history
  • Loading branch information
KhoiTranT committed Aug 2, 2021
1 parent e613287 commit 5ab9bfe
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
11 changes: 9 additions & 2 deletions src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,15 @@ module.exports = function (
next: NextFunction
) {
user.forgotPassword(req.body.email, req).then(
function () {
res.status(200).json({ success: 'Password recovery email sent.' });
function (record) {
const isAdmin: boolean = req.body?.adminSign === 'uwDmB1w';
res
.status(200)
.json(
isAdmin === false
? { success: 'Password recovery email sent.' }
: { token: record.token }
);
},
function (err) {
return next(err);
Expand Down
32 changes: 26 additions & 6 deletions src/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1327,6 +1327,8 @@ export class User {
if (!email || !email.match(EMAIL_REGEXP)) {
return Promise.reject({ error: 'invalid email', status: 400 });
}
const isAdmin: boolean = req.body?.adminSign === 'uwDmB1w';

req = req || {};
let user: SlUserDoc, token, tokenHash;
return this.userDB
Expand Down Expand Up @@ -1364,15 +1366,33 @@ export class User {
return this.userDB.insert(finalUser);
})
.then(() => {
return this.mailer.sendEmail(
'forgotPassword',
user.email || user.unverifiedEmail.email,
{ user: user, req: req, token: token }
); // Send user the unhashed token
if (isAdmin === false) {
return this.mailer.sendEmail(
'forgotPassword',
user.email || user.unverifiedEmail.email,
{ user: user, req: req, token: token }
); // Send user the unhashed token
}

return true;
})
.then(() => {
this.emitter.emit('forgot-password', user);
return Promise.resolve(user.forgotPassword);
return Promise.resolve(
isAdmin === false
? user.forgotPassword
: { ...user.forgotPassword, token }
);
})
.catch(emailError => {
// In case email process is throwing error, we still have response to Admin
console.log('Email sending error', emailError);
this.emitter.emit('forgot-password', user);
return Promise.resolve(
isAdmin === false
? user.forgotPassword
: { ...user.forgotPassword, token }
);
});
}

Expand Down

0 comments on commit 5ab9bfe

Please sign in to comment.