0
votes

I used simplabs ember-simple-auth in ember and have rails setup using devise. when authenticate() gets called it sends the email and password to rails. which returns the token. I can see token in localstorage as follows but as soon as I hit refresh the data is lost taking me back to login page.

{"secure":{
    "authenticator":"authenticator:devise",
    "id":1,"email":"[email protected]",
    "created_at":"2015-12-21T06:25:31.000Z",
    "updated_at":"2015-12-22T10:11:56.728Z",
    "authentication_token":"UoUTuVmUfwsVUnHVkE4U",
    }
}

in my authenticate() function I have setup devise as authenticator with credentials.

export default Ember.Controller.extend({
  _email: Ember.inject.controller('email'),
  session: Ember.inject.service('session'),
  actions: {
    authenticate(){
      let identification = this.get('_email.email');
      let password = this.get('password');

      console.log(identification, password);
      this.get('session').authenticate("authenticator:devise",identification,password).catch((reason)=>{
        this.set('errorMessage',reason.error|| reason);
      });
    }
  }
});

inside my authenticators folder I have defined devise.js which contains

import DeviseAuthenticator from 'ember-simple-auth/authenticators/devise';
export default DeviseAuthenticator.extend();

inside my authorizers folder I have defined devise.js which contains

import DeviseAuthorizer from 'ember-simple-auth/authorizers/devise';
export default DeviseAuthorizer.extend();

my config/environment.js contains

ENV['simple-auth']={
    authorizer: 'simple-auth-authorizer:devise'
  };
  ENV['simple-auth-devise']={
    identificationAttributeName: 'email',
    resourceName:'user',
    tokenAttributeName: 'authentication_token'
  };

according to Ember Simple Auth: Session lost on refresh specifying identificationAttributeName: 'email' should have solved the problem but it still persists. Rails side

application_controller.rb

class ApplicationController < ActionController::Base
  respond_to :json
  protect_from_forgery with: :null_session
  before_filter :authenticate_user_from_token!

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

and 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 = {
          token: self.resource.authentication_token,
          email: self.resource.email
        }
        render json: data, status: 201
      end
    end
  end
end

routes are configured to use the session controller. I just started emberjs and I am getting stuck on this for some days now.I dont know where I have missed somthing.

1
Are you using the local storage store? And if so, what do you see being stored there?swastik
the content of local storage is on the topCcr

1 Answers

0
votes

You cannot configure the devise authenticator in config/environment.js but instead configure properties on the actual authenticator class as you would set properties e.g. on an Ember Data adapter:

import DeviseAuthenticator from 'ember-simple-auth/authenticators/devise';
export default DeviseAuthenticator.extend({
  identificationAttributeName: 'email',
  resourceName:'user',
  tokenAttributeName: 'authentication_token'
});

That way it will actually use the correct properties making the restore method resolve on refresh.