├── public └── stylesheets │ ├── style.styl │ └── style.css ├── routes ├── index.js └── profile.js ├── views ├── profile.ejs └── index.ejs ├── README.md ├── package.json ├── LICENSE └── app.js /public/stylesheets/style.styl: -------------------------------------------------------------------------------- 1 | body 2 | padding: 50px 3 | font: 14px "Lucida Grande", Helvetica, Arial, sans-serif 4 | a 5 | color: #00B7FF -------------------------------------------------------------------------------- /routes/index.js: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * GET home page. 4 | */ 5 | 6 | exports.index = function(req, res){ 7 | res.render('index', { title: 'Commons' }); 8 | }; -------------------------------------------------------------------------------- /public/stylesheets/style.css: -------------------------------------------------------------------------------- 1 | body { 2 | padding: 50px; 3 | font: 14px "Lucida Grande", Helvetica, Arial, sans-serif; 4 | } 5 | a { 6 | color: #00b7ff; 7 | } 8 | -------------------------------------------------------------------------------- /routes/profile.js: -------------------------------------------------------------------------------- 1 | exports.show = function(req, res){ 2 | //console.log(req.session); 3 | res.render('profile', { title: 'Commons', user: req.session.passport.user.displayName }); 4 | }; -------------------------------------------------------------------------------- /views/profile.ejs: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 |Welcome to <%= user %>'s profile
10 | 11 | -------------------------------------------------------------------------------- /views/index.ejs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |Welcome to <%= title %>
10 | Login with Facebook 11 | 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | express-passport-redis-template 2 | =============================== 3 | 4 | This is a tiny tiny Node.js application which uses Passport Facebook authentication and Redis session storage to demonstrate log-in and persistent sessions. 5 | 6 | The application is by no means exciting, but a quick clone or fork and plugging in the location of your Redis server and Facebook app and you are on your way 7 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "express-passport-redis-template", 3 | "version": "0.0.1", 4 | "private": true, 5 | "scripts": { 6 | "start": "node app.js" 7 | }, 8 | "dependencies": { 9 | "express": "3.3.4", 10 | "ejs": "*", 11 | "stylus": "*", 12 | "passport": "~0.1.17", 13 | "passport-facebook": "~0.1.5", 14 | "connect-redis": "~1.4.5", 15 | "redis": "~0.8.4" 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 Andy Biar 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | var express = require('express') 2 | , routes = require('./routes') 3 | , profile = require('./routes/profile') 4 | , http = require('http') 5 | , path = require('path') 6 | , RedisStore = require('connect-redis')(express) 7 | , passport = require('passport') 8 | , FacebookStrategy = require('passport-facebook').Strategy; 9 | 10 | var app = express(); 11 | 12 | var redis = require('redis').createClient(); 13 | 14 | passport.use(new FacebookStrategy({ 15 | //Get this information from your app's page on developers.facebook.com 16 | clientID: 'INSERT FACEBOOK CLIENT ID', 17 | clientSecret: 'INSERT FACEBOOK SECRET', 18 | callbackURL: '/auth/facebook/callback' 19 | }, 20 | function(accessToken, refreshToken, profile, done) { 21 | // YOLO - use the entire public facebook profile as the session cookie 22 | done(null, profile); 23 | } 24 | )); 25 | 26 | passport.serializeUser(function(user, done) { 27 | done(null, user); 28 | }); 29 | 30 | passport.deserializeUser(function(id, done) { 31 | done(null, id); 32 | }); 33 | 34 | // all environments 35 | app.set('port', process.env.PORT || 3000); 36 | app.set('views', __dirname + '/views'); 37 | app.set('view engine', 'ejs'); 38 | app.use(express.favicon()); 39 | app.use(express.logger('dev')); 40 | app.use(express.bodyParser()); 41 | app.use(express.methodOverride()); 42 | app.use(express.cookieParser('your secret here')); 43 | app.use(express.session({ 44 | secret: "secretysecret", 45 | //Change this location if you are running Redis remotely 46 | store: new RedisStore({ host: 'localhost', port: 6397, client: redis}) 47 | })); 48 | app.use(passport.initialize()); 49 | app.use(app.router); 50 | app.use(require('stylus').middleware(__dirname + '/public')); 51 | app.use(express.static(path.join(__dirname, 'public'))); 52 | 53 | // development only 54 | if ('development' == app.get('env')) { 55 | app.use(express.errorHandler()); 56 | } 57 | 58 | app.get('/', routes.index); 59 | app.get('/profile', profile.show); 60 | app.get('/auth/facebook', passport.authenticate('facebook')); 61 | app.get('/auth/facebook/callback', 62 | passport.authenticate('facebook', { successRedirect: '/profile', 63 | failureRedirect: '/'})); 64 | 65 | http.createServer(app).listen(app.get('port'), function(){ 66 | console.log('Express server listening on port ' + app.get('port')); 67 | }); 68 | --------------------------------------------------------------------------------