Skip to content

Commit

Permalink
Merge pull request perfood#2 from foxysun/koay-api
Browse files Browse the repository at this point in the history
Allow admin to reset password without inform user
  • Loading branch information
ahmedsoror-sbs authored Aug 2, 2021
2 parents d208352 + 5ab9bfe commit 74cdcb1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
},
"scripts": {
"test": "gulp",
"build": "node_modules/typescript/bin/tsc",
"lint": "node_modules/typescript/bin/tsc --noEmit && eslint \"**/*.{js,ts}\" --quiet --fix",
"prepare": "node_modules/typescript/bin/tsc"
"build": "tsc",
"lint": "tsc --noEmit && eslint \"**/*.{js,ts}\" --quiet --fix",
"prepare": "tsc"
},
"keywords": [
"authentication",
Expand Down
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 74cdcb1

Please sign in to comment.