1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
| /**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path')
//passport moduleをrequire。
, passport = require('passport')
//passport-facebookをrequire。
, FacebookStrategy = require('passport-facebook').Strategy
//passport-twitterをrequire
, TwitterStrategy = require('passport-twitter').Strategy
;
var app = express();
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
//passportのinitializeとsessionを使います。
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function(){
app.use(express.errorHandler());
});
app.get('/', routes.index);
app.get('/users', user.list);
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
//passportのセッションを使うので
//リアライズ、デシリアライズのための関数を追記。
passport.serializeUser(function(user, done){
done(null, user);
});
passport.deserializeUser(function(obj, done){
done(null, obj);
});
//ここからFacebook認証の記述
var FACEBOOK_APP_ID = "--insert-facebook-app-id-here--"
var FACEBOOK_APP_SECRET = "--insert-facebook-app-secret-here--";
passport.use(new FacebookStrategy({
clientID: FACEBOOK_APP_ID,
clientSecret: FACEBOOK_APP_SECRET,
callbackURL: "http://localhost:3000/auth/facebook/callback"
},
function(accessToken, refreshToken, profile, done){
process.nextTick(function(){
done(null ,profile);
});
}
));
app.get('/account/facebook', facebookEnsureAuthenticated, routes.account);
app.get('/login/facebook', routes.login_facebook);
app.get('/auth/facebook',
passport.authenticate('facebook'),
function(req, res){}
);
app.get('/auth/facebook/callback',
passport.authenticate('facebook', { failureRedirect: '/login/facebook' }),
function(req, res) {
res.redirect('/');
}
);
app.get('/logout/facebook', function(req, res){
req.logout();
res.redirect('/');
});
function facebookEnsureAuthenticated(req, res, next) {
if (req.isAuthenticated()) { return next(); }
res.redirect('/login');
};
//ここからTwitter認証の記述
var TWITTER_CONSUMER_KEY = "--insert-twitter-consumer-key-here--";
var TWITTER_CONSUMER_SECRET = "--insert-twitter-consumer-secret-here--";
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) {
passport.session.accessToken = token;
passport.session.profile = profile;
process.nextTick(function () {
return done(null, profile);
});
}
));
app.get('/account/twitter', twitterEnsureAuthenticated, routes.account);
app.get('/login/twitter', routes.login_facebook);
app.get('/auth/twitter',
passport.authenticate('twitter'),
function(req, res){}
);
app.get('/auth/twitter/callback',
passport.authenticate('twitter', { failureRedirect: '/login/twitter' }),
function(req, res) {
res.redirect('/');
}
);
app.get('/logout/twitter', function(req, res){
req.logout();
res.redirect('/');
});
function twitterEnsureAuthenticated(req, res, next) {
if (req.isAuthenticated()) { return next(); }
res.redirect('/login/twitter');
};
|