Skip to content

Commit

Permalink
Merge pull request #112 from pow-auth/permit-link-opts
Browse files Browse the repository at this point in the history
Permit link opts
  • Loading branch information
danschultzer committed Nov 29, 2019
2 parents f172aae + 7cc29b4 commit 2f1071f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
22 changes: 12 additions & 10 deletions lib/pow_assent/phoenix/views/view_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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]
Expand All @@ -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
6 changes: 6 additions & 0 deletions test/pow_assent/phoenix/views/view_helpers_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2f1071f

Please sign in to comment.