Skip to content

Instantly share code, notes, and snippets.

@indyarocks
Created March 16, 2017 04:35
Show Gist options
  • Save indyarocks/9228f38733bf82752fb7c18ad07f825d to your computer and use it in GitHub Desktop.
Save indyarocks/9228f38733bf82752fb7c18ad07f825d to your computer and use it in GitHub Desktop.
Auth Module for JWT Authentication
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