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

users final #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
42 changes: 42 additions & 0 deletions routes/api/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,48 @@ const validator = require('../../validations/userValidations')
// cosole.log(req.body);
//});

router.post('/register', async (req, res) => {

try {

const isValidated = validator.registerValidation(req.body);

if (isValidated.error) return res.status(400).send({ error: isValidated.error.details[0].message });

const { email, age, name, password } = req.body;

const user = await User.findOne({ email });

if (user) return res.status(400).json({ email: 'Email already exists' });

const salt = bcrypt.genSaltSync(10);

const hashedPassword = bcrypt.hashSync(password, salt);

const newUser = new User({

name,

password: hashedPassword,

email,

age,

});

await User.create(newUser);

res.json({ msg: 'User created successfully', data: newUser });

} catch (error) {

res.status(422).send({ error: 'Can not create user' });

}

});

router.post('/', (req,res) => {
try {
const isValidated = validator.createValidation(req.body)
Expand Down