Skip to content
Browse files

Accounted for possible uniqueness error appearing in the registerUser…

… step of the password module.
  • Loading branch information...
1 parent 3a3e59b commit 9472fe2dcb794f9f4fef19a3a8efe44ba60a1946 @bnoguchi committed
Showing with 30 additions and 0 deletions.
  1. +15 −0 README.md
  2. +15 −0 lib/password.js
View
15 README.md
@@ -304,9 +304,24 @@ everyauth.password
.registerUser( function (newUserAttributes) {
// This step is only executed if we pass the validateRegistration step without
// any errors.
+ //
// Returns a user (or a Promise that promises a user) after adding it to
// some user store. You can also do things here like registration validation
// and re-directing back to the registration page upon invalid registration
+ //
+ // As an edge case, sometimes your database may make you aware of violation
+ // of the unique login index, so if this error is sent back in an async
+ // callback, then you can just return that error as a single element array
+ // containing just that error message, and everyauth will automatically handle
+ // that as a failed registration. Again, you will have access to this error via
+ // the `errors` local in your register view jade template.
+ // e.g.,
+ // var promise = this.Promise();
+ // User.create(newUserAttributes, function (err, user) {
+ // if (err) return promise.fulfill([err]);
+ // promise.fulfill(user);
+ // });
+ // return promise;
})
.registerSuccessRedirect('/'); // Where to redirect to after a successful registration
View
15 lib/password.js
@@ -137,7 +137,11 @@ everyModule.submodule('password')
.step('registerUser')
.description('Creates and returns a new user with newUserAttributes')
.accepts('newUserAttributes')
+ .promises('userOrErrors')
+ .step('extractUserOrHandleErrors') // User registration may throw an error if DB detects a non-unique value for login
+ .accepts('userOrErrors newUserAttributes')
.promises('user')
+ .canBreakTo('registrationFailSteps')
.step('getSession')
.step('addToSession')
.step('respondToRegistrationSucceed')
@@ -168,6 +172,17 @@ everyModule.submodule('password')
return this.breakTo('registrationFailSteps', req, res, errors, user);
}
})
+ .extractUserOrHandleErrors( function (userOrErrors, newUserAttributes) {
+ var errors, user;
+ if (Array.isArray(userOrErrors)) {
+ errors = userOrErrors;
+ user = newUserAttributes;
+ delete user.password;
+ return this.breakTo('registrationFailSteps', req, res, errors, user);
+ }
+ user = userOrErrors;
+ return user;
+ })
.respondToRegistrationSucceed( function (res, user) {
res.writeHead(303, {'Location': this.registerSuccessRedirect()});
res.end();

0 comments on commit 9472fe2

Please sign in to comment.
Something went wrong with that request. Please try again.