-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.routes.js
86 lines (78 loc) · 2.7 KB
/
auth.routes.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const helpers = require('./helpers');
const jwt = require('jsonwebtoken');
const config = require('./config');
const authRouter = new express.Router();
const User = mongoose.model('User');
// configure the body parser
authRouter.use(bodyParser.urlencoded({ extended: true }));
authRouter.use(bodyParser.json());
authRouter.post('/login', loginPost);
authRouter.post('/signup', signUpPost);
module.exports = authRouter;
function loginPost(req, res) {
const email = req.body.email;
const password = req.body.password;
if (!email) {
helpers.returnFail(res, 400, 'email is a required field');
} else if (!password) {
helpers.returnFail(res, 400, 'password is a required field');
} else {
User.findOne({ email: email })
.exec()
.then(function (user) {
if (!user) return helpers.returnFail(res, 404, 'No such user exists.');
return user.comparePassword(password)
.then(function (result) {
if (result) {
// do this if it matches
const token = jwt.sign(user, config.secret, {
// token only valid for 1 hour
expiresIn: '1h'
});
const msg = 'Login succeeded';
return helpers.returnSuccess(res, 200, msg, { token: token });
}
// do this if it doesn't
return helpers.returnFail(res, 401, 'Wrong credentials.');
})
})
.catch(function (err) {
console.log(err.stack);
helpers.returnFail(res, 500, 'Failure during finding user', { err: err });
});
}
}
function signUpPost(req, res) {
const email = req.body.email;
const password = req.body.password;
if (!email) {
helpers.returnFail(res, 400, 'email is a required field');
} else if (!password) {
helpers.returnFail(res, 400, 'password is a required field');
} else {
// Check if user already exists in the database
User.findOne({ email: email })
.exec()
.then(function (user) {
if (user) return helpers.returnFail(res, 400, 'user already exists');
// Create the user and save in the database
const newUser = new User({
email: email,
password: password
});
console.log('newUser');
console.log(newUser);
return newUser.save();
})
.then(function () {
return helpers.returnSuccess(res, 201, 'account created successfully for ' + email, {});
})
.catch(function (err) {
console.log(err.stack);
return helpers.returnFail(res, 500, 'failure during create user', { err: err });
})
}
}