Created
March 16, 2017 04:35
-
-
Save indyarocks/9228f38733bf82752fb7c18ad07f825d to your computer and use it in GitHub Desktop.
Auth Module for JWT Authentication
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 Auth | |
ALGORITHM = 'HS256' | |
class << self | |
def issue(payload, exp = 24.hours.from_now) | |
payload[:exp] = exp.to_i | |
JWT.encode(payload, auth_secret, ALGORITHM) | |
end | |
def decode(token) | |
body = JWT.decode(token, auth_secret, true, { algorithm: ALGORITHM })[0] | |
HashWithIndifferentAccess.new body | |
rescue | |
nil | |
end | |
def auth_secret | |
ENV['AUTH_SECRET'] || Rails.application.secrets.secret_key_base | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment