1
votes

I've been working all week to get authentication working. I have gotten it working with

  • Ember-CLI
  • Ember-Simple-Auth
  • Torii
    • google-oauth2 provider

However I have proven unsuccessful in getting the users information from google. I have tried creating a torii-adapter as stated in their documentation but it doesn't appear to be called

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

I've exhausted my google-foo and am asking for your assistance. This is a great library combination for authorization however the documentation is lacking for this case, and when figured out I will be sure to contribute back.

Thank you

1
Not sure what the actual problem is. Is the above code a torii provider? What do you mean you cannot get the user information from Google?marcoow
I just want to be able to display the user account info from google (first name, last name, email, etc.) The above is a Torii Adapter but now that I think about it is not what I need. Torii says it provides this information in the session property (which is disabled by default) and Ember Simple Auth has its own which I don't see any user information in?Frozenfire
Ember Simple Auth defines a session and when you use the Ember Simple Auth torii authenticator then anything the torii provider resolves with will be made available via the session.marcoow
So ultimately it is down to getting the torii provider to resolve the user information (which I'm assuming isn't by default as I don't see it in session)?Frozenfire
Ultimately I would like to do something like {{session.currentUser}}Frozenfire

1 Answers

4
votes

The problem I was encountering is Torii's default google-oauth2 provider doesn't access this info for you, also it uses the code workflow instead of the token workflow which is needed for the google+ API

To fix this I wrote a custom provider that uses a jquery GET request to the G+ API, I then return the userName and userEmail to access it in the session under content.

I wrote a full tutorial detailing authorizing an ember app using google start to finish 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;