首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从护照-facebook读取用户信息

如何从护照-facebook读取用户信息
EN

Stack Overflow用户
提问于 2017-11-01 00:20:02
回答 1查看 342关注 0票数 0

我通过以下方式实现了代码以启用facebook身份验证:

代码语言:javascript
复制
const authentication = require('feathers-authentication');
const jwt = require('feathers-authentication-jwt');
const local = require('feathers-authentication-local');
const oauth2 = require('feathers-authentication-oauth2');
const GoogleStrategy = require('passport-google-oauth20');
const FacebookStrategy = require('passport-facebook');

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


  // Set up authentication with the secret
  app.configure(authentication(config));
  app.configure(jwt());
  app.configure(local());

  app.configure(oauth2(Object.assign({
    name: 'google',
    Strategy: GoogleStrategy
  }, config.google)));

  app.configure(oauth2(Object.assign({
    name: 'facebook',
    Strategy: FacebookStrategy
  }, config.facebook)));

  // The `authentication` service is used to create a JWT.
  // The before `create` hook registers strategies that can be used
  // to create a new valid JWT (e.g. local or oauth2)
  app.service('authentication').hooks({
    before: {
      create: [
        authentication.hooks.authenticate(config.strategies),

        // modifying params.payload.
        hook => {
          // make sure params.payload exists
          hook.params.payload = hook.params.payload || {}
          // merge in a `test` property
          if(hook.params.user){
            Object.assign(hook.params.payload, {email: hook.params.user.email, name: hook.params.user.name});
          }
        }
      ],
      remove: [
        authentication.hooks.authenticate('jwt')
      ]
    }
  });
};

如果我在node_modules/passport-facebook/lib/strategy.js中调试代码执行,我可以看到检索了所有的用户配置文件字段(名称、电子邮件、性别.)但我还没有弄清楚如何将这些值传递给钩子,以便将其存储到数据库中。

有人知道如何将这些值传递给身份验证服务/钩子以存储它们吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-01 15:43:29

请看一下如何自定义oAuth响应上的部分。您应该在创建或更新用户时获取配置文件信息,并能够将其映射到before服务的users钩子中:

代码语言:javascript
复制
function customizeGithubProfile() {
  return function(hook) {
    console.log('Customizing Github Profile');
    // If there is a github field they signed up or
    // signed in with github so let's pull the primary account email.
    if (hook.data.github) {
      hook.data.email = hook.data.github.profile.emails.find(email => email.primary).value;
    }

    // If you want to do something whenever any OAuth
    // provider authentication occurs you can do this.
    if (hook.params.oauth) {
      // do something for all OAuth providers
    }

    if (hook.params.oauth.provider === 'github') {
      // do something specific to the github provider
    }

    return Promise.resolve(hook);
  };
}


app.service('users').hooks({
  before: {
    create: [customizeGithubProfile()],
    update: [customizeGithubProfile()]
  }
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47046421

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档