【发布时间】:2019-11-23 00:36:21
【问题描述】:
我无法使用 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) 中解释的 Feathers 聊天应用程序正常工作后,我添加了 OAuth2 部分,如 Medium 文档中所述。在 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 数据库中创建用户并存储有效的 JWT,这样 feathersclient.authenticate() 调用就不会失败。 但不是那样,而是正确调用了 Facebook 登录页面,然后浏览器返回到主页 ('/'),但之后,当重新加载主页并调用 feathersclient.authenticate() 时,服务器抱怨没有任何有效的 JWT 令牌,因此身份验证失败。我也看不到在 NeDB DB 中创建的用户,所以应该由 feathers-authentication-oauth2 插件完成的假定用户和 JWT 创建不是......
【问题讨论】:
标签: oauth-2.0 feathersjs feathers-authentication