Module: RailsJwt::Authentication

Defined in:
app/controllers/rails_jwt/authentication.rb

Instance Method Summary collapse

Instance Method Details

#authenticate_userObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/controllers/rails_jwt/authentication.rb', line 4

def authenticate_user
    begin
        payload = jwt_payload

        expiry_time = Time.at(payload["exp"])
        if Time.now > expiry_time
            render status: :unauthorized    
        end
        user = User.find(payload["sub"]["id"])
        if !user.active
            render status: :unauthorized
        end

    rescue ActiveRecord::RecordNotFound
        render status: :unauthorized
    rescue JWT::DecodeError
        render status: :unauthorized
    end
end

#current_userObject



24
25
26
27
# File 'app/controllers/rails_jwt/authentication.rb', line 24

def current_user
    payload = jwt_payload
    User.find(payload["sub"]["id"])
end