How do you make a seperate signup callback for ueberauth identity

322 Views Asked by At

ueberauth_identity has an example of a single callback for both login and signup but this leads to bad error handling. Is there a way to make a separate signup callback?

1

There are 1 best solutions below

0
On

I would say it's just a matter of sending a particular parameter and propagating it through all the relevant calls. I'm not sure "how much cleaner" it'll end up being though.

Say for example that you have it set up exactly like in the docs:

scope "/auth", MyApp do
  pipe_through :browser

  get "/:provider", AuthController, :request
  get "/:provider/callback", AuthController, :callback
  post "/identity/callback", AuthController, :identity_callback
end

So that when you want to force a user to log in, you redirect him to the following route:

Routes.auth_path(@conn, :request, "identity")

Which is implemented somewhat like this:

defmodule UeberauthExampleWeb.AuthController do
  use UeberauthExampleWeb, :controller

  plug Ueberauth

  alias Ueberauth.Strategy.Helpers

  def request(conn, _params) do
    render(conn, "request.html", callback_url: Helpers.callback_url(conn))
  end

  # ...
end

Now, nothing really stops you from adding some query parameters so that it looks like this:

Routes.auth_path(@conn, :request, "identity", login: true)

Which you can propagate to the callback URL:

def request(conn, %{"login" => _}) do
  callback_url = Helpers.callback_url(conn) <> "?login=true"
  render(conn, "request.html", callback_url: callback_url)
end

So that you can finally pattern match in your callback:

def identity_callback(%{assigns: %{ueberauth_auth: auth}} = conn, %{"login" => _}) do
  # ...
end