Skip to content

Instantly share code, notes, and snippets.

@donrestarone
Created March 4, 2020 02:30
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 donrestarone/c3a4cb905ab7accfa917c7443296915f to your computer and use it in GitHub Desktop.
Save donrestarone/c3a4cb905ab7accfa917c7443296915f to your computer and use it in GitHub Desktop.
simple module for encoding and decoding JWT's using the jwt gem
module CoreModules::JsonWebToken
require 'jwt'
JWT_SECRET = Rails.application.secrets.secret_key_base
def self.encode(payload, exp = 24.hours.from_now)
payload[:exp] = exp.to_i
JWT.encode(payload, JWT_SECRET)
end
def self.decode(token)
begin
body = JWT.decode(token, JWT_SECRET)
if body then HashWithIndifferentAccess.new body[0] else return false end
rescue JWT::ExpiredSignature, JWT::VerificationError => e
return false
rescue JWT::DecodeError, JWT::VerificationError => e
return false
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment