Created
December 6, 2020 07:04
-
-
Save jianhandev/09ef047b2b1490c15710e9377d502190 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This class authenticates a valid user by generating a JWT token | |
class AuthenticateUser | |
def initialize(email, password) | |
@email, @password = email, password | |
end | |
def token | |
JsonWebToken.encode({ user_id: user.id }) | |
end | |
attr_reader :email, :password | |
private | |
def user | |
user = User.find_by(email: email) | |
if user && user.authenticate(password) | |
return user | |
else | |
raise(ExceptionHandler::AuthenticationError, | |
Message.invalid_credentials) | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AuthenticationController < ApplicationController | |
skip_before_action :authorize_request | |
def login | |
token = AuthenticateUser | |
.new(auth_params[:email], auth_params[:password]) | |
.token | |
render json: { user: user, token: token} | |
end | |
private | |
def user | |
User | |
.find_by(email: auth_params[:email]) | |
.as_json(except: :password_digest) | |
end | |
def auth_params | |
params.require(:authentication).permit(:email, :password) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class JsonWebToken | |
class << self | |
HMAC_SECRET = Rails.application.credentials.secret_key_base | |
def encode(payload) | |
# token expires in 30 minutes | |
payload[:exp] = Time.now.to_i + 3600 | |
JWT.encode(payload, HMAC_SECRET) | |
end | |
def decode(token) | |
begin | |
body = JWT.decode(token, HMAC_SECRET)[0] | |
HashWithIndifferentAccess.new(body) | |
rescue JWT::DecodeError | |
raise(ExceptionHandler::InvalidToken, | |
Message.invalid_token) | |
nil | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment