-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
63 lines (59 loc) · 2.05 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
var express = require('express');
var app = express();
require('./config/app')(app);
require('./config/authentication')(app);
require('./config/database');
var User = require('./models/users');
app.post('/api/signup', function(req, res){
var msg = "";
User.findOne({'username': req.body.username}, function(err, Userb){
if(err){ console.log(err); }
if(Userb){
msg = "user already exists";
res.send({success:false, msg: msg, userID:null});
}
else{
var newUser = new User();
newUser.email = req.body.email;
newUser.username = req.body.username;
newUser.password = newUser.generateHash(req.body.password);
newUser.save(function(err){
if(err){
console.log(err);
msg = err;
} else {
msg = "success!";
res.send({success: true, msg: msg, userID: newUser._id, username: req.body.username});
}
});
}
});
});
app.post('/api/login', function(req,res){
var msg = "";
console.log('post login');
User.findOne({'username': req.body.username}, function(err,user){
if(err) {
console.log(err);
msg = err;
res.send({success:false, msg: msg, userID: null, username: req.body.username});
}
else if (!user){
msg = "user does not exist";
res.send({success:false, msg: msg, userID: null, username: req.body.username});
}
else if (!user.validPassword(req.body.password)){
msg = "incorrect password";
res.send({success:false, msg: msg, userID: null, username:req.body.username});
}
else{
console.log("success");
res.send({success:true, msg: "login success!", userID: user._id, username: req.body.username});
}
});
});
var http = require('http');
var server = http.createServer(app);
server.listen(31000, function(){
console.log("listening on port 31000");
});