Created
May 18, 2013 11:44
-
-
Save yoppi/5604149 to your computer and use it in GitHub Desktop.
Twitterでログインするやつを、node + express + Redisにセッションを保存する実装
This file contains 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'), | |
RedisStore = require('connect-redis')(express), | |
OAuth = require('oauth').OAuth, | |
app = express(); | |
var redisConfig = require(__dirname + '/redis.json'); | |
app.use(express.favicon()); | |
app.use(express.cookieParser()); | |
app.use(express.session({ | |
secret: 'deadbeef', | |
key: "_sid", | |
cookie: { maxAge: 1000 * 60 * 60 }, // ミリ秒で指定する | |
store: new RedisStore(redisConfig) | |
})); | |
app.use(count); // debug | |
app.set("views", __dirname); | |
app.set("view engine", "jade"); | |
var _consumerKey = "TODO: your application key"; | |
var _consumerSecret = "TODO: your application secret"; | |
var requestTokenUrl = "https://api.twitter.com/oauth/request_token"; | |
var authenticateUrl = "https://api.twitter.com/oauth/authenticate"; | |
var accessTokenUrl = "https://api.twitter.com/oauth/access_token"; | |
var callbackUrl = "http://localhost:8888/oauth/callback"; | |
function count(req, res, next) { | |
req.session.count = req.session.count || 0; | |
var n = req.session.count++; | |
console.log('viewed ' + n + ' times'); | |
next(); | |
} | |
function isAuthorized(req, res, next) { | |
if (req.url != '/' && !req.session.oauthAccessToken) { | |
res.redirect('/'); | |
return; | |
} | |
if (req.url != '/top' && req.session.oauthAccessToken) { | |
res.redirect('/top'); | |
return; | |
} | |
next(); | |
} | |
function _OAuth() { | |
return new OAuth( | |
requestTokenUrl, accessTokenUrl, _consumerKey, _consumerSecret, "1.0A", callbackUrl, "HMAC-SHA1" | |
); | |
} | |
app.get('/', isAuthorized, function(req, res) { | |
res.render('index'); | |
}); | |
app.get('/top', isAuthorized, function(req, res) { | |
res.render('top'); | |
}); | |
app.get('/auth/twitter', function(req, res) { | |
var oauth = _OAuth(); | |
oauth.getOAuthRequestToken(function(err, oauthToken, oauthTokenSecret, results) { | |
if (err) { console.log(err); return; } | |
req.session.oauthToken = oauthToken; | |
req.session.oauthTokenSecret = oauthTokenSecret; | |
res.redirect(authenticateUrl + "?oauth_token=" + oauthToken); | |
}); | |
}); | |
app.get('/oauth/callback', function(req, res) { | |
var oauth = _OAuth(); | |
oauth.getOAuthAccessToken( | |
req.session.oauthToken, | |
req.session.oauthTokenSecret, | |
req.query.oauth_verifier, | |
function(err, oauthAccessToken, oauthAccessTokenSecret, results) { | |
if (err) { console.log(err); return; } | |
req.session.oauthAccessToken = oauthAccessToken; | |
req.session.oauthAccessTokenSecret = oauthAccessTokenSecret; | |
res.redirect('/top'); | |
}); | |
}); |
This file contains 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 | |
meta(charset="utf-8") | |
title="Twitter Login with Express" | |
body | |
h1="Not Logined" | |
a(href="/auth/twitter") login with Twitter Account |
This file contains 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
{ | |
"host": "127.0.0.1", | |
"port": "6379", | |
"ttl": 3600, | |
"db": 9, | |
"prefix": "_sid:" | |
} |
This file contains 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 | |
meta(charset="utf-8") | |
title="Twitter Logined Page" | |
body | |
h1="Logined!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment