meteor:用google登录(如何显示配置文件用户?)

时间:2016-04-27 15:01:16

标签: meteor

我用google Api创建了登录系统,但我有问题显示图片和名称用户。那是我的代码。请有人帮助我。

  // js
if (Meteor.isClient){
     Meteor.subscribe ('user');


 Template.body.helpers({
   firstName: function(){
    var user = Meteor.user(); 
    if (user) {
      return user.services.google.given_name;    
    } 
   },

   profileURL: function() {
    var user = Meteor.user(); 
    if (user) {
      return user.services.google.picture; 
    } 
 }
});



     //server
Meteor.publish("user", function () {
  return Meteor.users.find({_id: this.userId},
                           {fields: {'other': 1, 'things': 1}});
});

2 个答案:

答案 0 :(得分:0)

您可以使用Accounts.onCreateUser保存值并保存相关信息。

//in server
Accounts.onCreateUser(function(options,user){
    if (typeof(user.services.google) != "undefined") {
        user.email = user.services.google.email;
        user.profilePicture = user.services.google.picture;
        user.username = user.services.google.name;
    }
  return user;
});

然后,您可以使用{{}}返回这些字段。

{{currentUser.username}} // returns email of the current user

此外,您需要路由来定义路由。查看iron:routerflow:router

答案 1 :(得分:0)

您必须将所需的值添加到集合中的用户文档中。

此代码使用来自meteor.loginwith [ facebook google twitter ]的登录服务的常用值构建用户对象。然后将其添加到用户的mongo集合中。

现在可以从currentUser对象访问所有这些数据。

您可以删除未使用的值或网络。

Accounts.onCreateUser( ( options, user )=> {

    user.profile  = getProfile( user );

    function getProfile( user ) {

        let service = user.services;
        let network = _.keys( service )[ 0 ];
        let data    = service[ network ];
        let profile = {};

        if ( network === 'facebook' ) {
            profile.network = network;
            profile.id      = data.id;
            profile.email   = data.email;
            profile.name    = data.name;
            profile.locale  = data.locale;
            profile.gender  = data.gender;
            profile.picture = 'http://graph.facebook.com/' + data.id + '/picture?type=square';
        }

        else if ( network === 'google' ) {
            profile.network = network;
            profile.id      = data.id;
            profile.email   = data.email;
            profile.name    = data.name;
            profile.locale  = data.locale;
            profile.picture = data.picture;
        }

        else if ( network === 'twitter' ) {
            profile.network = network;
            profile.id      = data.id;
            profile.email   = data.email;
            profile.name    = data.screenName;
            profile.locale  = data.lang;
            profile.picture = data.profile_image_url_https;
        }

        let value        = {};
        value[ network ] = profile;
        return value;
    }

    Meteor.users.update( user._id, {
        $set: {
            profile: user.profile
        }
    } );

    return user;

} );