/
users.server.controller.js
165 lines (144 loc) · 4.78 KB
/
users.server.controller.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Invoke 'strict' JavaScript mode
'use strict';
// Load the module dependencies
var User = require('mongoose').model('User'),
passport = require('passport');
// Create a new error handling controller method
var getErrorMessage = function(err) {
// Define the error message variable
var message = '';
// If an internal MongoDB error occurs get the error message
if (err.code) {
switch (err.code) {
// If a unique index error occurs set the message error
case 11000:
case 11001:
message = 'Username already exists';
break;
// If a general error occurs set the message error
default:
message = 'Something went wrong';
}
} else {
// Grab the first error message from a list of possible errors
for (var errName in err.errors) {
if (err.errors[errName].message) message = err.errors[errName].message;
}
}
// Return the message error
return message;
};
// Create a new controller method that renders the signin page
exports.renderSignin = function(req, res, next) {
// If user is not connected render the signin page, otherwise redirect the user back to the main application page
if (!req.user) {
// Use the 'response' object to render the signin page
res.render('signin', {
// Set the page title variable
title: 'Sign-in Form',
// Set the flash message variable
messages: req.flash('error') || req.flash('info')
});
} else {
return res.redirect('/');
}
};
// Create a new controller method that renders the signup page
exports.renderSignup = function(req, res, next) {
// If user is not connected render the signup page, otherwise redirect the user back to the main application page
if (!req.user) {
// Use the 'response' object to render the signup page
res.render('signup', {
// Set the page title variable
title: 'Sign-up Form',
// Set the flash message variable
messages: req.flash('error')
});
} else {
return res.redirect('/');
}
};
// Create a new controller method that creates new 'regular' users
exports.signup = function(req, res, next) {
// If user is not connected, create and login a new user, otherwise redirect the user back to the main application page
if (!req.user) {
// Create a new 'User' model instance
var user = new User(req.body);
var message = null;
// Set the user provider property
user.provider = 'local';
// Try saving the new user document
user.save(function(err) {
// If an error occurs, use flash messages to report the error
if (err) {
// Use the error handling method to get the error message
var message = getErrorMessage(err);
// Set the flash messages
req.flash('error', message);
// Redirect the user back to the signup page
return res.redirect('/signup');
}
// If the user was created successfully use the Passport 'login' method to login
req.login(user, function(err) {
// If a login error occurs move to the next middleware
if (err) return next(err);
// Redirect the user back to the main application page
return res.redirect('/');
});
});
} else {
return res.redirect('/');
}
};
// Create a new controller method that creates new 'OAuth' users
exports.saveOAuthUserProfile = function(req, profile, done) {
// Try finding a user document that was registered using the current OAuth provider
User.findOne({
provider: profile.provider,
providerId: profile.providerId
}, function(err, user) {
// If an error occurs continue to the next middleware
if (err) {
return done(err);
} else {
// If a user could not be found, create a new user, otherwise, continue to the next middleware
if (!user) {
// Set a possible base username
var possibleUsername = profile.username || ((profile.email) ? profile.email.split('@')[0] : '');
// Find a unique available username
User.findUniqueUsername(possibleUsername, null, function(availableUsername) {
// Set the available user name
profile.username = availableUsername;
// Create the user
user = new User(profile);
// Try saving the new user document
user.save(function(err) {
// Continue to the next middleware
return done(err, user);
});
});
} else {
// Continue to the next middleware
return done(err, user);
}
}
});
};
// Create a new controller method for signing out
exports.signout = function(req, res) {
// Use the Passport 'logout' method to logout
req.logout();
// Redirect the user back to the main application page
res.redirect('/');
};
// Create a new controller middleware that is used to authorize authenticated operations
exports.requiresLogin = function(req, res, next) {
// If a user is not authenticated send the appropriate error message
if (!req.isAuthenticated()) {
return res.status(401).send({
message: 'User is not logged in'
});
}
// Call the next middleware
next();
};