From b5f26e81ff9dff97d67d1a4df839598e15855e42 Mon Sep 17 00:00:00 2001 From: Andrew Hayward Date: Wed, 19 Jun 2013 14:20:09 +0100 Subject: [PATCH 1/4] Overhauling user sign-up flow Allowing users to restart sign-up, and go back in the process should they need to. Fixes second item in #621 --- controllers/auth.js | 90 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 73 insertions(+), 17 deletions(-) diff --git a/controllers/auth.js b/controllers/auth.js index 4f1a057..5fa02db 100644 --- a/controllers/auth.js +++ b/controllers/auth.js @@ -184,6 +184,16 @@ function processInitialLearnerSignup (req, res, next) { res.render('auth/signup.html', signup); } + function finish (user) { + signup.state = user.underage ? 'child' : 'more'; + signup.profileId = user.id; + signup.passwordGenerated = false; + signup.password = signup.generatedPassword = generatePassword(); + + req.session.signup = signup; + res.redirect(303, '/signup'); + } + var errors = []; var isValidUsername = validateUsername(signup.username); @@ -207,25 +217,37 @@ function processInitialLearnerSignup (req, res, next) { // race conditions on usernames - even if the username does not exist now, it may // well have been created by the time sign-up is complete. // This will fail if the username is already being used - learners.create({ - username: normalizedUsername, - password: '', - underage: underage, - birthday: birthday - }) - .error(function(err) { - // Did try a `findOrCreate`, but couldn't get `isNewRecord` to work - if (err.code === 'ER_DUP_ENTRY') + learners.find({where: {username: normalizedUsername}}) + .error(fail) + .success(function(user) { + if (!user) { + if (signup.profileId) { + // Releasing previous user profile, which they might have created if + // they've gone backwards in the sign-up process and started with + // another username + learners.find(signup.profileId).success(function(profile) { + if (profile && !profile.complete) + profile.destroy(); + }); + } + + return learners.create({ + username: normalizedUsername, + password: '', + underage: underage, + birthday: birthday + }).error(fail).success(finish); + } + + if (user.id !== signup.profileId) return fail(new FieldError('username', 'This username is already in use')); - return fail(err); - }) - .success(function(user) { - signup.state = underage ? 'child' : 'more'; - signup.passwordGenerated = false; - signup.password = signup.generatedPassword = generatePassword(); - req.session.signup = signup; - res.redirect(303, '/signup'); + user.updateAttributes({ + underage: underage, + birthday: birthday + }).error(fail).success(function() { + finish(user); + }); }); } @@ -734,6 +756,9 @@ module.exports = function (app) { if (signup.state === 'more') return res.render('auth/signup-next.html', signup); + if (signup.state === 'initial') + return res.render('auth/signup.html', signup); + req.session.signup = signup = { example: usernames.generate() }; @@ -744,6 +769,37 @@ module.exports = function (app) { app.post('/signup', function (req, res, next) { var signup = req.session.signup || {}; + if (req.body['action'] === 'go-back') { + var form = req.body; + + 'first_name' in form && (signup.first_name = (form.first_name||'').trim()); + 'last_name' in form && (signup.last_name = (form.last_name||'').trim()); + 'email' in form && (signup.email = (form.email||'').trim()); + 'parent_email' in form && (signup.parent_email = (form.parent_email||'').trim()); + + 'school' in form && (signup.school = (form.school||'').trim()); + 'studentId' in form && (signup.studentId = (form.studentId||'').trim()); + 'gender' in form && (signup.gender = form.gender || null); + 'raceEthnicity' in form && (signup.raceEthnicity = form.raceEthnicity); + 'zipCode' in form && (signup.zipCode = (form.zipCode||'').trim()); + + signup.state = 'initial'; + + req.session.signup = signup; + return res.redirect('/signup'); + } + + if (req.body['action'] === 'restart') { + return learners.find(signup.profileId) + .complete(function(err, profile) { + if (profile && !profile.complete) + profile.destroy(); + + delete req.session.signup; + return res.redirect('/signup'); + }); + } + if (signup.state === 'child') return processChildLearnerSignup(req, res, next); From dde8c37dcd21d33409be1ecdcb1c04b7f3f10e19 Mon Sep 17 00:00:00 2001 From: Andrew Hayward Date: Wed, 19 Jun 2013 14:21:32 +0100 Subject: [PATCH 2/4] Adding 'go back/restart' buttons to sign-up forms --- views/auth/signup-next-child.html | 3 +++ views/auth/signup-next.html | 3 +++ 2 files changed, 6 insertions(+) diff --git a/views/auth/signup-next-child.html b/views/auth/signup-next-child.html index f59a614..a85f971 100644 --- a/views/auth/signup-next-child.html +++ b/views/auth/signup-next-child.html @@ -6,6 +6,9 @@ {% block content %}
+ + +

diff --git a/views/auth/signup-next.html b/views/auth/signup-next.html index d08a39d..84a2126 100644 --- a/views/auth/signup-next.html +++ b/views/auth/signup-next.html @@ -6,6 +6,9 @@ {% block content %} + + +

Fields marked * are optional
From 35b31634f5c9fb4deeaa5fd4143ff7b4aa699e74 Mon Sep 17 00:00:00 2001 From: Andrew Hayward Date: Wed, 19 Jun 2013 14:43:01 +0100 Subject: [PATCH 3/4] Tidying date entry inputs given extra space --- views/auth/signup.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/views/auth/signup.html b/views/auth/signup.html index d809d7d..410f611 100644 --- a/views/auth/signup.html +++ b/views/auth/signup.html @@ -23,7 +23,7 @@
- {% for month in months %} @@ -35,7 +35,7 @@ {{ day }} {% endfor %} - {% for year in range(2012, 1979, (-1)) %} From a9dc9f38340982f5a190c50d1864f659014646e0 Mon Sep 17 00:00:00 2001 From: Andrew Hayward Date: Wed, 19 Jun 2013 14:43:32 +0100 Subject: [PATCH 4/4] Adding in option to allow deselection of ethnicity --- views/auth/signup-next.html | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/views/auth/signup-next.html b/views/auth/signup-next.html index 84a2126..36e9325 100644 --- a/views/auth/signup-next.html +++ b/views/auth/signup-next.html @@ -91,6 +91,10 @@ {{ ethnicity }} {% endfor %} + {% if _fields.raceEthnicity.message -%} {{ _fields.raceEthnicity.message }}