I have the following problems with dives in rails.
All requests and response are JSON
After I sign in /partners/sign_in I get the following response:
HTTP/1.1 200 OK {"success":true}
And after i signed out /partners/sign_out I get thie following response:
HTTP/1.1 200 OK {"success":true}
Now my Question: How can I create my own RegisterController and HOW it looks like, are there any examples? I have searched at github/devise but not find any examples.
I want to response something other, and if the authentication fails I want to response HTTP 404 ERROR
routes.rb
devise_for :partners, :controllers => { :sessions => "sessions" }
session_controller.rb
class SessionsController < Devise::SessionsController
def create
resource = warden.authenticate!(:scope => resource_name, :recall => :failure)
return sign_in_and_redirect(resource_name, resource)
end
def destroy
redirect_path = after_sign_out_path_for(resource_name)
signed_out = (Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name))
set_flash_message :notice, :signed_out if signed_out
respond_to do |format|
format.html { redirect_to redirect_path }
format.json { render :json => {:success => true} }
end
end
private
def sign_in_and_redirect(resource_or_scope, resource=nil)
scope = Devise::Mapping.find_scope!(resource_or_scope)
resource ||= resource_or_scope
sign_in(scope, resource) unless warden.user(scope) == resource
return render :json => {:success => true}
end
def failure
return render:json => {:success => false, :errors => ["Login failed."]}
end
end
The ActionController's render method takes hash with :status as the key. Here you can specify the HTTP error status code or the symbol for that status code:
render json: {success: false, errors: ["Login Failed"]}, status: 404
# or the following is a preferred way
render json: {success: false, errors: ["Login Failed"]}, status: :not_found
Here the documentation for render method.
And here is an excellent documentation by Cody Fauser of rails status code to symbol mapping
One Observation:
It is not idiomatic in Ruby to use return statements at the end of a function because the last expression's return value is the return value of the function.