-
Notifications
You must be signed in to change notification settings - Fork 13
/
route.js
127 lines (108 loc) · 3.03 KB
/
route.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// vendor library
var passport = require('passport');
var bcrypt = require('bcrypt-nodejs');
// custom library
// model
var Model = require('./model');
// index
var index = function(req, res, next) {
if(!req.isAuthenticated()) {
res.redirect('/signin');
} else {
var user = req.user;
if(user !== undefined) {
user = user.toJSON();
}
res.render('index', {title: 'Home', user: user});
}
};
// sign in
// GET
var signIn = function(req, res, next) {
if(req.isAuthenticated()) res.redirect('/');
res.render('signin', {title: 'Sign In'});
};
// sign in
// POST
var signInPost = function(req, res, next) {
passport.authenticate('local', { successRedirect: '/',
failureRedirect: '/signin'}, function(err, user, info) {
if(err) {
return res.render('signin', {title: 'Sign In', errorMessage: err.message});
}
if(!user) {
return res.render('signin', {title: 'Sign In', errorMessage: info.message});
}
return req.logIn(user, function(err) {
if(err) {
return res.render('signin', {title: 'Sign In', errorMessage: err.message});
} else {
return res.redirect('/');
}
});
})(req, res, next);
};
// sign up
// GET
var signUp = function(req, res, next) {
if(req.isAuthenticated()) {
res.redirect('/');
} else {
res.render('signup', {title: 'Sign Up'});
}
};
// sign up
// POST
var signUpPost = function(req, res, next) {
var user = req.body;
var usernamePromise = null;
usernamePromise = new Model.User({username: user.username}).fetch();
return usernamePromise.then(function(model) {
if(model) {
res.render('signup', {title: 'signup', errorMessage: 'username already exists'});
} else {
//****************************************************//
// MORE VALIDATION GOES HERE(E.G. PASSWORD VALIDATION)
//****************************************************//
var password = user.password;
var hash = bcrypt.hashSync(password);
var signUpUser = new Model.User({username: user.username, password: hash});
signUpUser.save().then(function(model) {
// sign in the newly registered user
signInPost(req, res, next);
});
}
});
};
// sign out
var signOut = function(req, res, next) {
if(!req.isAuthenticated()) {
notFound404(req, res, next);
} else {
req.logout();
res.redirect('/signin');
}
};
// 404 not found
var notFound404 = function(req, res, next) {
res.status(404);
res.render('404', {title: '404 Not Found'});
};
// export functions
/**************************************/
// index
module.exports.index = index;
// sigin in
// GET
module.exports.signIn = signIn;
// POST
module.exports.signInPost = signInPost;
// sign up
// GET
module.exports.signUp = signUp;
// POST
module.exports.signUpPost = signUpPost;
// sign out
module.exports.signOut = signOut;
// 404 not found
module.exports.notFound404 = notFound404;