ueberauth/guardian

guardian.encode_and_sign 1st argument: not an atom

gabrielmancini opened this issue · 1 comments

Steps to Reproduce

Hello everyone,

I start with phx.gen.auth and i am trying to add an api endpoint with login capabilities,
i follow the getstart documentation but i had an error:
this is familiar to somebody?

my config.ex

config :backend, Backend.Guardian,
    issuer: "backend",
    secret_key: "the secret key here",
    ttl: {3, :days}

 config :backend, BackendWeb.AuthAccessPipeline,
    module: Backend.Guardian,
    error_handler: BackendWeb.AuthErrorHandler

the error happends here:

 def create(conn = %{body_params: %Schemas.LoginRequest{user: %{email: email, password: password}}}, _params) do
    case Accounts.get_user_by_email_and_password(email, password) do
      %Accounts.User{} = user ->
        IO.inspect(user)
        {:ok, jwt, _full_claims} = Guardian.encode_and_sign(user, %{}) <---- ERROR

        conn
        |> render("create.json", user: user, jwt: jwt)

      nil ->
        conn
        |> put_status(401)
        |> render("error.json", message: "User could not be authenticated")
    end
  end

Expected Result

retrive the token

Actual Result

[error] #PID<0.1421.0> running BackendWeb.Endpoint (connection #PID<0.1404.0>, stream id 4) terminated
Server: localhost:4000 (http)
Request: POST /api/users/log_in
** (exit) an exception was raised:
** (ArgumentError) errors were found at the given arguments:

  • 1st argument: not an atom

    :erlang.apply(#Backend.Accounts.User<__meta__: #Ecto.Schema.Metadata<:loaded, "users">, confirmed_at: nil, customer: #Ecto.Association.NotLoaded<association :customer is not loaded>, customer_id: "6966b1a6-b6db-4e15-9317-d091dc6c14c7", email: "ecolab@iobee.io", id: "81be7e88-e0ce-4cd0-8998-86d599e97c41", inserted_at: ~N[2022-09-28 17:49:52], role: :manager, updated_at: ~N[2022-09-28 18:05:50], ...>, :config, [:token_module, Guardian.Token.Jwt])
    (guardian 2.3.0) lib/guardian.ex:789: Guardian.token_module/1
    (guardian 2.3.0) lib/guardian.ex:593: Guardian.encode_and_sign/4
    (backend 0.1.1) lib/backend_web/controllers/api_user_session_controller.ex:43: BackendWeb.ApiUserSessionController.create/2
    (backend 0.1.1) lib/backend_web/controllers/api_user_session_controller.ex:1: BackendWeb.ApiUserSessionController.action/2
    (backend 0.1.1) lib/backend_web/controllers/api_user_session_controller.ex:1: BackendWeb.ApiUserSessionController.phoenix_controller_pipeline/2
    (phoenix 1.6.11) lib/phoenix/router.ex:354: Phoenix.Router.__call__/2
    (backend 0.1.1) lib/backend_web/endpoint.ex:1: BackendWeb.Endpoint.plug_builder_call/2
    (backend 0.1.1) lib/plug/debugger.ex:136: BackendWeb.Endpoint."call (overridable 3)"/2
    (backend 0.1.1) lib/backend_web/endpoint.ex:1: BackendWeb.Endpoint.call/2
    (phoenix 1.6.11) lib/phoenix/endpoint/cowboy2_handler.ex:54: Phoenix.Endpoint.Cowboy2Handler.init/4
    (cowboy 2.9.0) /home/barba/Development/iobeeio/backend/deps/cowboy/src/cowboy_handler.erl:37: :cowboy_handler.execute/2
    (cowboy 2.9.0) /home/barba/Development/iobeeio/backend/deps/cowboy/src/cowboy_stream_h.erl:306: :cowboy_stream_h.execute/3
    (cowboy 2.9.0) /home/barba/Development/iobeeio/backend/deps/cowboy/src/cowboy_stream_h.erl:295: :cowboy_stream_h.request_process/3
    (stdlib 3.17.2.1) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
    

solved with:

Guardian.encode_and_sign(Backend.Guardian, user, %{})