1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-18 20:59:44 +02:00
planka/server/api/controllers/access-tokens/create.js

56 lines
1.2 KiB
JavaScript
Raw Normal View History

2019-08-31 04:07:25 +05:00
const bcrypt = require('bcrypt');
2020-04-03 00:35:25 +05:00
const validator = require('validator');
2019-08-31 04:07:25 +05:00
const Errors = {
2020-04-03 00:35:25 +05:00
INVALID_EMAIL_OR_USERNAME: {
invalidEmailOrUsername: 'Invalid email or username',
2019-08-31 04:07:25 +05:00
},
2020-04-03 00:35:25 +05:00
INVALID_PASSWORD: {
invalidPassword: 'Invalid password',
},
2019-08-31 04:07:25 +05:00
};
module.exports = {
inputs: {
2020-04-03 00:35:25 +05:00
emailOrUsername: {
2019-08-31 04:07:25 +05:00
type: 'string',
2020-04-03 00:35:25 +05:00
custom: (value) =>
value.includes('@')
? validator.isEmail(value)
2021-04-13 18:59:02 +05:00
: value.length >= 3 &&
value.length <= 16 &&
/^[a-zA-Z0-9]+((_|\.)?[a-zA-Z0-9])*$/.test(value),
2019-08-31 04:07:25 +05:00
required: true,
},
password: {
type: 'string',
required: true,
},
2019-08-31 04:07:25 +05:00
},
exits: {
2020-04-03 00:35:25 +05:00
invalidEmailOrUsername: {
responseType: 'unauthorized',
},
invalidPassword: {
responseType: 'unauthorized',
},
2019-08-31 04:07:25 +05:00
},
async fn(inputs) {
const user = await sails.helpers.users.getOneByEmailOrUsername(inputs.emailOrUsername);
2019-08-31 04:07:25 +05:00
if (!user) {
2020-04-03 00:35:25 +05:00
throw Errors.INVALID_EMAIL_OR_USERNAME;
2019-08-31 04:07:25 +05:00
}
if (!bcrypt.compareSync(inputs.password, user.password)) {
2020-04-03 00:35:25 +05:00
throw Errors.INVALID_PASSWORD;
2019-08-31 04:07:25 +05:00
}
return {
item: sails.helpers.utils.createToken(user.id),
};
},
2019-08-31 04:07:25 +05:00
};