1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-19 21:29:43 +02:00
planka/server/api/controllers/access-tokens/create.js

49 lines
848 B
JavaScript
Raw Normal View History

2019-08-31 04:07:25 +05:00
const bcrypt = require('bcrypt');
const Errors = {
EMAIL_NOT_EXIST: {
unauthorized: 'Email does not exist',
2019-08-31 04:07:25 +05:00
},
PASSWORD_NOT_VALID: {
unauthorized: 'Password is not valid',
},
2019-08-31 04:07:25 +05:00
};
module.exports = {
inputs: {
email: {
type: 'string',
required: true,
isEmail: true,
2019-08-31 04:07:25 +05:00
},
password: {
type: 'string',
required: true,
},
2019-08-31 04:07:25 +05:00
},
exits: {
unauthorized: {
responseType: 'unauthorized',
},
2019-08-31 04:07:25 +05:00
},
async fn(inputs, exits) {
2019-08-31 04:07:25 +05:00
const user = await sails.helpers.getUser({
email: inputs.email.toLowerCase(),
2019-08-31 04:07:25 +05:00
});
if (!user) {
throw Errors.EMAIL_NOT_EXIST;
}
if (!bcrypt.compareSync(inputs.password, user.password)) {
throw Errors.PASSWORD_NOT_VALID;
}
return exits.success({
item: sails.helpers.signToken(user.id),
2019-08-31 04:07:25 +05:00
});
},
2019-08-31 04:07:25 +05:00
};