Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] modify folder structure and add try catch for user routes and controller #62

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13,482 changes: 13,482 additions & 0 deletions client/package-lock.json

Large diffs are not rendered by default.

4,549 changes: 2,290 additions & 2,259 deletions client/yarn.lock

Large diffs are not rendered by default.

136 changes: 136 additions & 0 deletions controller/user.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
const express = require('express');
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const checkUserUniqueness = require('../utils/checkUserUniquess');
const User = require('../models/usersModel');
const validateEmail = require('../utils/validateEmail');
const config = require('../config');


const userValidate = async(req,res) => {
try {
const { field, value } = req.body;
const { error, isUnique } = await checkUserUniqueness(field, value);

if (isUnique) {
res.json({ success: 'success' });
} else {
res.json({ error });
}
} catch (error) {
res.status(400).json({error})
}
}
const userLogin = async (req, res) => {
try {
const username = req.body.username || '';
const password = req.body.password || '';

let errors = {};

if (username === '') {
errors = { ...errors, username: 'This field is required' };
}
if (password === '') {
errors = { ...errors, password: 'This field is required' };
}

if (Object.keys(errors).length > 0) {
res.json({ errors });
} else {
User.findOne({ username: username }, (err, user) => {
if (err) throw err;
if (Boolean(user)) {
// Match Password
bcrypt.compare(password, user.password, (err, isMatch) => {
if (err) return err;
if (isMatch) {
const token = jwt.sign({
id: user._id,
username: user.username
}, config.jwtSecret);
res.json({ token, success: 'success' })
} else {
res.json({ errors: { invalidCredentials: 'Invalid Username or Password' } });
}
});
} else {
res.json({ errors: { invalidCredentials: 'Invalid Username or Password' } });
}
});
}
} catch (error) {
res.status(400).json({error})
}
}


const userSignup = async (req, res) => {
try {
const name = req.body.name || '';
const username = req.body.username || '';
const email = req.body.email || '';
const password = req.body.password || '';
const confirmPassword = req.body.confirmPassword || '';

const reqBody = { name, username, email, password, confirmPassword };

let errors = {};
Object.keys(reqBody).forEach(async field => {
if (reqBody[field] === '') {
errors = { ...errors, [field]: 'This field is required' }
}
if (field === 'username' || field === 'email') {
const value = reqBody[field];
const { error, isUnique } = await checkUserUniqueness(field, value);
if (!isUnique) {
errors = { ...errors, ...error };
}
}
if (field === 'email' && !validateEmail(reqBody[field])) {
errors = { ...errors, [field]: 'Not a valid Email' }
}
if (field === 'password' && password !== '' && password < 4) {
errors = { ...errors, [field]: 'Password too short' }
}
if (field === 'confirmPassword' && confirmPassword !== password) {
errors = { ...errors, [field]: 'Passwords do not match' }
}
});

if (Object.keys(errors).length > 0) {
res.json({ errors });
} else {
const newUser = new User({
name: name,
username: username,
email: email,
password: password
});

// Generate the Salt
bcrypt.genSalt(10, (err, salt) => {
if (err) return err;
// Create the hashed password
bcrypt.hash(newUser.password, salt, (err, hash) => {
if (err) return err;
newUser.password = hash;
// Save the User
newUser.save(function (err) {
if (err) return err
res.json({ success: 'success' });
});
});
});
}
} catch (error) {
res.status(400).json({error})
}
}


module.exports = {
userLogin,
userSignup,
userValidate
}
Loading