aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/controllers
diff options
context:
space:
mode:
authorDennis Eriksen <dennis.se@gmail.com>2013-05-16 00:14:40 +0200
committerDennis Eriksen <dennis.se@gmail.com>2013-05-16 00:14:40 +0200
commit996db774a7e901b39c7389070c7c3d08e0970c57 (patch)
tree8700695822a3f4349ac80757a3742e82440484e8 /controllers
parentjust some small changes (diff)
downloadDivid-996db774a7e901b39c7389070c7c3d08e0970c57.tar.gz
added function for claiming invites!
Diffstat (limited to 'controllers')
-rw-r--r--controllers/users.js42
1 files changed, 40 insertions, 2 deletions
diff --git a/controllers/users.js b/controllers/users.js
index bd8c2b4..befb8a4 100644
--- a/controllers/users.js
+++ b/controllers/users.js
@@ -58,7 +58,7 @@ exports.signin = function(req, res) {
* Signup
*/
exports.signup = function(req, res) {
- res.render('signup', { title: 'Registrer deg' });
+ res.render('signup', { title: 'Registrer deg', invite: false });
}
/**
@@ -170,7 +170,7 @@ exports.postProjectParticipants = function(req, res) {
newUser.email = mailAddress;
newUser.status = 1;
newUser.password = newUser.generateRandomToken(32);
- newUser.randomToken = newUser.generateRandomToken(10);
+ newUser.randomToken = newUser.generateRandomToken(10, true);
newUser.save(function(err) {
if (err) return res.render('projectParticipants', { title: 'Nytt prosjekt - en feil oppstod', loggedin: true });
console.log('made new user ' + newUser._id);
@@ -234,10 +234,48 @@ exports.postProjectParticipants = function(req, res) {
exports.claimInvite = function(req, res) {
+ // first we need to check if the invite is valid!
+ User.findOne({ randomToken: sanitize(req.params.randomToken).escape(), status: 1 }, function(err, user) {
+ if (err) return res.status(500).render('error', { title: '500', text: 'En serverfeil oppstod', error: err.stack });
+ if (!user) return res.render('error', { title: 'This invite does not exist', text: 'Invitasjonen din er ugyldig' });
+
+ res.render('signup', {
+ invite: true,
+ title: 'Registrer deg!',
+ email: user.email }
+ );
+ });
+
+
}
exports.postClaimInvite = function(req, res) {
+ User.findOne({ randomToken: sanitize(req.params.randomToken).escape(), status: 1 }, function(err, user) {
+ if (err) return res.status(500).render('error', { title: '500', text: 'En serverfeil oppstod', error: err.stack });
+ if (!user) return res.render('error', { title: 'This invite does not exist', text: 'Invitasjonen din er ugyldig' });
+
+ v.check(req.body.password).notEmpty();
+ v.check(req.body.name).notEmpty();
+ v.check(req.body.username).notEmpty();
+
+ errors = v.getErrors();
+ if (errors.length !== 0) return res.status(500).render('error', { title: '500', text: 'Det oppstod en valideringsfeil<br>' + errors, error: errors });
+
+ user.name = sanitize(req.body.name).escape();
+ user.username = sanitize(req.body.username).escape();
+ user.password = req.body.password;
+ user.provider = 'local';
+ user.status = 3;
+ user.randomToken = '';
+ user.save(function(err) {
+ if (err) return res.render('signup', { errors: err.errors, user: user });
+ req.logIn(user, function(err) {
+ if (err) return next(err);
+ return res.redirect('/dashboard');
+ });
+ });
+ });
}