Created
March 4, 2020 02:30
-
-
Save donrestarone/c3a4cb905ab7accfa917c7443296915f to your computer and use it in GitHub Desktop.
simple module for encoding and decoding JWT's using the jwt gem
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
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