create basic backend with login
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
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
|
||||
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
|
||||
Reference in New Issue
Block a user