Module: SolidusJwt::Encodeable

Included in:
SolidusJwt
Defined in:
lib/solidus_jwt/concerns/encodeable.rb

Instance Method Summary collapse

Instance Method Details

#encode(payload:, expires_in: nil) ⇒ String

Encode a specified payload

Parameters:

  • payload (Hash)

    Attributes to place within the jwt

  • expires_in (Integer) (defaults to: nil)

    How long until token expires in Seconds (Optional). Note that if no expires at is set, then the token will last forever.

Returns:

  • (String)

See Also:



12
13
14
15
16
17
18
19
20
21
# File 'lib/solidus_jwt/concerns/encodeable.rb', line 12

def encode(payload:, expires_in: nil)
  # @see https://github.com/jwt/ruby-jwt#support-for-reserved-claim-names
  extras = {}
  extras['exp'] = Time.current.to_i + expires_in if expires_in.present?
  extras['iat'] = Time.current

  payload = extras.merge(payload).as_json
  JWT.encode(payload, SolidusJwt::Config.jwt_secret,
    SolidusJwt::Config.jwt_algorithm)
end