Files
ruby-sea-battle/main-server/app/services/jwt_service.rb
T
2026-09-22 09:07:13 +03:00

18 lines
500 B
Ruby

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