Issue with implementing A Token Based Authentication(for API) Via Coherence

Hello, Does anyone have any experience of building a Token Based API Authentication using Coherence ? I’m running into some issues with my code present as part of this PR - [WIP] Add Token Based User Auth using Coherence by boddhisattva · Pull Request #1 · boddhisattva/user_auth · GitHub.

When I try to create a DB and run the migrations I get the below error:

➜  user_auth git:(add_auth) ✗ mix ecto.create && mix ecto.migrate
Compiling 22 files (.ex)
warning: variable "token" does not exist and is being expanded to "token()", please use parentheses to remove the ambiguity or change the variable name
  lib/user_auth_web/channels/user_socket.ex:26


== Compilation error in file lib/user_auth_web/channels/user_socket.ex ==
** (CompileError) lib/user_auth_web/channels/user_socket.ex:26: undefined function token/0
    (stdlib) lists.erl:1338: :lists.foreach/2
    (stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
    (elixir) lib/kernel/parallel_compiler.ex:121: anonymous fn/4 in Kernel.ParallelCompiler.spawn_compilers/1
➜  user_auth git:(add_auth) ✗

As per the README here , I’m supposed to add the below Token credentials:

token = Coherence.Authentication.Token.generate_token
Coherence.CredentialStore.Server.put_credentials(token, %{role: :admin})

But I’m not exactly sure where should I add them. I tried adding them as a part of the def connect in user_socket.ex like(I’m still not sure where should the line Coherence.CredentialStore.Server.put_credentials(token, %{role: :admin}) go):

  def connect(_params, socket) do
    token = Coherence.Authentication.Token.generate_token

    case Coherence.verify_user_token(socket, token, &assign/3) do
      {:error, _} -> :error
      {:ok, socket} -> {:ok, socket}
    end
  end

but then I ran into another error:

➜ user_auth git:(add_auth) ✗ mix ecto.create && mix ecto.migrate
Compiling 22 files (.ex)

== Compilation error in file lib/user_auth_web/router.ex ==
** (KeyError) key :source not found in:
(elixir) lib/keyword.ex:371: Keyword.fetch!/2
lib/coherence/plugs/authorization/token.ex:82: Coherence.Authentication.Token.init/1
(plug) lib/plug/builder.ex:193: Plug.Builder.init_module_plug/3
(plug) lib/plug/builder.ex:181: anonymous fn/4 in Plug.Builder.compile/3
(elixir) lib/enum.ex:1826: Enum.“-reduce/3-lists^foldl/2-0-”/3
(plug) lib/plug/builder.ex:181: Plug.Builder.compile/3
lib/user_auth_web/router.ex:23: (module)
(stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
(elixir) lib/kernel/parallel_compiler.ex:121: anonymous fn/4 in Kernel.ParallelCompiler.spawn_compilers/1
➜ user_auth git:(add_auth) ✗

I’m a beginner to Phoenix so it’s quite possible that I could be missing something. Any tips on how to debug this would be really helpful…

First error message explains that the 26th line of user_socket.ex is calling token which is not defined or passed to its function. Check the params of the connect function to use token in it.

Second error seems, though I’m not sure, token is not generated. See the plug https://hexdocs.pm/coherence/Coherence.Authentication.Token.html#content