Created
          June 7, 2011 13:00 
        
      - 
      
 - 
        
Save dakatsuka/1012184 to your computer and use it in GitHub Desktop.  
    Sign in with twitter
  
        
  
    
      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
    
  
  
    
  | // This program is free software. It comes without any warranty, to | |
| // the extent permitted by applicable law. You can redistribute it | |
| // and/or modify it under the terms of the Do What The Fuck You Want | |
| // To Public License, Version 2, as published by Sam Hocevar. See | |
| // http://sam.zoy.org/wtfpl/COPYING for more details. | |
| var express = require('express'); | |
| var app = express.createServer(); | |
| var oauth = new (require('oauth').OAuth)( | |
| 'https://api.twitter.com/oauth/request_token', | |
| 'https://api.twitter.com/oauth/access_token', | |
| 'CONSUMER KEY', | |
| 'CONSUMER SECRET', | |
| '1.0', | |
| 'http://localhost:3000/auth/twitter/callback', | |
| 'HMAC-SHA1' | |
| ); | |
| app.configure(function() { | |
| app.use(express.logger()); | |
| app.use(express.bodyParser()); | |
| app.use(express.cookieParser()); | |
| app.use(express.session({ secret: "secret" })); | |
| app.set('view engine', 'jade') | |
| }); | |
| app.dynamicHelpers({ | |
| session: function(req, res) { | |
| return req.session; | |
| } | |
| }); | |
| app.get('/', function(req, res) { | |
| res.render('index', { layout: false }); | |
| }); | |
| app.get('/auth/twitter', function(req, res) { | |
| oauth.getOAuthRequestToken(function(error, oauth_token, oauth_token_secret, results) { | |
| if(error) { | |
| res.send(error) | |
| } else { | |
| req.session.oauth = {}; | |
| req.session.oauth.token = oauth_token; | |
| req.session.oauth.token_secret = oauth_token_secret; | |
| res.redirect('https://twitter.com/oauth/authenticate?oauth_token=' + oauth_token); | |
| } | |
| }); | |
| }); | |
| app.get('/auth/twitter/callback', function(req, res) { | |
| if(req.session.oauth) { | |
| req.session.oauth.verifier = req.query.oauth_verifier; | |
| oauth.getOAuthAccessToken(req.session.oauth.token, req.session.oauth.token_secret, req.session.oauth.verifier, | |
| function(error, oauth_access_token, oauth_access_token_secret, results) { | |
| if(error) { | |
| res.send(error); | |
| } else { | |
| req.session.oauth.access_token = oauth_access_token; | |
| req.session.oauth.access_token_secret = oauth_access_token_secret; | |
| req.session.user_profile = results | |
| res.redirect('/'); | |
| } | |
| } | |
| ); | |
| } | |
| }); | |
| app.get('/signout', function(req, res) { | |
| delete req.session.oauth; | |
| delete req.session.user_profile; | |
| res.redirect('/'); | |
| }); | |
| app.listen(3000); | 
  
    
      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
    
  
  
    
  | !!! 5 | |
| html(lang="ja") | |
| head | |
| title node.js sample app | |
| body | |
| h1 node.js sample app | |
| - if (session.user_profile) | |
| p= "Welcome, " + session.user_profile.screen_name | |
| p | |
| a(href="/signout") Sign out | |
| - else | |
| p | |
| a(href="/auth/twitter") Sign in with twitter | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment