Skip to content
This repository
Browse code

Add login example.

  • Loading branch information...
commit 1bb4e2c178a92458dac3bbfc27ffe52cc6c51faf 1 parent 38a52c7
Jared Hanson authored
122 examples/login/app.js
... ... @@ -0,0 +1,122 @@
  1 +var express = require('express')
  2 + , passport = require('passport')
  3 + , util = require('util')
  4 + , GitHubStrategy = require('passport-github').Strategy;
  5 +
  6 +var GITHUB_CLIENT_ID = "--insert-github-client-id-here--"
  7 +var GITHUB_CLIENT_SECRET = "--insert-github-client-secret-here--";
  8 +
  9 +
  10 +// Passport session setup.
  11 +// To support persistent login sessions, Passport needs to be able to
  12 +// serialize users into and deserialize users out of the session. Typically,
  13 +// this will be as simple as storing the user ID when serializing, and finding
  14 +// the user by ID when deserializing. However, since this example does not
  15 +// have a database of user records, the complete GitHub profile is serialized
  16 +// and deserialized.
  17 +passport.serializeUser(function(user, done) {
  18 + done(null, user);
  19 +});
  20 +
  21 +passport.deserializeUser(function(obj, done) {
  22 + done(null, obj);
  23 +});
  24 +
  25 +
  26 +// Use the GitHubStrategy within Passport.
  27 +// Strategies in Passport require a `verify` function, which accept
  28 +// credentials (in this case, an accessToken, refreshToken, and GitHub
  29 +// profile), and invoke a callback with a user object.
  30 +passport.use(new GitHubStrategy({
  31 + clientID: GITHUB_CLIENT_ID,
  32 + clientSecret: GITHUB_CLIENT_SECRET,
  33 + callbackURL: "http://127.0.0.1:3000/auth/github/callback"
  34 + },
  35 + function(accessToken, refreshToken, profile, done) {
  36 + // asynchronous verification, for effect...
  37 + process.nextTick(function () {
  38 +
  39 + // To keep the example simple, the user's GitHub profile is returned to
  40 + // represent the logged-in user. In a typical application, you would want
  41 + // to associate the GitHub account with a user record in your database,
  42 + // and return that user instead.
  43 + return done(null, profile);
  44 + });
  45 + }
  46 +));
  47 +
  48 +
  49 +
  50 +
  51 +var app = express.createServer();
  52 +
  53 +// configure Express
  54 +app.configure(function() {
  55 + app.set('views', __dirname + '/views');
  56 + app.set('view engine', 'ejs');
  57 + app.use(express.logger());
  58 + app.use(express.cookieParser());
  59 + app.use(express.bodyParser());
  60 + app.use(express.methodOverride());
  61 + app.use(express.session({ secret: 'keyboard cat' }));
  62 + // Initialize Passport! Also use passport.session() middleware, to support
  63 + // persistent login sessions (recommended).
  64 + app.use(passport.initialize());
  65 + app.use(passport.session());
  66 + app.use(app.router);
  67 + app.use(express.static(__dirname + '/public'));
  68 +});
  69 +
  70 +
  71 +app.get('/', function(req, res){
  72 + res.render('index', { user: req.user });
  73 +});
  74 +
  75 +app.get('/account', ensureAuthenticated, function(req, res){
  76 + res.render('account', { user: req.user });
  77 +});
  78 +
  79 +app.get('/login', function(req, res){
  80 + res.render('login', { user: req.user });
  81 +});
  82 +
  83 +// GET /auth/github
  84 +// Use passport.authenticate() as route middleware to authenticate the
  85 +// request. The first step in GitHub authentication will involve redirecting
  86 +// the user to github.com. After authorization, GitHubwill redirect the user
  87 +// back to this application at /auth/github/callback
  88 +app.get('/auth/github',
  89 + passport.authenticate('github'),
  90 + function(req, res){
  91 + // The request will be redirected to GitHub for authentication, so this
  92 + // function will not be called.
  93 + });
  94 +
  95 +// GET /auth/github/callback
  96 +// Use passport.authenticate() as route middleware to authenticate the
  97 +// request. If authentication fails, the user will be redirected back to the
  98 +// login page. Otherwise, the primary route function function will be called,
  99 +// which, in this example, will redirect the user to the home page.
  100 +app.get('/auth/github/callback',
  101 + passport.authenticate('github', { failureRedirect: '/login' }),
  102 + function(req, res) {
  103 + res.redirect('/');
  104 + });
  105 +
  106 +app.get('/logout', function(req, res){
  107 + req.logout();
  108 + res.redirect('/');
  109 +});
  110 +
  111 +app.listen(3000);
  112 +
  113 +
  114 +// Simple route middleware to ensure user is authenticated.
  115 +// Use this route middleware on any resource that needs to be protected. If
  116 +// the request is authenticated (typically via a persistent login session),
  117 +// the request will proceed. Otherwise, the user will be redirected to the
  118 +// login page.
  119 +function ensureAuthenticated(req, res, next) {
  120 + if (req.isAuthenticated()) { return next(); }
  121 + res.redirect('/login')
  122 +}
10 examples/login/package.json
... ... @@ -0,0 +1,10 @@
  1 +{
  2 + "name": "passport-github-examples-login",
  3 + "version": "0.0.0",
  4 + "dependencies": {
  5 + "express": ">= 0.0.0",
  6 + "ejs": ">= 0.0.0",
  7 + "passport": ">= 0.0.0",
  8 + "passport-github": ">= 0.0.0"
  9 + }
  10 +}
4 examples/login/views/account.ejs
... ... @@ -0,0 +1,4 @@
  1 +<p>ID: <%= user.id %></p>
  2 +<p>Username: <%= user.username %></p>
  3 +<p>Name: <%= user.displayName %></p>
  4 +<p>Email: <%= user.emails[0].value %></p>
5 examples/login/views/index.ejs
... ... @@ -0,0 +1,5 @@
  1 +<% if (!user) { %>
  2 + <h2>Welcome! Please log in.</h2>
  3 +<% } else { %>
  4 + <h2>Hello, <%= user.displayName %>.</h2>
  5 +<% } %>
21 examples/login/views/layout.ejs
... ... @@ -0,0 +1,21 @@
  1 +<!DOCTYPE html>
  2 +<html>
  3 + <head>
  4 + <title>Passport-GitHub Example</title>
  5 + </head>
  6 + <body>
  7 + <% if (!user) { %>
  8 + <p>
  9 + <a href="/">Home</a> |
  10 + <a href="/login">Log In</a>
  11 + </p>
  12 + <% } else { %>
  13 + <p>
  14 + <a href="/">Home</a> |
  15 + <a href="/account">Account</a> |
  16 + <a href="/logout">Log Out</a>
  17 + </p>
  18 + <% } %>
  19 + <%- body %>
  20 + </body>
  21 +</html>
1  examples/login/views/login.ejs
... ... @@ -0,0 +1 @@
  1 +<a href="/auth/github">Login with GitHub</a>

0 comments on commit 1bb4e2c

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