2021-07-01 07:24:35 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2021-03-31 16:18:42 +00:00
|
|
|
class JsonWebToken
|
2021-04-29 06:41:23 +00:00
|
|
|
class << self
|
2021-05-03 12:51:10 +00:00
|
|
|
def encode(payload, exp = 30.days.from_now)
|
2021-04-29 06:41:23 +00:00
|
|
|
payload[:exp] = exp.to_i
|
2021-07-01 07:24:35 +00:00
|
|
|
JWT.encode(payload, ENV.fetch("SECRET_KEY_BASE"))
|
2021-03-31 16:18:42 +00:00
|
|
|
end
|
2021-04-29 06:41:23 +00:00
|
|
|
|
|
|
|
|
def decode(token)
|
2021-07-01 07:24:35 +00:00
|
|
|
body = JWT.decode(token, ENV.fetch("SECRET_KEY_BASE"))[0]
|
2021-04-29 06:41:23 +00:00
|
|
|
HashWithIndifferentAccess.new body
|
|
|
|
|
rescue StandardError
|
|
|
|
|
nil
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|