Skip to content

Instantly share code, notes, and snippets.

@afiq90
Created July 29, 2015 16:40
Show Gist options
  • Save afiq90/ca1fd545821469f2865d to your computer and use it in GitHub Desktop.
Save afiq90/ca1fd545821469f2865d to your computer and use it in GitHub Desktop.
sessions_controller.rb
class SessionsController < Devise::SessionsController
before_filter :authenticate_user!, :except => [:create, :destroy]
respond_to :json
def create
resource = User.find_for_database_authentication(:email => params[:email])
return invalid_login_attempt unless resource
if resource.valid_password?(params[:password])
sign_in(:user, resource)
resource.ensure_authentication_token!
render :json=> {:success=>true, :auth_token=>resource.authentication_token, :email=>resource.email}
return
end
invalid_login_attempt
end
def destroy
resource = User.find_for_database_authentication(:email => params[:email])
resource.authentication_token = nil
resource.save
render :json=> {:success=>true}
end
protected
def invalid_login_attempt
render :json=> {:success=>false, :message=>"Error with your login or password"}, :status=>401
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment