aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/controllers/users.js
diff options
context:
space:
mode:
authorDennis Eriksen <dennis.se@gmail.com>2013-05-15 13:17:23 +0200
committerDennis Eriksen <dennis.se@gmail.com>2013-05-15 13:17:23 +0200
commit7ae1fbc3cfbe9ce5939fb068f71af8318d8c9dd7 (patch)
tree4bf4cfed78ed0e64b0adf741ce5e290e3dac942a /controllers/users.js
parentmade view for registerEmail (diff)
downloadDivid-7ae1fbc3cfbe9ce5939fb068f71af8318d8c9dd7.tar.gz
added registerEmail so it works.. also, now it checks to see if email
exists already
Diffstat (limited to 'controllers/users.js')
-rw-r--r--controllers/users.js39
1 files changed, 38 insertions, 1 deletions
diff --git a/controllers/users.js b/controllers/users.js
index 477a85a..fe47cac 100644
--- a/controllers/users.js
+++ b/controllers/users.js
@@ -84,13 +84,50 @@ exports.create = function(req, res) {
exports.authCallback = function(req, res, next) {
// if the user hasn't registered an email, we need to do so
- if (!req.user.email || req.user.email === 'undefined') return res.redirect('/registerEmail');
+ if (!req.user.email || req.user.email === undefined) return res.redirect('/registerEmail');
res.redirect('/dashboard');
}
/**
+ * registerEmail
+ * Will register the users email if they don't have already
+ */
+
+exports.registerEmail = function(req, res) {
+ // in case some user who has alreadu registered an email gets on this page
+ if (req.user.email !== undefined) return res.redirect('/dashboard');
+ res.render('registerEmail', { title: 'Registrer din e-post' });
+}
+
+
+/**
+ * postRegisterEmail
+ */
+
+exports.postRegisterEmail = function(req, res) {
+
+ v.check(req.body.email, 'You need to supply a proper email').isEmail();
+ var errors = v.getErrors();
+ if (errors.length !== 0) return res.status(500).render('error', { title: '500', text: 'Det oppstod en valideringsfeil<br>' + errors, error: errors });
+
+ // first we need to check if the email is already in use
+ User.findOne({ email: req.body.email }, function(err, user) {
+ if (err) return res.status(500).render('error', { title: '500', text: 'En serverfeil oppstod', error: err.stack });
+
+ // if mail is in use..
+ if (user) return res.render('registerEmail', { title: 'Den e-posten er allerede i bruk. Vennligs registrer en annen.' });
+
+ User.update({ _id: req.user._id }, { email: req.body.email }, function(err) {
+ if (err) return res.status(500).render('error', { title: '500', text: 'En serverfeil oppstod', error: err.stack });
+ return res.redirect('/dashboard');
+ });
+ });
+}
+
+
+/**
* postProjectParticipants
* This callback is in this file because it treats users.
*/