Updated gitignore to ignore log files, Refactored Authcontroller to use DB interface, Added Try/Catch to validation to catch uncaught error, Added username optional validation to Joi so it expects a username
This commit is contained in:
1 parent
c5b93e7437
commit
a4a61695b0
7 files changed
+137
-72
No files matched your search
@@ -1,32 +1,43 @@
|
||||
const express = require('express')
|
||||
const UserModel = require('../models/user')
|
||||
const { authValidation } = require('../validation/joi')
|
||||
const express = require("express");
|
||||
const UserModel = require("../models/user");
|
||||
const { authValidation } = require("../validation/joi");
|
||||
|
||||
/**
|
||||
* @function
|
||||
* @param {express.Request} req
|
||||
* @param {express.Response} res
|
||||
* @param {express.Request} req
|
||||
* @param {express.Response} res
|
||||
* @returns {{success: Boolean, msg: String}}
|
||||
*/
|
||||
const registerController = async (req, res) => {
|
||||
// joi validation
|
||||
const { error } = await authValidation.validateAsync(req.body);
|
||||
if (error) return res.status(400).json({ success: false, msg: error.details[0].message });
|
||||
// joi validation
|
||||
try {
|
||||
await authValidation.validateAsync(req.body);
|
||||
} catch (error) {
|
||||
return res
|
||||
.status(400)
|
||||
.json({ success: false, msg: error.details[0].message });
|
||||
}
|
||||
|
||||
// Check if the user exists
|
||||
const isUser = await UserModel.findOne({ email: req.body.email });
|
||||
if (isUser) return res.status(400).json({ success: false, msg: "User already exists!" })
|
||||
// Check if the user exists
|
||||
try {
|
||||
const isUser = await req.db.getUserByEmail(req, res);
|
||||
if (isUser)
|
||||
return res
|
||||
.status(400)
|
||||
.json({ success: false, msg: "User already exists!" });
|
||||
} catch (error) {
|
||||
return res.status(500).json({ success: false, msg: error.message });
|
||||
}
|
||||
|
||||
try {
|
||||
// Create a new user
|
||||
const newUser = await UserModel.create({ ...req.body });
|
||||
try {
|
||||
// Create a new user
|
||||
const newUser = await req.db.insertUser(req, res);
|
||||
return res.json({ success: true, msg: "User created}", data: newUser });
|
||||
// Send an email to user
|
||||
// Will add this later
|
||||
} catch (error) {
|
||||
return res.status(500).json({ success: false, msg: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
// Send an email to user
|
||||
// Will add this later
|
||||
} catch (error) {
|
||||
return res.status(500).json({success:false,msg:'Something went wrong!'})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = {registerController}
|
||||
module.exports = { registerController };
|
||||
Reference in new issue
Block a user