Ember-Simple-Auth与Torii访问用户信息

时间:2014-12-18 18:46:59

标签: ember.js google-oauth ember-simple-auth torii

我整个星期都在努力让身份验证工作。我已经与它合作了

  • 灰烬-CLI
  • 灰烬-简单-AUTH
  • 鸟居
    • google-oauth2提供商

但是我证明从谷歌获取用户信息是不成功的。我已经尝试过创建一个torii-adapter,如文档中所述,但它似乎没有被称为

// app/torii-adapters/application.js
export default Ember.Object.extend({
  open: function(authorization){
    console.log('authorization from adapter', authorization);
  }
});

我已经筋疲力尽了我的google-foo并且正在寻求你的帮助。这是一个很好的图书馆组合授权,但是这个案例缺乏文档,当我想出来时,我一定会回馈。

谢谢

1 个答案:

答案 0 :(得分:4)

我遇到的问题是Torii的默认google-oauth2提供商不会为您访问此信息,它也使用代码工作流程而不是google + API所需的令牌工作流程

为了解决这个问题,我编写了一个自定义提供程序,它使用对G + API的jquery GET请求,然后返回userName和userEmail以在内容下的会话中访问它。

我编写了一个完整的教程,详细说明了使用google开始完成授权使用ember应用程序here

//app/torii-providers/google-token.js
import {configurable} from 'torii/configuration';
import Oauth2Bearer from 'torii/providers/oauth2-bearer';

var GoogleToken = Oauth2Bearer.extend({
  name: 'google-token',
  baseUrl: 'https://accounts.google.com/o/oauth2/auth',

  // additional params that this provider requires
  requiredUrlParams: ['state'],
  optionalUrlParams: ['scope', 'request_visible_actions', 'access_type'],

  requestVisibleActions: configurable('requestVisibleActions', ''),

  accessType: configurable('accessType', ''),

  responseParams: ['token'],

  scope: configurable('scope', 'email'),

  state: configurable('state', 'STATE'),

  redirectUri: configurable('redirectUri',
                            'http://localhost:8000/oauth2callback'),

  open: function(){
      var name        = this.get('name'),
          url         = this.buildUrl(),
          redirectUri = this.get('redirectUri'),
          responseParams = this.get('responseParams');

      var client_id = this.get('client_id');

      return this.get('popup').open(url, responseParams).then(function(authData){
        var missingResponseParams = [];

        responseParams.forEach(function(param){
          if (authData[param] === undefined) {
            missingResponseParams.push(param);
          }
        });

        if (missingResponseParams.length){
          throw "The response from the provider is missing " +
                "these required response params: " + responseParams.join(', ');
        }

        return $.get("https://www.googleapis.com/plus/v1/people/me", {access_token: authData.token}).then(function(user){
          return {
            userName: user.displayName,
            userEmail: user.emails[0].value,
            provider: name,
            redirectUri: redirectUri
          };
        });
      });
    }
});

export default GoogleToken;