gonzalo-bulnes / simple_token_authentication

Simple (and safe*) token authentication for Rails apps or API with Devise.
GNU General Public License v3.0
1.51k stars 240 forks source link

Empty response on login #235

Closed MarkMT closed 8 years ago

MarkMT commented 8 years ago

I'm trying to use s_t_a with ember_simple_auth and Rails 5 api + devise. If I login with incorrect credentials I get a json response {"error":"Invalid email or password."} as expected. However if I submit correct credentials I get a 200 response from the server, but the response is empty.

Following this comment I've added

  include ActionController::MimeResponds
  include ActionController::ImplicitRender

  respond_to :json

to my ApplicationController.

Because my front end is Ember, I'm also using ActiveModelSerializer's json_api adapter. When I log in (in development mode) I see this in my server console -

[active_model_serializers] Rendered PersonSerializer with ActiveModelSerializers::Adapter::JsonApi (3.08ms)

yet I still see nothing in the body of the response received by the browser.

As suggested in this comment I've also tried overriding Devise::SessionsController#create to render json directly - this seems to bypass the AMS serialization, but again there's no content in the response received by the browser.

Any advice would be greatly appreciated.

MarkMT commented 8 years ago

Update: It seems that ActiveModelSerializers was the culprit here. The solution was to override the devise SessionsController#create action and render raw json instead of the user resource

render :json => {:email => current_person.email, :authentication_token => current_person.authentication_token}
gonzalo-bulnes commented 8 years ago

Hello @MarkMT,

You were faster than me, yes you have to include the authentication_token in your API response if you want to use it. I guess including it in your PersonSerializer wouldn't be a good idea, but I wonder if it is possible to append it in the context of the sign in action (?) maybe not.

I would be interested in your experience using the gem with Ember, if you want to put a short example in a gist or something alike, I would be glad to put a link in the wiki : )

MarkMT commented 8 years ago

Sure, I'm on a tight schedule atm, but when I get done with this project I'll try to put something together.

gonzalo-bulnes commented 8 years ago

I think I can close this, right? Don't hesitate to re-open it if not.

MarkMT commented 8 years ago

Thanks, all good.