Created
February 9, 2011 18:50
-
-
Save alfredwesterveld/819008 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // app.js | |
| var express = require('express'), | |
| app = express.createServer(), | |
| RedisStore = require('connect-redis'), | |
| redis = require('redis'), | |
| client = redis.createClient(); | |
| var auth = require('connect-auth'); | |
| var userStrategy = require('./userStrategy'); | |
| app.configure(function() { | |
| app.use(express.cookieDecoder()); | |
| app.use(express.session( { | |
| key : "123", | |
| secret : "123", | |
| store : new RedisStore( { | |
| maxAge : 10080000 | |
| }) | |
| })); | |
| app.use(express.bodyDecoder()); | |
| app.use(express.logger()); | |
| app.use(express.staticProvider(__dirname + '/public')); | |
| app.use(auth(userStrategy())); | |
| }); | |
| app.get('/signin', function(request, response){ | |
| response.send("<html><body><form action='/auth/form_callback' method='post'> \n\ | |
| <label for='user'>Name</label><input type='text' name='user' id='user'/><br/> \n\ | |
| <label for='password'>Password</label><input type='password' name='password' id='password'/> \n\ | |
| <input type='submit'/> \n\ | |
| </form></body</html>"); | |
| }); | |
| app.get('/', function(req, res, params) { | |
| req.authenticate(['user'], function(error, authenticated) { | |
| res.writeHead(200, { 'Content-Type': 'text/plain' }); | |
| res.end('Hello World'); | |
| }); | |
| }); | |
| app.get('/logout', function(req, res, params) { | |
| req.logout(); | |
| res.send(''); | |
| }); | |
| app.listen(3000, 'localhost'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var express = require('express'); | |
| var url = require('url'); | |
| module.exports = function(options) { | |
| options = options || {}; | |
| var that = {}; | |
| var my = {}; | |
| that.name = "user"; | |
| function failed_validation(request, response, uri) { | |
| response.writeHead(303, { | |
| 'Location' : "/signin" | |
| }); | |
| response.end(''); | |
| } | |
| function validate_credentials(executionScope, request, response, callback) { | |
| if (request.body && request.body.user && request.body.password) { | |
| if( request.body.user == 'foo' && request.body.password == 'bar' ) { | |
| executionScope.success( {name:request.body.user}, callback ); | |
| } else { | |
| executionScope.fail(callback); | |
| } | |
| } else { | |
| failed_validation( request, response ); | |
| } | |
| }; | |
| that.authenticate = function(request, response, callback) { | |
| if (request.body && request.body.user && request.body.password) { | |
| validate_credentials(this, request, response, callback); | |
| } else { | |
| failed_validation(request, response, request); | |
| } | |
| }; | |
| that.setupRoutes = function(server) { | |
| server.use('/', express.router(function routes(app) { | |
| app.post('/auth/form_callback', function(request, response, next) { | |
| request.authenticate( [that.name], function(error, authenticated) { | |
| response.writeHead(303, { 'Location': '/' }); | |
| response.end(''); | |
| }); | |
| }); | |
| })); | |
| }; | |
| return that; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment