私は数日間苦労しており、いくつかの良い進歩を遂げましたが、セッションのものを完全に機能させることができません.
Passport を使用して Facebook での認証に成功しました。FB ボタンでログインをクリックすると、セッションが完全に読み込まれ、req.user オブジェクトの準備が整いましたが、一度だけ行う必要があるように思えます。
パスポートがクッキーを保存することは知っています。私はそれを見て、それが働いているのを見ました。
インデックス ページの読み込み時にユーザーが既にログインしているかどうかを検出しようとしていますが、ページを読み込むと、req.user オブジェクトは常に null であり、passport.deserializeUser メソッドが呼び出されて読み込まれることはありません (クリックした場合) FBボタンにログインすると呼び出されます)。
私の質問は、ページの読み込み時にパスポートに Cookie をチェックし、ある場合はユーザー セッションを読み込むように指示するにはどうすればよいですか?
更新- 後でこの質問を見つけた人のために、ここで学んだことを確認したかっただけです (コメントしてくれた人に感謝します)。うまくいけば、それは他の誰かを助けてくれるでしょう。
私は、サーバーに関係なく有効な .NET Cookie に慣れています。Node.js とパスポートは同じ前提では機能しません。デフォルトでは、node.js はメモリストアを使用してセッションを維持します。ターミナル/コマンド ラインでノードをシャットダウンすると (サーバー コードを変更するたびにシャットダウンする必要があります)、memorystore Cookie 情報がリセットされるため、関連付けられている Cookie はすべて無意味になります。
これを修正するために、Redis ( http://cook.coredump.me/post/18886668039/brew-install-redis ) をインストールし、ストアとして接続しました ( http://www.hacksparrow.com/use-redisstore- instead-of-memorystore-express-js-in-production.html )。
これは、予定している実稼働サーバーである Azure で機能するので、すべて問題ありません。
わかりました、ここにいくつかのコードがあります。どのパーツを付けるか迷う…
これはserver.jsです
/**
* Module dependencies.
*/
var express = require('express')
, app = express()
, partials = require('express-partials')
, http = require('http')
, server = http.createServer(app)
, io = require('socket.io').listen(server)
, routes = require('./routes')
// facebook
, passport = require('passport')
, facebookStrategy = require('passport-facebook').Strategy
// custom stuff
, urlCommand = require('./middleware/UrlCommand')
, azureCommand = require('./middleware/AzureCommand')
, userCommand = require('./middleware/UserCommand');
// Ports
var port = process.env.port;
if (isNaN(port))
port = 3000;
server.listen(port);
//allows the use of Layouts
app.use(partials());
passport.serializeUser(function(user, done) {
done(null, user.RowKey);
});
passport.deserializeUser(function (id, done) {
console.log("deserialize");
userCommand.findByID(id, function (err, user) {
done(err, user);
});
});
// Configuration
app.configure(function () {
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.session({ secret: 'SECRET!' }));
app.use(express.methodOverride());
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Facebook
passport.use(new facebookStrategy({
clientID: CLIENT,
clientSecret: "SECRET",
callbackURL: "http://localhost:3000/auth/facebook/callback" //DEV
},
function (accessToken, refreshToken, profile, done) {
userCommand.findOrCreate(profile.id, profile.name.givenName, profile.name.familyName, profile.emails[0].value, accessToken, function (error, user) {
return done(null, user);
});
}
));
// Routes
app.get('/', routes.index);
app.get('/auth/facebook', passport.authenticate('facebook', { scope: 'email' }));
app.get('/auth/facebook/callback', passport.authenticate('facebook', { successRedirect: '/',
failureRedirect: '/login' }));
// Sockets
io.sockets.on('connection', function (socket) {
// when the client emits 'sendURL', this listens and executes
socket.on('sendURL', function (data) {
console.log('sendURL called: ' + data);
urlCommand.AddURL(data);
// we tell the client to execute 'processURL'
io.sockets.emit('urlComplete', data);
});
});
console.log("Express server listening on port %d in %s mode", port, app.settings.env);
index.js
exports.index = function (req, res) {
console.log(req.user); // ALWAYS NULL
res.render('index', { title: 'Express' })
};
ユーザーコマンド
var azureCommand = require('../middleware/AzureCommand');
var tableService = azureCommand.CreateTableService();
function findByID(id, callback) {
console.log('FindByID');
tableService.queryEntity('user', 'user', id, function (error, entity) {
console.log('Found him: ' + entity.Email);
callback(error, entity);
});
}
function findOrCreate(id, first, last, email, accessToken, callback) {
var user = {
PartitionKey: 'user'
, RowKey: id
, First: first
, Last: last
, Email: email
, AccessToken: accessToken
}
tableService.insertEntity('user', user, function (error) {
callback(null, user);
});
}
exports.findByID = findByID;
exports.findOrCreate = findOrCreate;
これは、セッションを出力したときに出力ログに表示されるものです...
node server.js
info - socket.io started
Express server listening on port 3000 in development mode
{ cookie:
{ path: '/',
_expires: null,
originalMaxAge: null,
httpOnly: true },
passport: {}
}
debug - served static content /socket.io.js