Search code examples
ruby-on-railsember.jsdevisecoffeescriptember-simple-auth

Ember Simple Auth (Devise) after update, authenticate break


I did an update from 0.6.4 to 0.7.2 with Ember Simple Auth (not Ember CLI version) for devise, now my authentification doesn't work at all :(, do you have an idea ? thank you very much for your help :)

PS : apparently, ApplicationController (application_controller.rb) don't continue after authenticate_with_http_token do |token, options| and authenticate_with_http_token is empty (tested with puts)

login_controller.js

App.LoginController = Ember.Controller.extend(SimpleAuth.LoginControllerMixin, {
  authenticator: 'simple-auth-authenticator:devise'
  //authenticator: 'authenticator:custom'
});

application.js.coffee

Ember.Application.initializer
  name: "authentication"
  after: "simple-auth"
  initialize: (container, application) ->
    applicationRoute = container.lookup("route:application")
    session = container.lookup("simple-auth-session:main")
    # handle the session events
    session.on "sessionAuthenticationSucceeded", ->

      applicationRoute.transitionTo "Myspace"
      return

    return

window.ENV = window.ENV || {}
window.ENV["simple-auth"] = { store: 'simple-auth-session-store:local-storage', authorizer: "simple-auth-authorizer:devise" };
window.ENV['simple-auth-devise'] = {
    crossOriginWhitelist: ['*'], 
    serverTokenEndpoint: 'users/sign_in',
  };

login.hbs

<br />
<div class="row">
    <div class="large-12 columns">
        <form {{action 'authenticate' on='submit'}}>
          <label for="identification">Login</label>
          {{input id='identification' placeholder='Enter Login' value=identification}}
          <label for="password">Password</label>
          {{input id='password' placeholder='Enter Password' type='password' value=password}}
          <button type="submit">Login</button>
        </form>
    </div>
</div>

login_route.js.coffee

App.LoginRoute = Ember.Route.extend(

  #model: (params) ->
    #return @store.find('user', @get('session.user_id'))

  setupController: (controller, model) ->
    #controller.set "content", model
    controller.set "errorMessage", null
    return

  actions:
    sessionAuthenticationFailed: (responseBody) ->
      message = responseBody.error
      @controller.set "errorMessage", message
      console.log "errorMessage : " + message
      return )

myspace_route.js.coffee

App.MyspaceRoute = Ember.Route.extend(SimpleAuth.AuthenticatedRouteMixin,  ....)

session_controller.rb

class SessionsController < Devise::SessionsController
  def create
    respond_to do |format|
      format.html { super }
      format.json do
        self.resource = warden.authenticate!(auth_options)
        sign_in(resource_name, resource)
        data = {
          user_token: self.resource.authentication_token,
          user_email: self.resource.email
        }
        render json: data, status: 201
      end
    end
  end
end

application_controller.rb

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :null_session,
      if: Proc.new { |c| c.request.format =~ %r{application/json} }

  before_filter :skip_trackable, :authenticate_user_from_token!

  private

    def skip_trackable
      request.env['warden'].request.env['devise.skip_trackable'] = '1'
    end

    def authenticate_user_from_token!
      puts "authentification"
      puts authenticate_with_http_token
      authenticate_with_http_token do |token, options|
        user_email = options[:user_email].presence
        user       = user_email && User.find_by_email(user_email)
        puts "user.authentication_token"
        puts user.authentication_token
        puts token
        puts "token"
        if user && Devise.secure_compare(user.authentication_token, token)
          sign_in user, store: false
        end
      end
    end
end

Solution

  • With the help of marcoow, just modified https://github.com/simplabs/ember-simple-auth/tree/master/packages/ember-simple-auth-devise#server-side-setup SessionsController like this :

    class SessionsController < Devise::SessionsController
      def create
        respond_to do |format|
          format.html { super }
          format.json do
            self.resource = warden.authenticate!(auth_options)
            sign_in(resource_name, resource)
            @data = {
              user_token: self.resource.authentication_token,
              user_email: self.resource.email
            }
            render json: @data.to_json, status: 201
          end
        end
      end
    end
    

    Now it's working

    Edit : to_json explanation : http://apidock.com/rails/ActiveRecord/Serialization/to_json