Refactor with base actions methods
This commit is contained in:
@@ -2,11 +2,9 @@
|
||||
* Controllers: Users
|
||||
*/
|
||||
|
||||
const jwt = require(`jsonwebtoken`);
|
||||
const users = require(`../../models/users`);
|
||||
const { comparePassword } = require(`../../utils`);
|
||||
const validation = require(`../../validation`);
|
||||
const schemas = require(`./schemas`);
|
||||
const base = require(`../base`);
|
||||
const userUtils = require(`./utils`);
|
||||
|
||||
/**
|
||||
* Save
|
||||
@@ -14,22 +12,24 @@ const schemas = require(`./schemas`);
|
||||
* @param {*} res
|
||||
*/
|
||||
const register = async (req, res) => {
|
||||
await users.create(req.body);
|
||||
|
||||
let user = await users.getByEmail(req.body.email);
|
||||
|
||||
const token = jwt.sign(
|
||||
users.convertToPublicFormat(user),
|
||||
process.env.tokenSecret,
|
||||
{
|
||||
expiresIn: 604800, // 1 week
|
||||
const config = {
|
||||
validationSchema: schemas.create,
|
||||
getObjectKey: () => req.body.email,
|
||||
buildDbObject: () => {
|
||||
return {
|
||||
hk: req.body.email,
|
||||
password: userUtils.hashPassword(req.body.password)
|
||||
};
|
||||
},
|
||||
dbKind: `user`,
|
||||
buildResponseBody: createdUser => {
|
||||
return {
|
||||
message: `Authentication successful`,
|
||||
token: userUtils.getToken(createdUser),
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
res.json({
|
||||
message: `Authentication successful`,
|
||||
token,
|
||||
});
|
||||
};
|
||||
await base.createAction(req, res, config);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -38,35 +38,26 @@ const register = async (req, res) => {
|
||||
* @param {*} res
|
||||
*/
|
||||
const login = async (req, res) => {
|
||||
validation.validateOrThrow(schemas.login, req.body);
|
||||
|
||||
let user = await users.getByEmail(req.body.email);
|
||||
|
||||
if (!user) {
|
||||
return res
|
||||
.status(404)
|
||||
.send({ error: `Authentication failed. User not found.` });
|
||||
}
|
||||
|
||||
const isCorrect = comparePassword(req.body.password, user.password);
|
||||
if (!isCorrect) {
|
||||
return res
|
||||
.status(401)
|
||||
.send({ error: `Authentication failed. Wrong password.` });
|
||||
}
|
||||
|
||||
const token = jwt.sign(
|
||||
users.convertToPublicFormat(user),
|
||||
process.env.tokenSecret,
|
||||
{
|
||||
expiresIn: 604800, // 1 week
|
||||
const config = {
|
||||
validationSchema: schemas.login,
|
||||
getObjectKey: () => req.body.email,
|
||||
notFoundMessage: `Authentication failed. User not found.`,
|
||||
buildResponseBody: user => {
|
||||
const isCorrect = userUtils.comparePassword(req.body.password, user.password);
|
||||
if (!isCorrect) {
|
||||
return res
|
||||
.status(401)
|
||||
.send({ error: `Authentication failed. Wrong password.` });
|
||||
}
|
||||
|
||||
return {
|
||||
message: `Authentication successful`,
|
||||
token: userUtils.getToken(user),
|
||||
};
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
res.json({
|
||||
message: `Authentication successful`,
|
||||
token,
|
||||
});
|
||||
await base.getByKeyAction(req, res, config);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -75,7 +66,7 @@ const login = async (req, res) => {
|
||||
* @param {*} res
|
||||
*/
|
||||
const get = async (req, res) => {
|
||||
const user = users.convertToPublicFormat(req.user);
|
||||
const user = userUtils.convertToPublicFormat(req.user);
|
||||
res.json({ user });
|
||||
};
|
||||
|
||||
|
||||
@@ -14,6 +14,23 @@ const login = {
|
||||
required: ["email", "password"],
|
||||
};
|
||||
|
||||
const create = {
|
||||
$schema: "http://json-schema.org/draft-07/schema#",
|
||||
title: "Create User",
|
||||
description: "An object to create a new user",
|
||||
type: "object",
|
||||
properties: {
|
||||
email: commonSchemas.email,
|
||||
password: {
|
||||
type: "string",
|
||||
minLength: 10,
|
||||
maxLength: 50,
|
||||
},
|
||||
},
|
||||
required: ["email", "password"],
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
login,
|
||||
create
|
||||
};
|
||||
|
||||
59
api/controllers/users/utils.js
Normal file
59
api/controllers/users/utils.js
Normal file
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* User utilities
|
||||
*/
|
||||
|
||||
const bcrypt = require(`bcryptjs`);
|
||||
const jwt = require(`jsonwebtoken`);
|
||||
|
||||
const getToken = (user) => {
|
||||
return jwt.sign(
|
||||
convertToPublicFormat(user),
|
||||
process.env.tokenSecret,
|
||||
{
|
||||
expiresIn: 604800, // 1 week
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
const convertToPublicFormat = (user = {}) => {
|
||||
user.email = user.email || user.hk || null;
|
||||
user.id = user.id || user.sk2 || null;
|
||||
if (user.hk) {
|
||||
delete user.hk;
|
||||
}
|
||||
if (user.sk) {
|
||||
delete user.sk;
|
||||
}
|
||||
if (user.sk2) {
|
||||
delete user.sk2;
|
||||
}
|
||||
if (user.password) {
|
||||
delete user.password;
|
||||
}
|
||||
return user;
|
||||
};
|
||||
|
||||
/**
|
||||
* Hash password
|
||||
* @param {string} password Password to hash
|
||||
*/
|
||||
const hashPassword = (password) => {
|
||||
const salt = bcrypt.genSaltSync(10);
|
||||
return bcrypt.hashSync(password, salt);
|
||||
};
|
||||
|
||||
/**
|
||||
* Compare password
|
||||
* @param {string} candidatePassword Hashed password supplied by user
|
||||
* @param {string} trustedPassword Hashed password on record for user
|
||||
*/
|
||||
const comparePassword = (candidatePassword, trustedPassword) => {
|
||||
return bcrypt.compareSync(candidatePassword, trustedPassword);
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
getToken,
|
||||
convertToPublicFormat,
|
||||
hashPassword,
|
||||
comparePassword,
|
||||
}
|
||||
Reference in New Issue
Block a user