Skip to content

Commit 5445fce

Browse files
committed
BE #1 User registration added
1 parent 49d2e88 commit 5445fce

File tree

4 files changed

+131
-3
lines changed

4 files changed

+131
-3
lines changed

models/user.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const mongoose=require('mongoose');
2+
const Schema=mongoose.Schema;
3+
4+
var passportLocalMongoose = require('passport-local-mongoose');
5+
6+
var User = new Schema({
7+
fullname: {
8+
type: String,
9+
default: ''
10+
},
11+
email: {
12+
type: String,
13+
default: ''
14+
}
15+
});
16+
17+
User.plugin(passportLocalMongoose,{
18+
usernameLowerCase:true,
19+
passwordValidator : (password,cb)=>{
20+
if (!password.trim()) {
21+
return cb('Password can not be empty!');
22+
}
23+
else if(password.trim().length<8){
24+
return cb('Password must be at least 8 chars long');
25+
}
26+
// return an empty cb() on success
27+
return cb()
28+
}
29+
});
30+
31+
module.exports = mongoose.model('User', User);

package-lock.json

Lines changed: 55 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
"express": "4.16.1",
1212
"mongodb": "^3.5.7",
1313
"mongoose": "^5.9.14",
14-
"morgan": "1.9.1"
14+
"morgan": "1.9.1",
15+
"passport": "^0.4.1",
16+
"passport-local-mongoose": "^6.0.1"
1517
}
1618
}

routes/users.js

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,49 @@
11
var express = require('express');
22
var router = express.Router();
3+
var User =require('../models/user');
34

45
/* GET users listing. */
5-
router.get('/', function(req, res, next) {
6-
res.send('respond with a resource');
6+
router.get('/', (req, res, next) => {
7+
User.find({})
8+
.then((users)=>{
9+
res.statusCode = 200;
10+
res.setHeader('Content-Type', 'application/json');
11+
res.json(users);
12+
}, (err) => next(err))
13+
.catch((err) => next(err));
714
});
815

16+
router.post('/signup', (req, res, next) => {
17+
User.register(new User({username: req.body.username}),
18+
req.body.password, (err, user) => {
19+
if(err) {
20+
res.statusCode = 500;
21+
res.setHeader('Content-Type', 'application/json');
22+
res.json({err: err});
23+
}
24+
else {
25+
26+
if (req.body.fullname)
27+
user.fullname = req.body.fullname;
28+
if (req.body.email)
29+
user.email = req.body.email;
30+
31+
user.save((err, user) => {
32+
if (err) {
33+
res.statusCode = 500;
34+
res.setHeader('Content-Type', 'application/json');
35+
res.json({err: err});
36+
return ;
37+
}
38+
else{
39+
res.statusCode = 200;
40+
res.setHeader('Content-Type', 'application/json');
41+
res.json({success: true, status: 'Registration Successful!'});
42+
}
43+
});
44+
}
45+
});
46+
});
47+
48+
949
module.exports = router;

0 commit comments

Comments
 (0)