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

**URL:** <https://forum.elixirforum.com/t/joken-problem-undefinedfunctionerror-function-joken-using-1-is-undefined-or-private/59594>\
**Category:** Questions\
**Tags:** joken\
**Created:** [November 9, 2023, 1:58pm UTC](https://forum.elixirforum.com/t/joken-problem-undefinedfunctionerror-function-joken-using-1-is-undefined-or-private/59594 "2023-11-09T13:58:42Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![SkyFlyOne](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/s/779978/32.png) [@SkyFlyOne](https://forum.elixirforum.com/u/SkyFlyOne)\
**Post date:** [November 9, 2023, 1:58pm UTC](https://forum.elixirforum.com/t/joken-problem-undefinedfunctionerror-function-joken-using-1-is-undefined-or-private/59594/1 "2023-11-09T13:58:42Z")

</div>

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

```elixir
defmodule ApiElixir.TokenManager do
  use Joken

  @secret "secret"

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

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

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

```

Error :

```plaintext
== 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

---

<div class="post-metadata">

**Author:** ![benwilson512](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/benwilson512/32/1457_2.png) [@benwilson512](https://forum.elixirforum.com/u/benwilson512)\
**Post date:** [November 9, 2023, 1:59pm UTC](https://forum.elixirforum.com/t/joken-problem-undefinedfunctionerror-function-joken-using-1-is-undefined-or-private/59594/2 "2023-11-09T13:59:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![SkyFlyOne](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/s/779978/32.png) [@SkyFlyOne](https://forum.elixirforum.com/u/SkyFlyOne)\
**Post date:** [November 10, 2023, 9:03am UTC](https://forum.elixirforum.com/t/joken-problem-undefinedfunctionerror-function-joken-using-1-is-undefined-or-private/59594/3 "2023-11-10T09:03:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![benwilson512](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/benwilson512/32/1457_2.png) [@benwilson512](https://forum.elixirforum.com/u/benwilson512)\
**Post date:** [November 10, 2023, 9:10am UTC](https://forum.elixirforum.com/t/joken-problem-undefinedfunctionerror-function-joken-using-1-is-undefined-or-private/59594/4 "2023-11-10T09:10:07Z")

</div>

Instead of ChatGPT, can you try some examples from the Joken docs? [Joken Overview — Joken v2.6.2](https://hexdocs.pm/joken/introduction.html#usage)

---

<div class="post-metadata">

**Author:** ![SkyFlyOne](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/s/779978/32.png) [@SkyFlyOne](https://forum.elixirforum.com/u/SkyFlyOne)\
**Post date:** [November 10, 2023, 1:30pm UTC](https://forum.elixirforum.com/t/joken-problem-undefinedfunctionerror-function-joken-using-1-is-undefined-or-private/59594/5 "2023-11-10T13:30:55Z")

</div>

Yes i will try with the Joken docs thanks

---

<div class="post-metadata">

**Author:** ![victorolinasc](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/victorolinasc/32/10667_2.png) [@victorolinasc](https://forum.elixirforum.com/u/victorolinasc)\
**Post date:** [November 10, 2023, 1:38pm UTC](https://forum.elixirforum.com/t/joken-problem-undefinedfunctionerror-function-joken-using-1-is-undefined-or-private/59594/6 "2023-11-10T13:38:39Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![SkyFlyOne](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/s/779978/32.png) [@SkyFlyOne](https://forum.elixirforum.com/u/SkyFlyOne)\
**Post date:** [November 10, 2023, 2:56pm UTC](https://forum.elixirforum.com/t/joken-problem-undefinedfunctionerror-function-joken-using-1-is-undefined-or-private/59594/7 "2023-11-10T14:56:33Z")

</div>

I tried with making this

```elixir
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)

end

```

But i have some error like this one

```elixir
== 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)

```

---

<div class="post-metadata">

**Author:** ![SkyFlyOne](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/s/779978/32.png) [@SkyFlyOne](https://forum.elixirforum.com/u/SkyFlyOne)\
**Post date:** [November 10, 2023, 2:59pm UTC](https://forum.elixirforum.com/t/joken-problem-undefinedfunctionerror-function-joken-using-1-is-undefined-or-private/59594/8 "2023-11-10T14:59:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Hermanverschooten](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hermanverschooten/32/6447_2.png) [@Hermanverschooten](https://forum.elixirforum.com/u/Hermanverschooten)\
**Post date:** [November 11, 2023, 8:59am UTC](https://forum.elixirforum.com/t/joken-problem-undefinedfunctionerror-function-joken-using-1-is-undefined-or-private/59594/9 "2023-11-11T08:59:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![victorolinasc](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/victorolinasc/32/10667_2.png) [@victorolinasc](https://forum.elixirforum.com/u/victorolinasc)\
**Post date:** [November 11, 2023, 6:00pm UTC](https://forum.elixirforum.com/t/joken-problem-undefinedfunctionerror-function-joken-using-1-is-undefined-or-private/59594/10 "2023-11-11T18:00:51Z")

</div>

@SkyFlyOne here is one working example:

```elixir
# 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
end
ApiToken.generate_and_sign(%{}, signer) # will use defult config
ApiToken.generate_and_sign(%{"user_id" => "someid"}, signer) # will add extra claims

```
