# Authentication with emberjs and devise

**URL:** <https://discuss.emberjs.com/t/authentication-with-emberjs-and-devise/4236>\
**Category:** Uncategorized\
**Created:** [February 21, 2014, 10:54pm UTC](https://discuss.emberjs.com/t/authentication-with-emberjs-and-devise/4236 "2014-02-21T22:54:14Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Remon\_Amin](https://avatars.discourse-cdn.com/v4/letter/r/85e7bf/32.png) [@Remon\_Amin](https://discuss.emberjs.com/u/Remon_Amin)\
**Post date:** [February 21, 2014, 10:54pm UTC](https://discuss.emberjs.com/t/authentication-with-emberjs-and-devise/4236/1 "2014-02-21T22:54:14Z")

</div>

I am using ember js to authenticate with my rails api my devise sessions controller

```
module Api
class SessionsController < Devise::SessionsController

  def create
   unless params[:email] && params[:password]
  return invalid_params('You need to provide both email and password')
  end

  res = User.find_for_database_authentication(email: params[:email])

 if res && res.valid_password?(params[:password])
    user = res
  end
 unless user
   unless params[:email] && params[:password]
  return invalid_params('invalid email or password')
  else
   return invalid_params('You need to provide both email and password')
  end 
else
 sign_in user
  user.ensure_authentication_token!
 render json: user ,serializer: UserSerializer ,status: 201
   end
end

protected

 def invalid_params(errorMessage)
 warden.custom_failure!
 render json: { errorMessage: errorMessage }, status: 403
end
end
end

```

my emberjs auth.js

```
Auth =Ember.Object.extend({
auth_token: null,
current_user: null,
signIn: function(params) {

 return Ember.$.post('http://localhost:3001/api/users/sign_in', params).then((function(_this) {
  return function(response) {
    return _this.set('auth_token', response.auth_token);
     
  };
})(this));

   },
   signUp: function(params) {
	var mypar = {'user':params};
	var that=this;
	    return Ember.$.post('/users', mypar,function(data){
   return that.set('auth_token', data.auth_token);
  });
 },
 signOut: function(){
  promise= Ember.$.ajax("/users/sign_out",{
	type: "DELETE"
});
var that =this;
promise.then(function(){
	that.set("auth_token",null);
});
return promise;
  }

  });

   Remon.Auth =Auth.create();
 $.ajaxSetup({

   beforeSend: function(xhr, options) {
    var encoded_auth_token, header;
   if (Remon.Auth.get('auth_token')) {
    encoded_auth_token = Base64.encode64(Remon.Auth.get('auth_token') + ":X");
    header = "Basic " + encoded_auth_token;
   return xhr.setRequestHeader('Authorization', header);
  }
 },
error: function(xhr) {
  if (xhr.status === 401) {
  //return window.location = '/#/login';
 
   }
 }
});

```

sign in and sign up working great also I got the user authentication token from sessions controler but sign in sessions not working as when I tried to get json data to another route its not working inpite in the other controller I have these method

```
   module Api
 class ProfilesController < ApplicationController

 before_filter :auth_only?
def index
@profiles =Profile.all
 end 
end 
end 

```

in devise.rb config.http\_authenticatable = [:token]

and in application\_controller.rb

```
   skip_before_filter :verify_authenticity_token, :if => Proc.new { |c| c.request.format == 'application/json' }

 def allow_ajax_request_from_other_domains
 headers['Access-Control-Allow-Origin'] = '*'
  headers['Access-Control-Request-Method'] = '*'
  end

```

---

<div class="post-metadata">

**Author:** ![towerhe](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/towerhe/32/14483_2.png) [@towerhe](https://discuss.emberjs.com/u/towerhe)\
**Post date:** [February 22, 2014, 7:39pm UTC](https://discuss.emberjs.com/t/authentication-with-emberjs-and-devise/4236/2 "2014-02-22T19:39:13Z")

</div>

You could have a try with [ember-simple-auth](https://github.com/simplabs/ember-simple-auth). It works well with devise.

BTW, if you use a devise (\>3.1.0)，you may need [simple\_token\_authentication](https://github.com/gonzalo-bulnes/simple_token_authentication)

---

<div class="post-metadata">

**Author:** ![Remon\_Amin](https://avatars.discourse-cdn.com/v4/letter/r/85e7bf/32.png) [@Remon\_Amin](https://discuss.emberjs.com/u/Remon_Amin)\
**Post date:** [March 1, 2014, 2:00am UTC](https://discuss.emberjs.com/t/authentication-with-emberjs-and-devise/4236/3 "2014-03-01T02:00:51Z")

</div>

my devise version is 2.1.2 even I migrated to lateste version of devise the problem is still the same I returned back to 2.1.2 …the problem is I can’t authenticate even Sessions create and destroy working fine but no authorization sent to the api to ckeck the current user

---

<div class="post-metadata">

**Author:** ![embermaps](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/embermaps/32/5356_2.png) [@embermaps](https://discuss.emberjs.com/u/embermaps)\
**Post date:** [March 1, 2014, 2:55am UTC](https://discuss.emberjs.com/t/authentication-with-emberjs-and-devise/4236/4 "2014-03-01T02:55:00Z")

</div>

This might be worth looking at.

[https://github.com/d-i/ember-devise-simple-auth](https://github.com/d-i/ember-devise-simple-auth)

---

<div class="post-metadata">

**Author:** ![towerhe](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/towerhe/32/14483_2.png) [@towerhe](https://discuss.emberjs.com/u/towerhe)\
**Post date:** [March 1, 2014, 7:08am UTC](https://discuss.emberjs.com/t/authentication-with-emberjs-and-devise/4236/5 "2014-03-01T07:08:31Z")

</div>

Take a look at the following links:

[https://github.com/emberjs-cn/ember-menglifang/blob/develop/src/app/lib/devise-authenticator.coffee](https://github.com/emberjs-cn/ember-menglifang/blob/develop/src/app/lib/devise-authenticator.coffee)

[https://github.com/emberjs-cn/ember-menglifang/blob/develop/src/app/lib/devise-authorizer.coffee](https://github.com/emberjs-cn/ember-menglifang/blob/develop/src/app/lib/devise-authorizer.coffee)

---

<div class="post-metadata">

**Author:** ![Remon\_Amin](https://avatars.discourse-cdn.com/v4/letter/r/85e7bf/32.png) [@Remon\_Amin](https://discuss.emberjs.com/u/Remon_Amin)\
**Post date:** [March 1, 2014, 7:57am UTC](https://discuss.emberjs.com/t/authentication-with-emberjs-and-devise/4236/6 "2014-03-01T07:57:13Z")

</div>

it’s working for rails4 only no rails 3 branch

---

<div class="post-metadata">

**Author:** ![Remon\_Amin](https://avatars.discourse-cdn.com/v4/letter/r/85e7bf/32.png) [@Remon\_Amin](https://discuss.emberjs.com/u/Remon_Amin)\
**Post date:** [March 1, 2014, 2:51pm UTC](https://discuss.emberjs.com/t/authentication-with-emberjs-and-devise/4236/7 "2014-03-01T14:51:34Z")

</div>

the main problem is that there is no current user authenticated with devise

---

<div class="post-metadata">

**Author:** ![joefiorini](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/joefiorini/32/3902_2.png) [@joefiorini](https://discuss.emberjs.com/u/joefiorini)\
**Post date:** [March 1, 2014, 2:56pm UTC](https://discuss.emberjs.com/t/authentication-with-emberjs-and-devise/4236/8 "2014-03-01T14:56:20Z")

</div>

> [@embermaps](#):
>
> This might be worth looking at.
> 
> [GitHub - the-refinery/ember-devise-simple-auth: A plugin to integrate your Ember app with a (mostly stock) Devise setup](https://github.com/d-i/ember-devise-simple-auth)

Thanks @embermaps! I’m the author of the above plugin. In developing it we noticed that Rails will generate a new CSRF token any time you sign in/sign out. If you don’t send this new token it can result in issues. We solved this by always sending back the CSRF token in response headers & using a jQuery ajax response handler to update it in the DOM.

Here’s our SessionsController:

[https://github.com/d-i/ember-devise-simple-auth/blob/master/support/rails/app/controllers/ember\_devise\_simple\_auth/sessions\_controller.rb#L15-L20](https://github.com/d-i/ember-devise-simple-auth/blob/master/support/rails/app/controllers/ember_devise_simple_auth/sessions_controller.rb#L15-L20)

And here’s the response handler:

[https://github.com/d-i/ember-devise-simple-auth/blob/master/config/initializers/csrf.js](https://github.com/d-i/ember-devise-simple-auth/blob/master/config/initializers/csrf.js)

Note that these are handled for you out of the box with the plugin. Hope this helps!

---

<div class="post-metadata">

**Author:** ![Remon\_Amin](https://avatars.discourse-cdn.com/v4/letter/r/85e7bf/32.png) [@Remon\_Amin](https://discuss.emberjs.com/u/Remon_Amin)\
**Post date:** [March 1, 2014, 3:09pm UTC](https://discuss.emberjs.com/t/authentication-with-emberjs-and-devise/4236/10 "2014-03-01T15:09:55Z")

</div>

I removed protect\_from\_forgery from my application controller I tried your gem but it says I must install rails 4 my main problem is that current user is not authenticated inspite sessions create action is ok and it send current user response I posted the full code of my api application on stackoverflow would you mind check it I am searching for an answer for more than a week now [jquery - devise authentication with emberjs - Stack Overflow](http://stackoverflow.com/questions/22109004/devise-authentication-with-emberjs)

---

<div class="post-metadata">

**Author:** ![Remon\_Amin](https://avatars.discourse-cdn.com/v4/letter/r/85e7bf/32.png) [@Remon\_Amin](https://discuss.emberjs.com/u/Remon_Amin)\
**Post date:** [March 2, 2014, 11:41pm UTC](https://discuss.emberjs.com/t/authentication-with-emberjs-and-devise/4236/11 "2014-03-02T23:41:18Z")

</div>

I ended up by sending my all ajax requests with auth\_token in it ,and its working now

---

<div class="post-metadata">

**Author:** ![asabjorn](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/asabjorn/32/15693_2.png) [@asabjorn](https://discuss.emberjs.com/u/asabjorn)\
**Post date:** [April 21, 2014, 10:43pm UTC](https://discuss.emberjs.com/t/authentication-with-emberjs-and-devise/4236/12 "2014-04-21T22:43:35Z")

</div>

@Remon_Amin[Ember Simple Auth](https://github.com/simplabs/ember-simple-auth) now supports authentication with oauth2, devise, Facebook and other custom authorizers. See kagemusha and my [Ember App Kit+Ember Simple Auth API Client](https://github.com/digitalplaywright/eak-simple-auth-blog-client) and our [companion rails+devise+doorkeeper API server](https://github.com/digitalplaywright/eak-simple-auth-blog-server) for a working example that use it. Kristian Mandrup and I are working on cancan-style authorization for Ember. More info on that when its ready.
