Joken problem - (UndefinedFunctionError) function Joken.using/1 is undefined or private

Hello I’m facing some problems using Joken to set up JWT for my backend application. I found no solution. I’ve pasted the code below with the error. I think there is a conflict between the versions of Joker

defmodule ApiElixir.TokenManager do
  use Joken

  @secret "secret"

  defp claims() do
      "iss" => "api_elixir",
      "exp" => |> Timex.add(Timex.Duration.from_seconds(3600)) |> Timex.to_unix(),
      "aud" => "user"

  def encode() do
    Joken.sign(%{}, @secret, claims())

  def decode(token) do
    case Joken.verify(token, @secret) do
      {:ok, _claims} -> {:ok, "Token valide"}
      {:error, reason} -> {:error, reason}

Error :

== Compilation error in file lib/token_manager.ex ==
** (UndefinedFunctionError) function Joken.__using__/1 is undefined or private
    (joken 2.6.0) Joken.__using__([])
    lib/token_manager.ex:2: (module)

Thanks all for the help

Welcome! What version of Joken are you using? What docs are you reading?

It s the 2.6 version and for the docs i read the official doc of but for the code you will insult me ^^’ it s a prompt i tell to chatgpt for the example

Instead of ChatGPT, can you try some examples from the Joken docs? Joken Overview — Joken v2.6.0

Yes i will try with the Joken docs thanks

@SkyFlyOne you should use Joken.Config and not Joken.

The docs also show you better intended ways of using Joken’s API. For instance, when “using” the config, you get some generated implementations of encode/decode and some others.

1 Like

I tried with making this

defmodule ApiElixir.Token do
  use Joken.Config

  signer = Joken.Signer.create("HS256", "secret")

  {:ok, token, claims} = ApiElixir.Token.generate_and_sign(%{}, signer)

  extra_claims = %{"user_id" => "some_id"}
  token_with_default_plus_custom_claims = ApiElixir.Token.generate_and_sign!(extra_claims, signer)

  {:ok, claims} = ApiElixir.Token.verify_and_validate(token, signer)


But i have some error like this one

== Compilation error in file lib/api_elixir/token.ex ==
** (UndefinedFunctionError) function ApiElixir.Token.generate_and_sign/2 is undefined (function not available)
    ApiElixir.Token.generate_and_sign(%{}, %Joken.Signer{jwk: #JOSE.JWK<keys: :undefined, fields: %{}, ...>, jws: %JOSE.JWS{alg: {:jose_jws_alg_hmac, :HS256}, b64: :undefined, fields: %{"typ" => "JWT"}}, alg: "HS256"})
    lib/api_elixir/token.ex:6: (module)

Yes i tried with Joken.Config instead of just Joken but it doesnt work. So now i retried basing on Joken docs but i always have some problem that you can see in my previous post. I m beginner with Elixir and the environnement and basically i dont have some greats skills in dev.

Have you tried putting the statements inside a function inside your module?

1 Like

@SkyFlyOne here is one working example:

# Without a module token
signer = Joken.Signer.create("HS256", "secret")
Joken.generate_and_sign(%{}, %{}, signer) # => will generate an empty token
Joken.generate_and_sign(%{}, %{"user_id" => "some_id"}, signer) # with a claim

# With a module token
defmodule ApiToken do
  use Joken.Config
ApiToken.generate_and_sign(%{}, signer) # will use defult config
ApiToken.generate_and_sign(%{"user_id" => "someid"}, signer) # will add extra claims