Ember-Simple-Auth with Torii again Rails

Hi all! Just managed to get OAuth working with an ember app. I’ve never used ember-simple-auth or torii before so I wanted to ask what you all through. I plan to write a blog-post regarding how I implemented this so I want to know if there is anything that I am doing that seems odd :wink:

Flow is pretty basic.

  • Ember obtains authorization code
  • Ember hands code off to Rails Omniauth middleware
  • Omniauth obtains token
  • creates user
  • user_id is returned to Ember
  • Ember saves user with given id to the current_user on the ember-simple-auth-session

environment.js

  ENV['torii'] = {
    sessionServiceName: 'session:custom',
    providers: {
      'google-oauth2': {
        apiKey: process.env.GOOGLE_OAUTH_KEY,
        redirectUri: 'http://localhost:4200/users/auth/google_oauth2/callback' // default is the current URL
      }
    }
  }

routes/index.coffee

`import Ember from 'ember'`

IndexRoute = Ember.Route.extend(
  authenticator: 'authenticator:custom'

  actions:
    authenticate: ->
      @get('session').authenticate('simple-auth-authenticator:torii', 'google-oauth2').then =>
        code =  @get('session.secure.authorizationCode')
        $.ajax(
          type: 'POST'
          url: '/users/auth/google_oauth2/callback'
          dataType: 'json'
          crossDomain: false
          data:
            code: code
          success: (json) =>
            @store.find('user', json.user_id).then (user) =>
              @set('session.current_user', user)
        )

    invalidate: ->
      @get('session').invalidate('simple-auth-authenticator:torii', 'google-oauth2')
)

`export default IndexRoute`

index.hbs

{{#if session.isAuthenticated}}
  Hello {{session.current_user.email}}
  <br/>
  <button {{action "invalidate"}}>Logout</button>
{{else}}
  <button {{action "authenticate"}}>Login</button>
{{/if}}

Did you ever test this in production?

It looks very similar to a setup I had, but I have the feeling your redirect_uri will cause issues when your Ember and Rails apps are served on the same port.