将 Meteor.loginWithGoogle 中的电子邮件地址放入 Session 变量中 [英] Put the email address from Meteor.loginWithGoogle into a Session variable

查看:41
本文介绍了将 Meteor.loginWithGoogle 中的电子邮件地址放入 Session 变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用中使用了 Meteor.loginWithGoogle.我正在尝试获取 google 用户的电子邮件地址,将其放入 Session 变量中.

I'm using Meteor.loginWithGoogle in my app. I'm trying to get the email address of the google user to put it into a Session variable.

Template.login.events({
  'click #google-login': function(event){
    Meteor.loginWithGoogle({}, function(err){
      if ( err ) {
        throw new Meteor.Error("Google login failed");
      } else {
        const emailAddress = ?; // how do I get this from google?
        Session.set('email',emailAddress);
        Router.go('/profile');
      }
    });
  }
});

推荐答案

我不确定我是否理解您的问题,但我想您想问的是:用户执行 loginWithGoogle 后,如何我得到他的电子邮件地址,并将其设置到他的会话中?"

I'm not sure whether i understood your question, but i guess what you're trying to ask is: "After a user performed a loginWithGoogle, how can i get his email address, and set it into his Session?"

登录后,Meteor.user() 保存当前用户文档.考虑到这一点:

After a login, Meteor.user() holds the current user document. Having that in mind:

const currentUser = Meteor.user();
const userGoogleServiceMain = currentUser.services.google.email;

有了它,你可以:

Template.login.events({
  'click #google-login': function(event){
    Meteor.loginWithGoogle({}, function(err){
      if ( err ) {
        throw new Meteor.Error("Google login failed");
      } else {
        const currentUser = Meteor.user();
        const emailAddress = currentUser.services.google.email;
        Session.set('email',emailAddress);
        Router.go('/profile');
      }
    });
  }
});

您可以在:Meteor 文档http://cs.wellesley.edu/~mashups/pages/meteor6.html

这篇关于将 Meteor.loginWithGoogle 中的电子邮件地址放入 Session 变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆