ueberauth / guardian

Elixir Authentication
MIT License
3.4k stars 381 forks source link

guardian.encode_and_sign 1st argument: not an atom #710

Closed gabrielmancini closed 1 year ago

gabrielmancini commented 1 year ago

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:

gabrielmancini commented 1 year ago

solved with:

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