Skip to content

Instantly share code, notes, and snippets.

@renatoalencar
Created October 13, 2017 14:52
Show Gist options
  • Select an option

  • Save renatoalencar/58836191da06a5855e0a1e3a0ed4504f to your computer and use it in GitHub Desktop.

Select an option

Save renatoalencar/58836191da06a5855e0a1e3a0ed4504f to your computer and use it in GitHub Desktop.
class AuthenticationService < ApplicationService
validates :user_present?,
I18n.t("devise.failure.invalid"),
Errors::AuthenticationError
validates :check_locked,
I18n.t("devise.failure.locked"),
Errors::AuthenticationError
validates :valid_credentials?,
I18n.t("devise.failure.invalid"),
Errors::AuthenticationError
def initialize(user, password)
@user = user
@password = password
end
def perform
super do
JWTWrapper.encode user_id: @user.id
end
end
private
def user_present?
@user.present?
end
def valid_credentials?
user_present? && valid_for_authentication?
end
def valid_for_authentication?
@user.valid_for_authentication? {
@user.valid_password?(@password)
}
end
def check_locked
!@user.access_locked?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment