私はpassportJSでNodeJsを使用して、ユーザーがFacebookを使用してログイン/登録できるようにしています。それはうまく動作しているようだが、私はそこに何が起こっているか完全に理解しているかどうかはわからない。つまり、ユーザーはFacebookの詳細を使用してアプリ内の別のページからログインしようとすることができます。 Facebookはすべての詳細を私に返した後、私はそれらを記録したり、Facebookが返さものを使用して、新規ユーザーを登録し、これが行われた後、同じページにリダイレクトが、すでにログインしているコードは次のとおりです。Facebook戦略によるPassportJS、私のコードは正しいですか?
var passport = require('passport'),
FacebookStrategy = require('passport-facebook').Strategy;
function setupFBStrategy(req, res, next) {
return function(req, res, next) {
var redirectUrl = req.params.currentPage;
console.log('setupFBStrategy')
passport.use(new FacebookStrategy({
clientID: 'abc',
clientSecret: 'def',
// step 2 ################################
callbackURL: "/auth/facebook/callback?currentPage="+redirectUrl,
passReqToCallback: true,
profileFields: ['first_name', 'last_name', 'photos', 'email']
},
function(req, accessToken, refreshToken, profile, done) {
// step 5 ###########################
// verify callback. Now I have user email inside 'profile' and I authenticate this aginst my database. I use `done(..)` to further instruct my app if the registration/authentication with my system was success or not
}
));
next();
}
}
app.use(passport.initialize());
// step 1 ################################
app.get('/auth/facebook/cp/:currentPage', setupFBStrategy(), passport.authenticate('facebook', { authType: 'rerequest', scope: ['email'] }));
app.get('/auth/facebook/callback',
function(req, res, next) {
console.log(req.query.currentPage)
passport.authenticate('facebook', {
successRedirect: '/'+req.query.currentPage, // redirects when user allowed or logged in with username password
failureRedirect: '/'+req.query.currentPage // takes here when user clicks cancel when asked to give permissions
})(req,res,next);
}
);
passport.serializeUser(function(user, done) {
console.log('serialize='+user)
done(null, {});
});
passport.deserializeUser(function(user, done) {
console.log('deserialize=' + user);
done(null, {});
});
の手順は次のとおりです。
私のアプリのユーザーの場合は、
auth/facebook/cp
にリクエストする「ログイン/ facebookで登録」をクリックします。ルートコール
setUpFBStrategy
。ユーザーがcallbackUrlを見ている現在のページを追加します。PassportJSはユーザブラウザにリダイレクトされ、ユーザブラウザは認証のためにFacebookにリダイレクトされます。
ユーザーの認証はユーザーのブラウザにリダイレクトするので、ブラウザは手順2で指定したURLでcallbackURLにリダイレクトします。また、コールバックURLに '
?code=
'クエリー文字列を追加します。私のケースの公開情報とメールで、Facebookが返すものをこのクエースリングハッシュバージョンのバージョンですか?私のサーバー '
auth/facebook/callback
'が実行され、確認コールバックが実行されます。場合によってはdone(null,profile)
などを呼び出すかどうかコールバックサーバーはをブラウザにリダイレクトし、ブラウザにリダイレクトするとsuccessRedirect
またはfailureRedirect
'successRedirect
'または 'failureRedirect
'のルートにリダイレクトされます。
これまでのところすべて正常に機能しているようですが、わかりましたか? 'code
'は詳細なFacebookの返信クエリ文字列バージョンですか?私のコードにserializeUser
とdeserializeUser
関数が必要なのはなぜですか? refreshToken
とaccessToken
はいつ使用しますか?