ueberauth / guardian

Elixir Authentication
MIT License
3.43k stars 382 forks source link

Hooks doesn't work #135

Closed madshargreave closed 8 years ago

madshargreave commented 8 years ago

Hi

I am trying to hook into the authentication process by overriding after_encode_and_sign, but it is never called

web/hooks/guardian.ex

defmodule Resend.GuardianHooks do
  use Guardian.Hooks

  def after_encode_and_sign(_, _, _, _) do
    IO.puts("after_encode_and_sign")
    IO.puts("after_encode_and_sign")
    IO.puts("after_encode_and_sign")
    IO.puts("after_encode_and_sign")
  end

end

web/controllers/session_controller

defmodule Resend.SessionController do
  use Resend.Web, :controller
  alias Resend.User

  def create(conn, params = %{ "email" => email, "password" => password }) do
    case User.authenticate(params) do
      {:ok, user} ->
        IO.inspect "ENCODING USER"
        {:ok, jwt, full_claims} = Guardian.encode_and_sign(user)
        IO.inspect "ENCODING DONE"

        conn |> render(Resend.SessionView, "show.json", %{user: user, token: jwt})
      {:error, message} ->
        conn
        |> put_status(:unauthorized)
        |> render(Resend.ErrorView, "error.json", error: "Invalid email and/or password")
    end
  end
  ...
doomspork commented 8 years ago

@madshargreave did you add the hooks to your configuration?

config :guardian, Guardian,
       hooks: Resend.GuardianHooks,
       ...
madshargreave commented 8 years ago

I totally missed that, my bad

doomspork commented 8 years ago

No worries @madshargreave! If you're on Slack, we have a Ueberauth/Guardian channel you can visit for support too: #ueberauth

Can we close this issue now?

madshargreave commented 8 years ago

Perfect, much appreciated

And yes, works now

doomspork commented 8 years ago

You're welcome @madshargreave, thank you 👍