Skip to content

Instantly share code, notes, and snippets.

@piyushawasthi
Created July 29, 2019 07:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save piyushawasthi/a7fd9a42dd863ed1b6bb0fbc92890175 to your computer and use it in GitHub Desktop.
Save piyushawasthi/a7fd9a42dd863ed1b6bb0fbc92890175 to your computer and use it in GitHub Desktop.
Json web token implementation
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