class DecodeAuthenticationCommand
Authentication implementation mostly copied and slightly adapted from paweljw.github.io/2017/07/rails-5.1-api-app-part-4-authentication-and-authorization/ Big thanks!
Attributes
headers[R]
Public Class Methods
new(headers)
click to toggle source
# File natural-backend/app/commands/decode_authentication_command.rb, line 10 def initialize(headers) @headers = headers @user = nil end
Private Instance Methods
authentication_header()
click to toggle source
# File natural-backend/app/commands/decode_authentication_command.rb, line 35 def authentication_header headers['Authentication'] end
decoded_id()
click to toggle source
# File natural-backend/app/commands/decode_authentication_command.rb, line 47 def decoded_id token_contents['user_id'] end
run()
click to toggle source
# File natural-backend/app/commands/decode_authentication_command.rb, line 15 def run return unless token_present? @result = user if user end
token()
click to toggle source
# File natural-backend/app/commands/decode_authentication_command.rb, line 29 def token return authentication_header.split(' ').last if authentication_header.present? errors.add(:token, "Token missing") nil end
token_contents()
click to toggle source
# File natural-backend/app/commands/decode_authentication_command.rb, line 39 def token_contents @token_contents ||= begin decoded = JwtService.decode(token) errors.add(:token, "Token expired") unless decoded decoded end end
token_present?()
click to toggle source
# File natural-backend/app/commands/decode_authentication_command.rb, line 25 def token_present? token.present? && token_contents.present? end
user()
click to toggle source
# File natural-backend/app/commands/decode_authentication_command.rb, line 20 def user @user ||= User.find_by(id: decoded_id) @user || errors.add(:token, "Token invalid") && nil end