Module: RailsJwtAuth::AuthenticableHelper

Included in:
InvitationsController, ProfilesController, SessionsController
Defined in:
app/controllers/concerns/rails_jwt_auth/authenticable_helper.rb

Instance Method Summary collapse

Instance Method Details

#authenticateObject



35
36
37
38
39
40
41
42
43
44
# File 'app/controllers/concerns/rails_jwt_auth/authenticable_helper.rb', line 35

def authenticate
  begin
    @jwt_payload = RailsJwtAuth::JwtManager.decode(get_jwt_from_request).first
    @current_user = RailsJwtAuth.model.from_token_payload(@jwt_payload)
  rescue JWT::ExpiredSignature, JWT::VerificationError, JWT::DecodeError
    @current_user = nil
  end

  track_request
end

#authenticate!Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/controllers/concerns/rails_jwt_auth/authenticable_helper.rb', line 21

def authenticate!
  begin
    @jwt_payload = RailsJwtAuth::JwtManager.decode(get_jwt_from_request).first
  rescue JWT::ExpiredSignature, JWT::VerificationError, JWT::DecodeError
    unauthorize!
  end

  if !@current_user = RailsJwtAuth.model.from_token_payload(@jwt_payload)
    unauthorize!
  else
    track_request
  end
end

#current_userObject



5
6
7
# File 'app/controllers/concerns/rails_jwt_auth/authenticable_helper.rb', line 5

def current_user
  @current_user
end

#get_jwt_from_requestObject



17
18
19
# File 'app/controllers/concerns/rails_jwt_auth/authenticable_helper.rb', line 17

def get_jwt_from_request
  request.env['HTTP_AUTHORIZATION']&.split&.last
end

#jwt_payloadObject



9
10
11
# File 'app/controllers/concerns/rails_jwt_auth/authenticable_helper.rb', line 9

def jwt_payload
  @jwt_payload
end

#signed_in?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'app/controllers/concerns/rails_jwt_auth/authenticable_helper.rb', line 13

def signed_in?
  !current_user.nil?
end

#track_requestObject



50
51
52
53
54
# File 'app/controllers/concerns/rails_jwt_auth/authenticable_helper.rb', line 50

def track_request
  if @current_user&.respond_to? :update_tracked_request_info
    @current_user.update_tracked_request_info(request)
  end
end

#unauthorize!Object

Raises:



46
47
48
# File 'app/controllers/concerns/rails_jwt_auth/authenticable_helper.rb', line 46

def unauthorize!
  raise NotAuthorized
end