Module: JWT::Auth::Authentication

Defined in:
lib/jwt/auth/authentication.rb

Overview

Controller methods

Instance Method Summary collapse

Instance Method Details

#current_userObject

Current user



14
15
16
# File 'lib/jwt/auth/authentication.rb', line 14

def current_user
  token&.subject
end

#require_tokenObject

Require a token to be present

Apply this filter for API actions that require an access token



60
61
62
# File 'lib/jwt/auth/authentication.rb', line 60

def require_token
  raise JWT::Auth::UnauthorizedError if token.nil?
end

#set_access_token(user = current_user) ⇒ Object

Set API token in the response



67
68
69
# File 'lib/jwt/auth/authentication.rb', line 67

def set_access_token(user = current_user)
  set_header JWT::Auth::AccessToken.new(:subject => user)
end

#set_refresh_token(user = current_user) ⇒ Object

Set refresh token in the response



74
75
76
# File 'lib/jwt/auth/authentication.rb', line 74

def set_refresh_token(user = current_user)
  set_header JWT::Auth::RefreshToken.new(:subject => user)
end

#validate_access_tokenObject

Authenticate the user with the token

Apply this filter for API actions that need an access token This filter does not enforce token presence



37
38
39
# File 'lib/jwt/auth/authentication.rb', line 37

def validate_access_token
  raise JWT::Auth::UnauthorizedError unless header.nil? || token.is_a?(AccessToken)
end

#validate_refresh_tokenObject

Validate a refresh token

Apply this filter for the API token refresh action This filter does not enforce token presence



49
50
51
# File 'lib/jwt/auth/authentication.rb', line 49

def validate_refresh_token
  raise JWT::Auth::UnauthorizedError unless header.nil? || token.is_a?(RefreshToken)
end

#validate_tokenObject

Validate a token (if it’s present)

Apply this before_action filter for every API action



25
26
27
# File 'lib/jwt/auth/authentication.rb', line 25

def validate_token
  raise JWT::Auth::UnauthorizedError unless token.nil? || token&.valid?
end