代码之家  ›  专栏  ›  技术社区  ›  Niko Gamulin

如何从passport facebook读取用户信息

  •  0
  • Niko Gamulin  · 技术社区  · 7 年前

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

    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

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

    1 回复  |  直到 7 年前
        1
  •  1
  •   Daff    7 年前

    请看上的部分 how to customize the oAuth response . 您应该在创建或更新用户时获得配置文件信息,并能够在 before 用于 users

    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()]
      }
    });
    
    推荐文章