-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.js
75 lines (66 loc) · 2.31 KB
/
user.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
/**
* Module dependencies.
*/
var passport = require('passport'),
User = require('./models/user'),
activationRequest = require('./emailActivation/controllers').request,
bcrypt = require('bcrypt');
exports.info = [
passport.authenticate('bearer', { session: false }),
function(req, res) {
// req.authInfo is set using the `info` argument supplied by
// `BearerStrategy`. It is typically used to indicate scope of the token,
// and used in access control checks. For illustrative purposes, this
// example simply returns the scope in the response.
// var scope = req.authInfo.scope;
res.json({ user_id: req.user.id, username: req.user.username, name: req.user.username, email: req.user.email, isAdmin: req.user.isAdmin, scope: req.authInfo.scope })
}
]
exports.create = function (req, res) {
if (!req.body.username || !req.body.password || !req.body.email) {
req.flash('error', 'You need to fill in username, password and email');
return res.render('register');
}
User.findOne({
$or: [
{username: req.body.username},
{email: req.body.email}
]
}, function (err, user) {
if (err) {
req.flash('error', 'Something went wrong, try again later');
return res.redirect('/register');
} else if (user) {
req.flash('error', 'Username or email already in use, try something else');
return res.redirect('/register');
}
bcrypt.hash(req.body.password, 8, function(err, hash) {
if (err) {
req.flash('error', 'Something went wrong, try again later');
return res.render('register');
}
user = new User({
username: req.body.username,
username_lower: req.body.username.toLowerCase(),
password: hash,
email: req.body.email,
isAdmin: false
});
user.save(function(err, user) {
if (err) {
req.flash('error', 'Something went wrong, try again later');
return res.render('register');
}
activationRequest(req, res, user._id)
.then(()=>{
req.flash('success', 'User created, please confirm email');
return res.redirect('/registered');
})
.catch((err)=>{
req.flash('error', err);
return res.render('register');
});
});
});
});
};