Ember-Simple-Auth with Torii access user info

1.4k views Asked by At

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

There are 1 answers

2
Frozenfire On BEST ANSWER

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;