Permalink
Browse files

Improved password.authenticate API to use more intuitive return values.

  • Loading branch information...
1 parent 46a6e09 commit 5097b2deff0be5a360be4ed2cd3f742db6b9a868 @bnoguchi committed May 2, 2011
Showing with 29 additions and 9 deletions.
  1. +7 −6 README.md
  2. +7 −3 example/server.js
  3. +15 −0 lib/password.js
View
@@ -260,23 +260,23 @@ everyauth.password
.postLoginPath('/login') // Uri path that your login form POSTs to
.loginView('a string of html; OR the name of the jade/etc-view-engine view')
.authenticate( function (login, password) {
- // Either, we return an array [user, errors] if doing sync auth.
- // Or, we return a Promise that can fulfill to promise.fulfill(user, errors).
+ // Either, we return a user or an array of errors if doing sync auth.
+ // Or, we return a Promise that can fulfill to promise.fulfill(user) or promise.fulfill(errors)
// `errors` is an array of error message strings
//
// e.g.,
// Example 1 - Sync Example
// if (usersByLogin[login] && usersByLogin[login].password === password) {
- // return [usersByLogin[login], []];
+ // return usersByLogin[login];
// } else {
- // return [null, ['Login failed']]
+ // return ['Login failed'];
// }
//
// Example 2 - Async Example
// var promise = this.Promise()
// YourUserModel.find({ login: login}, function (err, user) {
- // if (err) return promise.fulfill(null, [err]);
- // promise.fulfill(user, []);
+ // if (err) return promise.fulfill([err]);
+ // promise.fulfill(user);
// }
// return promise;
})
@@ -306,6 +306,7 @@ everyauth.password
// some user store. You can also do things here like registration validation
// and re-directing back to the registration page upon invalid registration
})
+ .registerSuccessRedirect('/'); // Where to redirect to after a successful registration
var routes = function (app) {
// Define your routes here
View
@@ -44,10 +44,14 @@ everyauth
.postLoginPath('/login')
.loginView('login.jade')
.authenticate( function (login, password) {
+ var errors = [];
+ if (!login) errors.push('Missing login');
+ if (!password) errors.push('Missing password');
+ if (errors.length) return errors;
var user = usersByLogin[login];
- if (!user) return [null, ['Login failed']];
- if (user.password !== password) return [null, ['Login failed']];
- return [user, []];
+ if (!user) return ['Login failed'];
+ if (user.password !== password) return ['Login failed'];
+ return user;
})
.getRegisterPath('/register')
View
@@ -38,22 +38,37 @@ everyModule.submodule('password')
.promises('login password')
.step('authenticate')
.accepts('login password req res')
+ .promises('userOrErrors')
+ .step('interpretUserOrErrors')
+ .description('Pipes the output of the step `authenticate` into either the `user` or `errors` param')
+ .accepts('userOrErrors')
.promises('user errors')
.step('getSession')
+ .description('Retrieves the session of the incoming request and returns in')
.accepts('req')
.promises('session')
.step('addToSession')
+ .description('Adds the user to the session')
.accepts('session user errors')
.promises(null)
.step('respondToLoginSucceed') // TODO Rename to maybeRespondToLoginSucceed ?
+ .description('Execute a HTTP response for a successful login')
.accepts('res user')
.promises(null)
.step('respondToLoginFail')
+ .description('Execute a HTTP response for a failed login')
.accepts('res errors login')
.promises(null)
.extractLoginPassword( function (req, res) {
return [req.body[this.loginFormFieldName()], req.body[this.passwordFormFieldName()]];
})
+ .interpretUserOrErrors( function (userOrErrors) {
+ if (Array.isArray(userOrErrors)) {
+ return [null, userOrErrors]; // We have an array of errors
+ } else {
+ return [userOrErrors, []]; // We have a user
+ }
+ })
.getSession( function (req) {
return req.session;
})

0 comments on commit 5097b2d

Please sign in to comment.