0

我无法使用 OAuth2 Facebook 策略对 FeathersJS 服务器进行身份验证,因为在 Facebook 授予对用户个人资料的访问权限后,feathers-authentication-oauth2 插件不会将用户创建到数据库中,也不会创建所需的 JWT 令牌以进行身份​​验证在客户端应用程序中调用 feathersclient.authenticate() 时。

我试图按照我找到的所有解释如何做的文件,但作为一个很好的例子,我可以选择这个(https://blog.feathersjs.com/how-to-setup-oauth-flow- with-featherjs-522bdecb10a8 ) 这很好解释。

作为起点,我在文档(https://docs.feathersjs.com/guides/chat/readme.html)中解释的羽毛聊天应用程序正常工作后,我已经添加了 OAuth2 部分解释在中文档中。在 default.json 文件中,我添加了“facebook”身份验证策略:

"facebook": {
      "clientID": "MY_CLIENT_ID",
      "clientSecret": "MY_CLIENT_SECRET"
    }

在 authentication.js 文件中,我添加了 Facebook OAuth2 身份验证的配置:

const authentication = require('@feathersjs/authentication');
const jwt = require('@feathersjs/authentication-jwt');
const oauth2 = require('@feathersjs/authentication-oauth2');
const FacebookStrategy = require('passport-facebook').Strategy;

module.exports = function (app) {
  const config = app.get('authentication');

  // Set up authentication with the secret
  app.configure(authentication(config));
  app.configure(jwt());
  app.configure(oauth2({
    name: 'facebook',
    Strategy: FacebookStrategy,
    callbackURL: '/',
    scope: ['public_profile', 'email'],
  }));
...

最后,在 src/app.js 文件中,我添加了一个新的“Facebook 登录”按钮,它只是将 window.location 更改为 '/auth/facebook',这样 OAuth2 Facebook 流程就可以开始了。

按下“Facebook 登录”后,我希望在 NeDB DB 中创建用户并存储有效的 JWT,以便 feathersclient.authenticate() 调用不会失败。但不是那样,而是正确调用了 Facebook 登录页面,然后浏览器返回到主页 ('/'),​​但之后,当重新加载主页并调用 feathersclient.authenticate() 时,服务器抱怨没有任何有效的 JWT 令牌,因此身份验证失败。我也看不到在 NeDB DB 中创建的用户,所以应该由 feathers-authentication-oauth2 插件完成的假定用户和 JWT 创建不是......

4

1 回答 1

0

我终于让它工作了......我错误地配置了 Facebook 身份验证策略,我将其更改为:

app.configure(oauth2({
    name: 'facebook',
    successRedirect: '/',
    failureRedirect: '/',
    Strategy: FacebookStrategy
  }));

现在它正在工作。

于 2019-07-15T13:50:27.923 回答