Skip to content

Commit 5d80adf

Browse files
committed
check email exists in database
1 parent 32c6f70 commit 5d80adf

File tree

2 files changed

+67
-27
lines changed

2 files changed

+67
-27
lines changed

routes/auth.js

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,36 @@
11
const router = require('express').Router();
22
const User = require('../model/user');
3-
const Joi = require ('joi')
3+
const {
4+
RegisterValidation
5+
} = require('../validation');
46

5-
const schema = Joi.object({
6-
name: Joi.string()
7-
.alphanum()
8-
.min(3)
9-
.max(30)
10-
.required(),
11-
password: Joi.string()
12-
.pattern(new RegExp('^[a-zA-Z0-9]{3,30}$')),
13-
repeat_password: Joi.ref('password'),
14-
email: Joi.string()
15-
.email({ minDomainSegments: 2, tlds: { allow: ['com', 'net'] } })
16-
17-
18-
});
197
router.post('/register', async (req, res) => {
208
// Validate the data before we make a user
21-
const { error } = schema.validate(req.body);
22-
if(error) return res.status(400).send(error.details[0].message);
23-
const user = new User({
24-
name: req.body.name,
25-
email: req.body.email,
26-
password: req.body.password,
27-
date: req.body.date
9+
const {
10+
error
11+
} = RegisterValidation(req.body);
12+
if (error) return res.status(400).send(error.details[0].message);
13+
//create new user
14+
const emailExist = await User.findOne({
15+
email: req.body.email
2816
});
29-
try{
17+
if (emailExist) return res.status(400).send('Email already exists');
18+
19+
const user = new User({
20+
name: req.body.name,
21+
email: req.body.email,
22+
password: req.body.password,
23+
date: req.body.date
24+
});
25+
try {
26+
//save user to MongoDB
3027
const savedUser = await user.save();
3128
res.send(savedUser);
32-
}catch(err){
33-
res .status(400).send(err);
34-
}
35-
29+
} catch (err) {
30+
//res.status in case of error
31+
res.status(400).send(err);
32+
}
33+
3634
});
3735

3836
module.exports = router;

validation.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const Joi = require('joi');
2+
3+
//validate users
4+
5+
const RegisterValidation = (data) => {
6+
const schema = Joi.object({
7+
name: Joi.string()
8+
.alphanum()
9+
.min(3)
10+
.max(30)
11+
.required(),
12+
password: Joi.string(),
13+
//.pattern(new RegExp('^[a-zA-Z0-9]{3,30}$')),
14+
//repeat_password: Joi.ref('password'),
15+
email: Joi.string()
16+
.email({
17+
minDomainSegments: 2,
18+
tlds: {
19+
allow: ['com', 'net']
20+
}
21+
})
22+
});
23+
return Joi.isSchema(data, schema);
24+
}
25+
const LoginValidation = (data) => {
26+
const schema = Joi.object({
27+
password: Joi.string(),
28+
//.pattern(new RegExp('^[a-zA-Z0-9]{3,30}$')),
29+
//repeat_password: Joi.ref('password'),
30+
email: Joi.string()
31+
.email({
32+
minDomainSegments: 2,
33+
tlds: {
34+
allow: ['com', 'net']
35+
}
36+
})
37+
});
38+
return Joi.isSchema(data, schema);
39+
}
40+
41+
module.exports.RegisterValidation = RegisterValidation;
42+
module.exports.LoginValidation = LoginValidation;

0 commit comments

Comments
 (0)