Skip to content

Instantly share code, notes, and snippets.

@wajeeh-devsinc
Created January 30, 2020 10:55
Show Gist options
  • Save wajeeh-devsinc/2632c95785e2f1a59d1d2c6e94ccceef to your computer and use it in GitHub Desktop.
Save wajeeh-devsinc/2632c95785e2f1a59d1d2c6e94ccceef to your computer and use it in GitHub Desktop.
Rails User Authentication with Devise and simple_token_authentication
class Api::V1::SessionsController < Devise::SessionsController
before_action :sign_in_params, only: :create
before_action :load_user, only: :create
# sign in
def create
if @user.valid_password?(sign_in_params[:password])
sign_in "user", @user
render json: {
messages: "Signed In Successfully",
is_success: true,
data: {user: @user}
}, status: :ok
else
render json: {
messages: "Signed In Failed - Unauthorized",
is_success: false,
data: {}
}, status: :unauthorized
end
end
private
def sign_in_params
params.require(:sign_in).permit :email, :password
end
def load_user
@user = User.find_for_database_authentication(email: sign_in_params[:email])
if @user
return @user
else
render json: {
messages: "Cannot get User",
is_success: false,
data: {}
}, status: :failure
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment