使用帐户时,如何确保用户在应用会话和Google会话中保持一致

时间:2015-02-25 15:05:33

标签: meteor

我在我的应用上使用了Google帐户,我想解决相当奇怪的身份验证方案。

A 现在登录为应用会话和Google会话 A 切换到gmail并在那里注销。

现在,请注意, A 实际上仍然在流星应用程序中登录。

B 出现,使用他的帐户登录Gmail。

切换到流星应用程序以查看他已登录,但奇怪的是,使用 A 的帐户登录。

这种情况会导致许多混淆,并且人们在不知情的情况下使用其他用户共享计算机的帐户。

所以,基本上,我需要在meteor会话和google会话中的用户是相同的,如果没有,请确保当前的meteor会话无效并再次调用loginWithGoogle()。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

Meteor的当前账户套餐似乎不太可能,但可以使用谷歌最新的googleplus api创建一个新的套餐。

但似乎存在一种解决方法:

1)在路由器上设置onBeforeAction挂钩以自动登录用户(如果用户未登录到外部服务,则会要求提供凭据)

var loginWithGoogle = function() {
  if (Meteor.isClient) {
    Session.set('loginError', undefined);
    Meteor.loginWithGoogle({
      loginStyle         : "redirect",
      requestPermissions : ['profile', 'email'],
      requestOfflineToken: true
    }, function (err) {
      if (err)
        Session.set('loginError', 'reason: ' + err.reason + ' message: ' + err.message || 'Unknown error');
    });
  }
}

var requireLogin = function() {
  if (! Meteor.user()) {
    if (Meteor.loggingIn()) {
      this.render(this.loadingTemplate);
    } else {
      console.log('The app is automatically asking for you to log in.');
      loginWithGoogle();
    }
  } else {
    this.next();
  }
}

Router.onBeforeAction(requireLogin, {except: ['some-special-public-route']});

2)当用户离开每个页面时将用户注销(警告:每次用户在应用程序中导航时都会调用login / logout)

Meteor.startup(function(){
    $(window).bind('beforeunload', function() {
        closingWindow();
    });
});

closingWindow = function(){
    console.log('The app is automatically logging you out because you are leaving.');
    Meteor.logout();
}

3)改进区域:设置会话变量以跟踪用户在应用程序中的导航,并根据变量运行unload事件。