From 2e7bc087e4d8d14fe0adce747ef2f61c3fb7e749 Mon Sep 17 00:00:00 2001 From: Toby Archer Date: Fri, 25 Aug 2023 16:41:38 +0200 Subject: [PATCH] Implement verification flow --- .../controllers/account_controller.ex | 58 +++++++++++++++++++ .../account_html/settings.html.heex | 2 +- .../account_html/verification.html.heex | 1 - .../controllers/account_html/verify.html.heex | 5 ++ elixir-ory-network/lib/example_web/router.ex | 1 + 5 files changed, 65 insertions(+), 2 deletions(-) delete mode 100644 elixir-ory-network/lib/example_web/controllers/account_html/verification.html.heex create mode 100644 elixir-ory-network/lib/example_web/controllers/account_html/verify.html.heex diff --git a/elixir-ory-network/lib/example_web/controllers/account_controller.ex b/elixir-ory-network/lib/example_web/controllers/account_controller.ex index efab545..3391f30 100644 --- a/elixir-ory-network/lib/example_web/controllers/account_controller.ex +++ b/elixir-ory-network/lib/example_web/controllers/account_controller.ex @@ -5,6 +5,64 @@ defmodule ExampleWeb.AccountController do alias Ory.Api.Frontend + @doc """ + Verify page from flow parameters with optional code pre-filling + """ + + def verify(conn, %{"flow" => flow, "code" => code}) do + case Frontend.get_verification_flow(Ory.Connection.new(), flow, cookie: cookies(conn)) do + {:ok, %Ory.Model.VerificationFlow{} = flow} -> + render(conn, :verify, flow: flow) + + {:ok, %Ory.Model.ErrorGeneric{} = error} -> + IO.inspect(error) + render(conn, :verify, flow: nil) + + _ -> + IO.puts("oops") + end + end + + def verify(conn, %{"flow" => id}) do + case Frontend.get_verification_flow(Ory.Connection.new(), id, cookie: cookies(conn)) do + {:ok, %Ory.Model.VerificationFlow{} = flow} -> + render(conn, :verify, flow: flow) + + {:ok, %Ory.Model.ErrorGeneric{}} -> + render(conn, :verify, flow: nil) + + _ -> + IO.puts("oops") + end + end + + def verify(%{assigns: %{session: session}} = conn, %{return_to: return_to}) do + IO.inspect(session) + + {:ok, %{url: url}} = + Frontend.create_browser_verification_flow(Ory.Connection.new(), return_to: return_to) + + redirect(conn, external: url) + end + + def verify(%{assigns: %{session: session}} = conn, _params) do + IO.inspect(session) + + # TODO(@tobbbles): Check for verified session(s) + + {:ok, %{url: url}} = Frontend.create_browser_verification_flow(Ory.Connection.new()) + redirect(conn, external: url) + end + + # Redirect on no-session + def verify(conn, _params) do + redirect(conn, to: ~p"/account/settings") + end + + @doc """ + Settings controller + """ + def settings(conn, %{"flow" => id}) do {:ok, %Ory.Model.LogoutFlow{logout_url: logout_url}} = Frontend.create_browser_logout_flow(Ory.Connection.new(), cookie: cookies(conn)) diff --git a/elixir-ory-network/lib/example_web/controllers/account_html/settings.html.heex b/elixir-ory-network/lib/example_web/controllers/account_html/settings.html.heex index 72353e7..cc4aa58 100644 --- a/elixir-ory-network/lib/example_web/controllers/account_html/settings.html.heex +++ b/elixir-ory-network/lib/example_web/controllers/account_html/settings.html.heex @@ -1,4 +1,4 @@ -

+

<%= gettext("Settings") %>

diff --git a/elixir-ory-network/lib/example_web/controllers/account_html/verification.html.heex b/elixir-ory-network/lib/example_web/controllers/account_html/verification.html.heex deleted file mode 100644 index 8b13789..0000000 --- a/elixir-ory-network/lib/example_web/controllers/account_html/verification.html.heex +++ /dev/null @@ -1 +0,0 @@ - diff --git a/elixir-ory-network/lib/example_web/controllers/account_html/verify.html.heex b/elixir-ory-network/lib/example_web/controllers/account_html/verify.html.heex new file mode 100644 index 0000000..7eef2c7 --- /dev/null +++ b/elixir-ory-network/lib/example_web/controllers/account_html/verify.html.heex @@ -0,0 +1,5 @@ +

+ <%= gettext("Verify") %> +

+ + diff --git a/elixir-ory-network/lib/example_web/router.ex b/elixir-ory-network/lib/example_web/router.ex index e801695..9b73f76 100644 --- a/elixir-ory-network/lib/example_web/router.ex +++ b/elixir-ory-network/lib/example_web/router.ex @@ -33,6 +33,7 @@ defmodule ExampleWeb.Router do pipe_through [:browser] get "/settings", AccountController, :settings + get "/verify", AccountController, :verify end # Other scopes may use custom stacks.