Skip to content
Victor Oliveira Nascimento edited this page Sep 18, 2015 · 1 revision

Custom claims

Joken provides several ways of creating custom claims:

  • fixed value claims: used for claims like issuer, audience or others that are normally fix value.
  • dynamic generated claims: used for claims that need to change based on time.
  • map of claims: passed to the token/1 function.

Fixed value claims

To create fixed value claims like issuer, use the with_claim/3 function. Example:

import Joken
alias Joken.Token

%Token{}
|> with_claim("iss", "Joken") # claim key followed by claim value

Dynamic value claims

Dynamic value claims are generated from functions in the claim_generators map. This function is called on signing. That means generated tokens will have values based on the time of signing. Example:

import Joken
alias Joken.Token

%Token{}
|> with_claim_generator("nbf", fn -> current_time() end)

Passing a map of values

If you have a map of values for claims you can use the token/1 function to initialize the tokens claims. Example:

import Joken

%{"user_id": user[:id]}
|> token