User Login for Development

Now I use the stone devise, and after successful authorization, it redirects the user to the root page.

How to change the action sign_into return a json array as follows: {"auth_result":"1"}instead of redirecting?

Is it possible? I did not find recipes for them in wiki development.

+3
source share
1 answer

Devise does not currently handle JSON responses - see the issue on github . Therefore, you will need to create a custom session_controller to handle your response. Take a look at the implementation on github to get an idea. I put together some rough pseudo codes that hopefully get you started:

class Users::SessionsController < Devise::SessionsController
  def create
    resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new")
    set_flash_message :notice, :signed_in
    # Remove sign_in_and_redirect(resource_name, resource) and replace with the following, I think
    sign_in(resource_name, resource)
    respond_to do |format|
      format.json { render :json => hash_of_things_to_return.to_json }
    end
  end
end

Then in the routes.rb file you will need to specify a new controller - devise_for :users, :controllers => { :sessions => "users/sessions" }

Hope this helps!

+2
source

Source: https://habr.com/ru/post/1795575/


All Articles