Permalink
Browse files

Added password module for login/password authentication.

  • Loading branch information...
1 parent c390d70 commit 3f5a806cfca2444e95c249db07826b7d585bf8cc @bnoguchi committed Apr 5, 2011
Showing with 96 additions and 3 deletions.
  1. +40 −1 README.md
  2. +1 −1 index.js
  3. +4 −1 lib/everymodule.js
  4. +51 −0 lib/password.js
View
@@ -24,12 +24,51 @@ Authentication and authorization (password, facebook, & more) for your node.js C
## Setting up Facebook Connect
- var everyauth = require('everyauth');
+ var everyauth = require('everyauth')
+ , connect = require('connect');
everyauth.facebook
.myHostname('http://localhost:3000')
.appId('YOUR APP ID HERE')
.appSecret('YOUR APP SECRET HERE')
.findOrCreateUser( function (session, accessToken, fbUserMetadata) {
+ // find or create user logic goes here
})
.redirectPath('/');
+
+ var routes = function (app) {
+ // Define your routes here
+ };
+
+ connect(
+ connect.bodyParser()
+ , connect.cookieParser()
+ , connect.session({secret: 'whodunnit'})
+ , everyauth.middleware()
+ , connect.router(routes);
+ ).listen(3000);
+
+## Setting up Password Authentication
+ var everyauth = require('everyauth')
+ , connect = require('connect');
+
+ everyauth.password
+ .myHostname('http://localhost:3000')
+ .appId('YOUR APP ID HERE')
+ .appSecret('YOUR APP SECRET HERE')
+ .findOrCreateUser( function (session, accessToken, fbUserMetadata) {
+ // find or create user logic goes here
+ })
+ .redirectPath('/');
+
+ var routes = function (app) {
+ // Define your routes here
+ };
+
+ connect(
+ connect.bodyParser()
+ , connect.cookieParser()
+ , connect.session({secret: 'whodunnit'})
+ , everyauth.middleware()
+ , connect.router(routes);
+ ).listen(3000);
View
@@ -65,7 +65,7 @@ everyauth
});
everyauth.modules = {};
-includeModules = {oauth: false, facebook: true}
+includeModules = {password: true, oauth: false, facebook: true}
for (var name in includeModules) {
var mod =
everyauth[name] =
View
@@ -119,7 +119,10 @@ var everyModule = module.exports = {
var self = this;
for (var method in this._routes) {
this._routes[method].forEach( function (routeAlias) {
- app[method](self[routeAlias](), self.routeHandler(method, routeAlias));
+ var path = self[routeAlias]();
+ if (!path)
+ throw new Error('You have not defined a path for the route alias ' + routeAlias + '.');
+ app[method](path, self.routeHandler(method, routeAlias));
});
}
}
View
@@ -0,0 +1,51 @@
+var everyModule = require('./everymodule');
+
+var password = module.exports =
+everyModule.submodule('password')
+ .configurable('loginView redirectPath')
+ .get('loginPath')
+ .step('displayLogin')
+ .accepts('req res')
+ .promises(null)
+ .post('authPath')
+ .step('extractLoginPassword')
+ .accepts('req res')
+ .promises('login password')
+ .step('authenticate')
+ .accepts('login password')
+ .promises('didSucceed')
+ .step('findUser')
+ .accepts('didSucceed login')
+ .promises('user')
+ .step('getSession')
+ .accepts('req')
+ .promises('session')
+ .step('addToSession')
+ .accepts('session user')
+ .promises(null)
+ .step('sendResponse')
+ .accepts('res didSucceed')
+ .promises(null)
+ .displayLogin( function (req, res) {
+ if (res.render) {
+ res.render(this.loginView());
+ } else {
+ res.writeHead(200, {'Content-Type': 'text/html'});
+ res.end(this.loginView());
+ }
+ })
+ .extractLoginPassword( function (req, res) {
+ return [req.body.login, req.body.password];
+ })
+ .getSession( function (req) {
+ return req.session;
+ })
+ .addToSession( function (sess, user) {
+ var _auth = sess.auth || (sess.auth = {});
+ _auth.userId = user.id;
+ _auth.loggedIn = !!user;
+ })
+ .sendResponse( function (res, didSucceed) {
+ res.writeHead(303, {'Location': this.redirectPath()});
+ res.end();
+ });

0 comments on commit 3f5a806

Please sign in to comment.