class JwtService SECRET_KEY = Rails.application.secret_key_base ALGORITHM = "HS256" EXPIRATION = 7.days def self.encode(payload) payload[:exp] = EXPIRATION.from_now.to_i if payload[:exp] == nil JWT.encode(payload, SECRET_KEY, ALGORITHM) end def self.decode(token) decoded = JWT.decode(token, SECRET_KEY, true, { algorithm: ALGORITHM }).first HashWithIndifferentAccess.new(decoded).slice(:user_id, :exp) rescue JWT::DecodeError, JWT::ExpiredSignature nil end end