5

PassportJS を使用して、ブラウザーとモバイル クライアントの両方の FB 認証を処理しています。Web ユーザーの場合、Passport FacebookStrategy を使用していますが、これは意図したとおりに機能しています。また、モバイル クライアントが API にアクセスできるようにしたいと考えています。これを容易にするために Passport FacebookTokenStrategy を使用しようとしています。これは、1つの小さな問題で機能しているようです。モバイル クライアントがサーバーに対して GET 要求を行うと、FacebookTokenStrategy が使用され、verify コールバック関数が呼び出されます。検証機能では、ユーザー プロファイルが使用可能であるため、認証が成功したことがわかります。ただし、モバイル クライアントへの応答で 404 の HTTP ステータスが返されます。これを適切に構成する方法がわかりません。これは私が現在試していることです:

// Web based auth
passport.use(new FacebookStrategy({
  clientID: Config.facebook.clientID,
  clientSecret: Config.facebook.clientSecret,
  callbackURL: "http://localhost/auth/facebook/callback"
},
function(accessToken, refreshToken, profile, done) {
 User.findOrCreate(profile, function(err, user){
  done(err, user);
});
}
));

// Mobile client auth
passport.use(new FacebookTokenStrategy({
  clientID: Config.facebook.clientID,
  clientSecret: Config.facebook.clientID
},
function(accessToken, refreshToken, profile, done) {
  console.log(profile);
  User.findOrCreate(profile, function(err, user){
    done(err, user);
  });
}
));

// Redirect the user to Facebook for authentication.  When complete,
// Facebook will redirect the user back to the application at
//     /auth/facebook/callback
exports.fb_auth = passport.authenticate('facebook',{ scope: 'email' });
// Facebook will redirect the user to this URL after approval.  Finish the
// authentication process by attempting to obtain an access token.  If
// access was granted, the user will be logged in.  Otherwise,
// authentication has failed.
exports.fb_callback = passport.authenticate('facebook', { successRedirect: '/',
  failureRedirect: '/login' });
// Mobile Authentication
exports.mobile_fb_auth = passport.authenticate('facebook-token');

Passport.authenticate('facebook-token'); を提供する必要がありますか? 追加の「onsuccess」コールバックを使用しますか? これは Web クライアントのコンテキストでは理にかなっていますが、facebook-token 戦略を使用してこれをどのように処理する必要があるかはわかりません。

4

1 に答える 1

0

私はちょうど同じ問題を抱えていて、それを解決することができました. ミドルウェアがエクスプレスでどのように機能するかにより、404 が返されます。成功を返す 3 番目の関数を渡す必要があります。

3 番目の関数は常に呼び出されるわけではありません。前のミドルウェアが成功した場合にのみ呼び出されます。

apiRouter.get('/auth/facebook',
    // authenticate with facebook-token.  
    passport.authenticate('facebook-token'),

    // if the user didn't successfully authenticate with the above line, 
    // the below function won't be called
    function(req, res){
        res.send(200);
    });

`

于 2015-10-17T19:29:24.047 に答える