From 7cc29b4c3b10f63ee62620c199c264e8159c1104 Mon Sep 17 00:00:00 2001 From: Dan Schultzer Date: Fri, 29 Nov 2019 08:17:52 -0800 Subject: [PATCH] Permit link opts --- CHANGELOG.md | 3 ++- lib/pow_assent/phoenix/views/view_helpers.ex | 22 ++++++++++--------- .../phoenix/views/view_helpers_test.exs | 6 +++++ 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f3f1560..7540254 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,8 @@ ## v0.4.5 (TBA) * [`PowAssent.Phoenix.AuthorizationController`] Now supports `:request_path` param so the user will be redirected back to `:request_path` after successful authorization -* [`PowAssent.Phoenix.ViewHelpers`] `PowAssent.Phoenix.ViewHelpers.authorization_link/2` now adds `:request_path` to the query param if assigned to the conn +* [`PowAssent.Phoenix.ViewHelpers`] `PowAssent.Phoenix.ViewHelpers.authorization_link/3` now adds `:request_path` to the query param if assigned to the conn +* [`PowAssent.Phoenix.ViewHelpers`] `PowAssent.Phoenix.ViewHelpers.authorization_link/3`, `PowAssent.Phoenix.ViewHelpers.deauthorization_link/3`, and `PowAssent.Phoenix.ViewHelpers.provider_links/2` now accepts keyword list with options to be passed on to the link generation ## v0.4.4 (2019-11-22) diff --git a/lib/pow_assent/phoenix/views/view_helpers.ex b/lib/pow_assent/phoenix/views/view_helpers.ex index f8f6ecc..bd8d2e3 100644 --- a/lib/pow_assent/phoenix/views/view_helpers.ex +++ b/lib/pow_assent/phoenix/views/view_helpers.ex @@ -17,16 +17,16 @@ defmodule PowAssent.Phoenix.ViewHelpers do be looked up with `PowAssent.Plug.providers_for_current_user/1`. `deauthorization_link/2` will be used for any already authorized providers. """ - @spec provider_links(Conn.t()) :: [HTML.safe()] - def provider_links(conn) do + @spec provider_links(Conn.t(), keyword()) :: [HTML.safe()] + def provider_links(conn, link_opts \\ []) do available_providers = Plug.available_providers(conn) providers_for_user = Plug.providers_for_current_user(conn) available_providers |> Enum.map(&{&1, &1 in providers_for_user}) |> Enum.map(fn - {provider, true} -> deauthorization_link(conn, provider) - {provider, false} -> authorization_link(conn, provider) + {provider, true} -> deauthorization_link(conn, provider, link_opts) + {provider, false} -> authorization_link(conn, provider, link_opts) end) end @@ -37,14 +37,15 @@ defmodule PowAssent.Phoenix.ViewHelpers do `:invited_user` is assigned to the conn, the invitation token will be passed on through the URL query params. """ - @spec authorization_link(Conn.t(), atom()) :: HTML.safe() - def authorization_link(conn, provider) do + @spec authorization_link(Conn.t(), atom(), keyword()) :: HTML.safe() + def authorization_link(conn, provider, opts \\ []) do query_params = invitation_token_query_params(conn) ++ request_path_query_params(conn) msg = AuthorizationController.extension_messages(conn).login_with_provider(%{conn | params: %{"provider" => provider}}) path = AuthorizationController.routes(conn).path_for(conn, AuthorizationController, :new, [provider], query_params) + opts = Keyword.merge(opts, to: path) - Link.link(msg, to: path) + Link.link(msg, opts) end defp invitation_token_query_params(%{assigns: %{invited_user: %{invitation_token: token}}}), do: [invitation_token: token] @@ -58,11 +59,12 @@ defmodule PowAssent.Phoenix.ViewHelpers do The link is used to remove authorization with the provider. """ - @spec deauthorization_link(Conn.t(), atom()) :: HTML.safe() - def deauthorization_link(conn, provider) do + @spec deauthorization_link(Conn.t(), atom(), keyword()) :: HTML.safe() + def deauthorization_link(conn, provider, opts \\ []) do msg = AuthorizationController.extension_messages(conn).remove_provider_authentication(%{conn | params: %{"provider" => provider}}) path = AuthorizationController.routes(conn).path_for(conn, AuthorizationController, :delete, [provider]) + opts = Keyword.merge(opts, to: path, method: :delete) - Link.link(msg, to: path, method: :delete) + Link.link(msg, opts) end end diff --git a/test/pow_assent/phoenix/views/view_helpers_test.exs b/test/pow_assent/phoenix/views/view_helpers_test.exs index 5751399..ffe7719 100644 --- a/test/pow_assent/phoenix/views/view_helpers_test.exs +++ b/test/pow_assent/phoenix/views/view_helpers_test.exs @@ -36,6 +36,12 @@ defmodule PowAssent.ViewHelpersTest do assert {:safe, iodata} == Link.link("Remove Test provider authentication", to: "/auth/test_provider", method: "delete") end + test "provider_links/1 with link opts", %{conn: conn} do + [safe: iodata] = ViewHelpers.provider_links(conn, class: "example") + + assert {:safe, iodata} == Link.link("Sign in with Test provider", to: "/auth/test_provider/new", class: "example") + end + test "provider_links/1 with request_path", %{conn: conn} do [safe: iodata] = conn