Module: SimpleJwtAuth::ApplicationHelper

Defined in:
app/helpers/simple_jwt_auth/application_helper.rb

Instance Method Summary collapse

Instance Method Details

#authenticate_userObject



23
24
25
26
27
# File 'app/helpers/simple_jwt_auth/application_helper.rb', line 23

def authenticate_user
  unless current_user
    render json: {}, status: :unauthorized
  end
end

#current_userObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/helpers/simple_jwt_auth/application_helper.rb', line 5

def current_user
  auth_headers = request.headers["Authorization"]
  if auth_headers.present? && auth_headers[/(?<=\A(Bearer ))\S+\z/]
    token = auth_headers[/(?<=\A(Bearer ))\S+\z/]
    begin
      decoded_token = JWT.decode(
        token,
        Rails.application.credentials.fetch(:secret_key_base),
        true,
        { algorithm: "HS256" }
      )
      User.find_by(id: decoded_token[0]["user_id"])
    rescue JWT::ExpiredSignature
      nil
    end
  end
end