1

I did an update from 0.7.2 to 1.0.1 with ember-simple-authfor for devise. Reference resources: https://github.com/jimbeaudoin/ember-simple-auth-devise-example .
Now my authentification doesn't work at all, because authenticate_with_http_token alway empty, I could not find any problems with it. Do you have an idea ? Thanks.

Version:

Ember: 2.1.0
Ember Simple Auth: 1.0.1
Rails: 4.2.4
Devise: 3.5.2
No Ember data

Rails:

class Api::SessionsController < Devise::SessionsController
  skip_before_action :authenticate_user_from_token!
  skip_before_action :verify_authenticity_token

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


class Api::BaseController < ApplicationController
  respond_to :json
  before_action :authenticate_user_from_token!

  # Enter the normal Devise authentication path,
  # using the token authenticated user if available
  before_action :authenticate_user!

  private

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

end

Ember:
login-controller:

import Ember from 'ember';

export default Ember.Controller.extend({
  session: Ember.inject.service('session'),

  actions: {
    authenticate() {
      let { identification, password } = this.getProperties('identification', 'password');
      this.get('session').authenticate('authenticator:devise', identification, password).catch((reason) => {
        this.set('errorMessage', reason.error);
      });
    }
  }
})

ember model:

import Ember from 'ember';
import PromiseArray from './promise-array';
import PromiseObject from './promise-object';

var Account = Ember.Object.extend({
  ...
})

Account.reopenClass({
  _find() {
    return $.get("/api/account.json").then(obj => {
      return this.create(obj.user);
    })
  },

  find() {
    return PromiseObject.create({promise: this._find()});
  }
})

export default Account;

after login route:

import Ember from 'ember';
import Account from '../models/account';
import AuthenticatedRouteMixin from 'simple-auth/mixins/authenticated-route-mixin';

export default Ember.Route.extend(AuthenticatedRouteMixin, {
  model: function() {
    return Account.find()
  }
})
JeskTop
  • 481
  • 1
  • 4
  • 20

1 Answers1

2

I think what's failing for you is authorization of requests following the initial login request? Do you authorize outgoing requests anywhere in your Ember app?

marcoow
  • 4,062
  • 1
  • 14
  • 21
  • authenticate_with_http_token always ends up being nil and the loop is always escaped – Abhaya Nov 02 '15 at 17:19
  • Yes. When I logined success, I got token. But the another request got the 401 problem. I update my problem and set more code. I think when I use ember-simple-authfor and not use ember data, I must set the every requests must take the token? – JeskTop Nov 03 '15 at 09:32
  • Auto authorization has been removed in 1.0. If you're not authorizing requests anywhere then no auth info will be included. See this thread: http://stackoverflow.com/questions/33483643/ember-simple-auth-custom-authorizer-not-called/33485726?noredirect=1#comment54774684_33485726 – marcoow Nov 03 '15 at 09:39