ueberauth / guardian

Elixir Authentication
MIT License
3.43k stars 382 forks source link

Guardian.Plug.UnauthenticatedError #605

Closed tamtran-agilityio closed 5 years ago

tamtran-agilityio commented 5 years ago

`case Auth.authenticate(email, password) do {:ok, user} -> new_conn = Guardian.Plug.sign_in(conn, user) jwt = Guardian.Plug.current_token(new_conn)

 new_conn
    |> put_status(:created)
    |> render("show.json", user: user, jwt: jwt)

  :error ->
    conn
    |> put_status(:unauthorized)
    |> render("error.json", error: "User or email invalid")
end`

Have error: ** (exit) an exception was raised: web_1 | ** (Guardian.Plug.UnauthenticatedError) {:error, :secret_not_found} web_1 | (guardian) lib/guardian/plug.ex:380: Guardian.Plug.handle_unauthenticated/3

This is my config # in each environment config file you should overwrite this if it's external config :guardian, Nailer.Guardian, allowed_algos: ["HS512", "RS256"], # optional ttl: {30, :days}, allowed_drift: 2000, secret_key: "H6UqD/bw0LvwzNxtX4FZibQi8WnHrX3mdG/cDl8xlWFPC2tPZnOc+N5FGJYuBgSe", issuer: "nailer"

You can help me, thanks!

yordis commented 5 years ago

{:error, :secret_not_found} <---

Are you sure you are using the correct Guardian module?

tamtran-agilityio commented 5 years ago

@yordis This is file using

defmodule Nailer.Guardian do
  use Guardian, otp_app: :nailer
  import Plug.Conn

  alias Nailer.Accounts

  def subject_for_token(user, _claims) do
    {:ok, to_string(user.id)}
  end

  def resource_from_claims(%{"sub" => id}) do
    case Accounts.get_user(id) do
      nil -> {:error, :resource_not_found}
      user -> {:ok, user}
    end
  end

end

defmodule Nailer.Guardian.ErrorHandler do
  import Plug.Conn

  @behaviour Guardian.Plug.ErrorHandler

  @impl Guardian.Plug.ErrorHandler
  def auth_error(conn, {type, reason}, _opts) do
    body = Jason.encode!(%{message: to_string(type)})
    send_resp(conn, 401, body)
  end
end
yordis commented 5 years ago

https://github.com/ueberauth/guardian/issues/523 https://github.com/ueberauth/guardian/issues/559

Did this help?

tamtran-agilityio commented 5 years ago

I have update secret_key. It working now, thanks all!