Permalink
Browse files

Add examples.

  • Loading branch information...
1 parent 6fdf2fb commit 74dedd632ea8a9d956f4f8f26b929f0e98aa188e @jaredhanson committed Oct 23, 2011
View
@@ -0,0 +1,122 @@
+var express = require('express')
+ , passport = require('passport')
+ , util = require('util')
+ , TwitterStrategy = require('passport-twitter').Strategy;
+
+var TWITTER_CONSUMER_KEY = "--insert-twitter-consumer-key-here--"
+var TWITTER_CONSUMER_SECRET = "--insert-twitter-consumer-secret-here--";
+
+
+// Passport session setup.
+// To support persistent login sessions, Passport needs to be able to
+// serialize users into and deserialize users out of the session. Typically,
+// this will be as simple as storing the user ID when serializing, and finding
+// the user by ID when deserializing. However, since this example does not
+// have a database of user records, the complete Twitter profile is serialized
+// and deserialized.
+passport.serializeUser(function(user, done) {
+ done(null, user);
+});
+
+passport.deserializeUser(function(obj, done) {
+ done(null, obj);
+});
+
+
+// Use the TwitterStrategy within Passport.
+// Strategies in passport require a `verify` function, which accept
+// credentials (in this case, a token, tokenSecret, and Twitter profile), and
+// invoke a callback with a user object.
+passport.use(new TwitterStrategy({
+ consumerKey: TWITTER_CONSUMER_KEY,
+ consumerSecret: TWITTER_CONSUMER_SECRET,
+ callbackURL: "http://127.0.0.1:3000/auth/twitter/callback"
+ },
+ function(token, tokenSecret, profile, done) {
+ // asynchronous verification, for effect...
+ process.nextTick(function () {
+
+ // To keep the example simple, the user's Twitter profile is returned to
+ // represent the logged-in user. In a typical application, you would want
+ // to associate the Twitter account with a user record in your database,
+ // and return that user instead.
+ return done(null, profile);
+ });
+ }
+));
+
+
+
+
+var app = express.createServer();
+
+// configure Express
+app.configure(function() {
+ app.set('views', __dirname + '/views');
+ app.set('view engine', 'ejs');
+ app.use(express.logger());
+ app.use(express.cookieParser());
+ app.use(express.bodyParser());
+ app.use(express.methodOverride());
+ app.use(express.session({ secret: 'keyboard cat' }));
+ // Initialize Passport! Also use passport.session() middleware, to support
+ // persistent login sessions (recommended).
+ app.use(passport.initialize());
+ app.use(passport.session());
+ app.use(app.router);
+ app.use(express.static(__dirname + '/public'));
+});
+
+
+app.get('/', function(req, res){
+ res.render('index', { user: req.user });
+});
+
+app.get('/account', ensureAuthenticated, function(req, res){
+ res.render('account', { user: req.user });
+});
+
+app.get('/login', function(req, res){
+ res.render('login', { user: req.user });
+});
+
+// GET /auth/twitter
+// Use passport.authenticate() as route middleware to authenticate the
+// request. The first step in Twitter authentication will involve redirecting
+// the user to twitter.com. After authorization, the Twitter will redirect
+// the user back to this application at /auth/twitter/callback
+app.get('/auth/twitter',
+ passport.authenticate('twitter'),
+ function(req, res){
+ // The request will be redirected to Twitter for authentication, so this
+ // function will not be called.
+ });
+
+// GET /auth/twitter/callback
+// Use passport.authenticate() as route middleware to authenticate the
+// request. If authentication fails, the user will be redirected back to the
+// login page. Otherwise, the primary route function function will be called,
+// which, in this example, will redirect the user to the home page.
+app.get('/auth/twitter/callback',
+ passport.authenticate('twitter', { failureRedirect: '/login' }),
+ function(req, res) {
+ res.redirect('/');
+ });
+
+app.get('/logout', function(req, res){
+ req.logout();
+ res.redirect('/');
+});
+
+app.listen(3000);
+
+
+// Simple route middleware to ensure user is authenticated.
+// Use this route middleware on any resource that needs to be protected. If
+// the request is authenticated (typically via a persistent login session),
+// the request will proceed. Otherwise, the user will be redirected to the
+// login page.
+function ensureAuthenticated(req, res, next) {
+ if (req.isAuthenticated()) { return next(); }
+ res.redirect('/login')
+}
@@ -0,0 +1,10 @@
+{
+ "name": "passport-twitter-examples-signin",
+ "version": "0.0.0",
+ "dependencies": {
+ "express": ">= 0.0.0",
+ "ejs": ">= 0.0.0",
+ "passport": ">= 0.0.0",
+ "passport-twitter": ">= 0.0.0"
+ }
+}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,2 @@
+<p>ID: <%= user.id %></p>
+<p>Username: <%= user.username %></p>
@@ -0,0 +1,5 @@
+<% if (!user) { %>
+ <h2>Welcome! Please log in.</h2>
+<% } else { %>
+ <h2>Hello, <%= user.username %>.</h2>
+<% } %>
@@ -0,0 +1,21 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Passport-Twitter Example</title>
+ </head>
+ <body>
+ <% if (!user) { %>
+ <p>
+ <a href="/">Home</a> |
+ <a href="/login">Log In</a>
+ </p>
+ <% } else { %>
+ <p>
+ <a href="/">Home</a> |
+ <a href="/account">Account</a> |
+ <a href="/logout">Log Out</a>
+ </p>
+ <% } %>
+ <%- body %>
+ </body>
+</html>
@@ -0,0 +1 @@
+<a href="/auth/twitter"><img src="sign-in-with-twitter-l.png" width="151" height="24"></a>
@@ -12,9 +12,9 @@ var util = require('util')
* Twitter using the OAuth protocol.
*
* Applications must supply a `verify` callback which accepts a `token`,
- * `tokenSecret` and service-specific `info`, and then calls the `done` callback
- * supplying a `user`, which should be set to `false` if the credentials are not
- * valid. If an exception occured, `err` should be set.
+ * `tokenSecret` and service-specific `profile`, and then calls the `done`
+ * callback supplying a `user`, which should be set to `false` if the
+ * credentials are not valid. If an exception occured, `err` should be set.
*
* Options:
* - `consumerKey` identifies client to Twitter
@@ -28,7 +28,7 @@ var util = require('util')
* consumerSecret: 'shhh-its-a-secret'
* callbackURL: 'https://www.example.net/auth/twitter/callback'
* },
- * function(token, tokenSecret, info, done) {
+ * function(token, tokenSecret, profile, done) {
* User.findOrCreate(..., function (err, user) {
* done(err, user);
* });

0 comments on commit 74dedd6

Please sign in to comment.