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

Examples:

encode data into token

payload = {
  sub: 1,
  iat: DateTime.current.to_i,
  exp: 1.hour.from_now.to_i
}

SolidusJwt.encode payload: payload
#=> 'eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOjEsImlhdCI6MTU4NDEzMjExOCwiZXhwIj
 oxNTg0MTM1NzE4LCJpc3MiOiJzb2xpZHVzIn0.OKZOGlawx435GdgKp2AGD8SKxW7sqn0h-Ef2qdVSxqQ'

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:



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/solidus_jwt/concerns/encodeable.rb', line 25

def encode(payload:, expires_in: nil)
  jwt_payload = payload.dup.with_indifferent_access

  current_time = Time.current.to_i

  # @see https://github.com/jwt/ruby-jwt#support-for-reserved-claim-names
  jwt_payload[:exp] ||= current_time + expires_in.to_i if expires_in.present?
  jwt_payload[:iat] ||= current_time
  jwt_payload[:iss] ||= 'solidus'

  JWT.encode(jwt_payload, SolidusJwt::Config.jwt_secret,
    SolidusJwt::Config.jwt_algorithm)
end