Format and test fixes
This commit is contained in:
@@ -80,6 +80,9 @@ app.use(function (err, req, res, next) {
|
||||
if (res.headersSent) {
|
||||
return next(err);
|
||||
}
|
||||
if (err.code && err.code === 500) {
|
||||
console.log(err);
|
||||
}
|
||||
res.status(err.code || 500);
|
||||
res.json({
|
||||
error: err.message || `Internal Server Error - "${err.message}"`,
|
||||
|
||||
@@ -3,8 +3,10 @@
|
||||
*/
|
||||
|
||||
const jwt = require(`jsonwebtoken`);
|
||||
const users = require(`../models/users`);
|
||||
const { comparePassword } = require(`../utils`);
|
||||
const users = require(`../../models/users`);
|
||||
const { comparePassword } = require(`../../utils`);
|
||||
const validation = require(`../../validation`);
|
||||
const schemas = require(`./schemas`);
|
||||
|
||||
/**
|
||||
* Save
|
||||
@@ -13,7 +15,7 @@ const { comparePassword } = require(`../utils`);
|
||||
*/
|
||||
const register = async (req, res) => {
|
||||
await users.create(req.body);
|
||||
|
||||
|
||||
let user = await users.getByEmail(req.body.email);
|
||||
|
||||
const token = jwt.sign(
|
||||
@@ -36,6 +38,8 @@ const register = async (req, res) => {
|
||||
* @param {*} res
|
||||
*/
|
||||
const login = async (req, res) => {
|
||||
validation.validateOrThrow(schemas.login, res.body);
|
||||
|
||||
let user = await users.getByEmail(req.body.email);
|
||||
|
||||
if (!user) {
|
||||
19
api/controllers/users/schemas.js
Normal file
19
api/controllers/users/schemas.js
Normal file
@@ -0,0 +1,19 @@
|
||||
const commonSchemas = require(`../../validation/commonSchemas`);
|
||||
|
||||
const login = {
|
||||
$schema: "http://json-schema.org/draft-07/schema#",
|
||||
title: "Login User",
|
||||
description: "An object to log-in a new user",
|
||||
type: "object",
|
||||
properties: {
|
||||
email: commonSchemas.email,
|
||||
password: {
|
||||
type: "string",
|
||||
},
|
||||
},
|
||||
required: ["email", "password"],
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
login,
|
||||
};
|
||||
@@ -1,12 +1,11 @@
|
||||
"use strict";
|
||||
/**
|
||||
* Model: Users
|
||||
*/
|
||||
const utils = require(`../../utils`);
|
||||
const db = require(`../db`);
|
||||
const schemas = require(`./schemas`);
|
||||
const sharedSchemas = require(`../schemas`);
|
||||
const validation = require(`../validation`);
|
||||
const commonSchemas = require(`../../validation/commonSchemas`);
|
||||
const validation = require(`../../validation`);
|
||||
|
||||
/**
|
||||
* Create a new user
|
||||
@@ -19,7 +18,8 @@ const create = async (user = {}) => {
|
||||
const existingUser = await getByEmail(user.email);
|
||||
if (existingUser) {
|
||||
throw utils.customError(
|
||||
`A user with email "${user.email}" is already registered`
|
||||
`A user with email "${user.email}" is already registered`,
|
||||
400
|
||||
);
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ const create = async (user = {}) => {
|
||||
* @param {string} email Email address of user to retrieve
|
||||
*/
|
||||
const getByEmail = async (email) => {
|
||||
validation.validateOrThrow(sharedSchemas.email, email);
|
||||
validation.validateOrThrow(commonSchemas.email, email);
|
||||
|
||||
let user = await db.getByKey(email);
|
||||
|
||||
@@ -49,7 +49,7 @@ const getByEmail = async (email) => {
|
||||
* @param {string} id
|
||||
*/
|
||||
const getById = async (id) => {
|
||||
validation.validateOrThrow(sharedSchemas.id, id);
|
||||
validation.validateOrThrow(commonSchemas.id, id);
|
||||
|
||||
let user = await db.getById(`user`, id);
|
||||
|
||||
|
||||
@@ -1,19 +1,21 @@
|
||||
const sharedSchemas = require(`../schemas`);
|
||||
const commonSchemas = require(`../../validation/commonSchemas`);
|
||||
|
||||
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 = {
|
||||
"create": {
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"title": "Create User",
|
||||
"description": "An object to create a new user",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"email": sharedSchemas.email,
|
||||
"password": {
|
||||
"type": "string",
|
||||
"minLength": 10,
|
||||
"maxLength": 50
|
||||
}
|
||||
},
|
||||
"required": ["email", "password"]
|
||||
}
|
||||
create,
|
||||
};
|
||||
|
||||
@@ -9,7 +9,7 @@ const bcrypt = require(`bcryptjs`);
|
||||
* @param {string} message Error message
|
||||
* @param {number} code Error code, should be HTTP status code
|
||||
*/
|
||||
const customError = (message, code) => {
|
||||
const customError = (message = "An error occurred.", code = 500) => {
|
||||
const error = new Error(message);
|
||||
error.code = code;
|
||||
return error;
|
||||
@@ -19,7 +19,7 @@ const customError = (message, code) => {
|
||||
* Build a custom validation error
|
||||
* @param {array[string]} validationErrors Validation errors
|
||||
*/
|
||||
const validationError = (validationErrors) => {
|
||||
const validationError = (validationErrors = []) => {
|
||||
let error = new Error("Validation error(s)");
|
||||
error.validationErrors = validationErrors;
|
||||
error.code = 400;
|
||||
|
||||
Reference in New Issue
Block a user