SkyFlyOne

SkyFlyOne

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

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

First Post!

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

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

Most Liked

victorolinasc

victorolinasc

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

Hermanverschooten

Hermanverschooten

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

Last Post!

victorolinasc

victorolinasc

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

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

We're in Beta

About us Mission Statement