Class: AuthToken

Inherits:
Object
  • Object
show all
Defined in:
app/lib/auth_token.rb

Overview

this class generates the JWT using this gem github.com/jwt/ruby-jwt/

Author:

Class Method Summary (collapse)

Class Method Details

+ (Object) decode(token, leeway = nil)

Decode a token and return the payload inside If will throw an error if expired or invalid. See the docs for the JWT gem.



17
18
19
20
21
# File 'app/lib/auth_token.rb', line 17

def self.decode(token, leeway = nil)
  # https://github.com/jwt/ruby-jwt/blob/master/lib/jwt.rb#L97
  decoded = JWT.decode(token, '''', leeway: leeway)
  HashWithIndifferentAccess.new(decoded[0])
end

+ (Object) encode(payload, ttl_in_minutes = 60 * 24 * 30)

Encode a hash in a json web token token stays valid for a month



6
7
8
9
10
11
12
13
# File 'app/lib/auth_token.rb', line 6

def self.encode(payload, ttl_in_minutes = 60 * 24 * 30)
  payload[:exp] = ttl_in_minutes.minutes.from_now.to_i
  # https://github.com/jwt/ruby-jwt/blob/master/lib/jwt.rb#L88
  # can use Rails.application.secrets.secret_key_base as key base
  # but using empty string to reduce dependencies/make deployment easier/
  # ensure it's runnable by other members
  JWT.encode(payload, '')
end