如何在OAuth之后使用Meteor v1.4+;中的内置帐户ui包填充客户端Meteor.user.services;?

如何在OAuth之后使用Meteor v1.4+;中的内置帐户ui包填充客户端Meteor.user.services;?,meteor,meteor-accounts,Meteor,Meteor Accounts,我在Meteor v1.4.1中使用帐户ui和帐户google。我无法使user.services对象在客户端代码中显示范围。特别是,我需要谷歌的个人资料图片 我已将服务器端代码配置为通过Google身份验证,如下所示: import { Meteor } from 'meteor/meteor'; import { ServiceConfiguration } from 'meteor/service-configuration'; const services = Meteor.setti

我在Meteor v1.4.1中使用帐户ui和帐户google。我无法使
user.services
对象在客户端代码中显示范围。特别是,我需要谷歌的个人资料图片

我已将服务器端代码配置为通过Google身份验证,如下所示:

import { Meteor } from 'meteor/meteor';
import { ServiceConfiguration } from 'meteor/service-configuration';

const services = Meteor.settings.private.oauth;
for (let service of Object.keys(services)) {
  ServiceConfiguration.configurations.upsert({
    service
  }, {
    $set: {
      clientId: services[service].app_id,
      secret: services[service].secret,
      loginStyle: "popup"
    }
  });
}
…和客户端代码来配置权限,如下所示:

Accounts.ui.config({
  requestPermissions: {
    google: ['email', 'profile']
  },
  forceApprovalPrompt: {
    google: true
  },
  passwordSignupFields: 'EMAIL_ONLY'
});
当用户单击“使用谷歌登录”按钮时,会出现一个弹出窗口,用户可以进行身份验证。但是,尽管
google
forceApprovalPrompt
设置为true,但不会出现提示

最大的问题是当我执行这个

const user = Meteor.user();
console.log(user.services);
在客户端代码的任何地方,我都看不到预期的用户服务信息。我检查了我的数据库,它肯定是在那里拍摄的:

$ mongo localhost:27017
> db.users.find({})
> ... "services" : { "google" : { "accessToken" : ... } } ...

我很好奇我错过了什么?为了让用户服务数据存在于客户端中,我是否应该明确定义发布函数?

出于安全原因,服务属性被故意隐藏在客户端。这里有两种方法:

建议

  • 我更喜欢的方法是用流星的方法给你带来 在你需要的几个地方,你可能需要公钥和化身 他们
  • 成功登录后,您可以将所需数据记录在用户对象中的某个位置,但不在服务属性中
  • 正如您所说,您可以创建一个新的发布,明确指定要检索哪些字段以及要隐藏哪些字段。不过,你必须小心你发布的内容
  • 代码示例

  • 流星法:
  • //服务器
    流星法({
    getProfilePicture(){
    const services=Meteor.user().services;
    //替换为实际配置文件图片属性
    return services.google&&services.google.profilePicture;
    }
    });
    //客户
    Meteor.call('getProfilePicture',(呃,profilePicture)=>{
    console.log('profile picture url',profilePicture);
    });
    
  • 成功创建用户后的更新(您可能还希望有一个登录挂钩,以反映google中的任何头像/图片更改):
  • //配置用户创建时配置文件数据的情况
    Accounts.onCreateUser((选项,用户)=>{
    如果(!('profile'在选项中)){options.profile={};}
    如果(!('providers'在options.profile中)){options.profile.providers={};}
    //在此处定义其他特定配置文件选项
    if(user.services.google){
    options.profile.providers.google={
    图片:user.services.google.picture
    }
    }
    user.profile=options.profile;
    返回用户;
    });
    
  • 仅发布选定数据
  • //服务器
    Meteor.publish('userData',函数(){
    if(this.userId){
    返回Meteor.users.find({u id:this.userId}{
    字段:{other:1,things:1}
    });
    }否则{
    这个;
    }
    });
    //客户
    Meteor.subscribe('userData');