Token authentication with Rails and Devise

后端 未结 2 769
故里飘歌
故里飘歌 2021-02-02 04:04

I\'m following this excellent article to setup the authentification part of my rails (3.2) API:
http://blog.joshsoftware.com/2011/12/23/designing-rails-api-using-rabl-and-de

相关标签:
2条回答
  • 2021-02-02 04:44

    Regarding Luc's question, that's my understanding as well.

    For example, using default non-API login, SessionsController.create handles the login. How do I retrieve the authentication_token and reuse it as part of the API calls later? Also, how do I override the SessionsController.create to call resource.reset_authentication_token! ?

    0 讨论(0)
  • 2021-02-02 04:50

    It's my fault, I'll update the blog post. You need to add the following code to create the user in you registration controller

    if params[:api_key].blank? or params[:api_key] != API_KEY
      render :json => {'errors'=>{'api_key' => 'Invalid'}}.to_json, :status => 401
      return
    end
    build_resource
    if resource.save
       sign_in(resource)
       resource.reset_authentication_token!
       #rabl template with authentication token
       render :template => '/devise/registrations/signed_up' 
    else
       render :template => '/devise/registrations/new' #rabl template with errors 
    end
    

    Let me know if you face any problem?

    0 讨论(0)
提交回复
热议问题