This commit is contained in:
parent
f546edb810
commit
ffaec0b971
14 changed files with 137 additions and 33 deletions
42
src/api/endpoints/i/regenerate_token.ts
Normal file
42
src/api/endpoints/i/regenerate_token.ts
Normal file
|
@ -0,0 +1,42 @@
|
|||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
import $ from 'cafy';
|
||||
import * as bcrypt from 'bcryptjs';
|
||||
import User from '../../models/user';
|
||||
import event from '../../event';
|
||||
import generateUserToken from '../../common/generate-native-user-token';
|
||||
|
||||
/**
|
||||
* Regenerate native token
|
||||
*
|
||||
* @param {any} params
|
||||
* @param {any} user
|
||||
* @return {Promise<any>}
|
||||
*/
|
||||
module.exports = async (params, user) => new Promise(async (res, rej) => {
|
||||
// Get 'password' parameter
|
||||
const [password, passwordErr] = $(params.password).string().$;
|
||||
if (passwordErr) return rej('invalid password param');
|
||||
|
||||
// Compare password
|
||||
const same = bcrypt.compareSync(password, user.password);
|
||||
|
||||
if (!same) {
|
||||
return rej('incorrect password');
|
||||
}
|
||||
|
||||
// Generate secret
|
||||
const secret = generateUserToken();
|
||||
|
||||
await User.update(user._id, {
|
||||
$set: {
|
||||
token: secret
|
||||
}
|
||||
});
|
||||
|
||||
res();
|
||||
|
||||
// Publish i updated event
|
||||
event(user._id, 'my_token_regenerated');
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue