Created
July 29, 2019 07:15
-
-
Save piyushawasthi/a7fd9a42dd863ed1b6bb0fbc92890175 to your computer and use it in GitHub Desktop.
Json web token implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'jwt' | |
class JsonWebToken | |
def self.encode(payload, expiration = Rails.application.secrets.jwt_expiration_seconds.to_i.seconds.from_now) | |
payload = payload.dup | |
payload[:exp] = expiration.to_i | |
JWT.encode(payload, Rails.application.secrets.hmac_secret_key) | |
end | |
def self.decode(token) | |
JWT.decode(token, Rails.application.secrets.hmac_secret_key) | |
rescue JWT::ExpiredSignature, JWT::DecodeError | |
false | |
end | |
def self.decode_to_payload(token) | |
decode(token).first.except('exp').with_indifferent_access | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment