Packages
ash_authentication_phoenix
2.5.3
3.0.0-rc.9
3.0.0-rc.8
3.0.0-rc.7
3.0.0-rc.6
3.0.0-rc.4
3.0.0-rc.3
3.0.0-rc.2
3.0.0-rc.1
3.0.0-rc.0
2.17.2
2.17.1
2.17.0
2.16.0
2.15.0
2.14.1
2.14.0
2.13.1
2.13.0
2.12.2
2.12.1
2.12.0
2.11.0
2.10.5
2.10.4
2.10.3
2.10.2
2.10.1
2.10.0
2.9.0
2.8.0
2.7.0
2.6.3
2.6.2
2.6.1
2.6.0
2.5.4
2.5.3
2.5.2
2.5.1
2.5.0
2.4.8
2.4.7
2.4.6
2.4.5
2.4.4
2.4.3
2.4.2
2.4.1
2.4.0
2.3.0
2.2.1
2.2.0
2.1.11
2.1.10
2.1.9
2.1.8
2.1.7
2.1.6
2.1.5
2.1.4
2.1.3
2.1.2
2.1.1
2.1.0
2.0.2
2.0.1
2.0.0
2.0.0-rc.2
2.0.0-rc.1
2.0.0-rc.0
1.9.4
1.9.3
1.9.2
1.9.1
1.9.0
1.8.7
1.8.6
1.8.5
1.8.4
1.8.3
1.8.2
1.8.1
1.8.0
1.7.3
1.7.2
1.7.1
1.7.0
1.6.6
1.6.5
1.6.4
1.6.3
1.6.2
1.6.1
1.6.0
1.5.1
1.5.0
1.4.8
1.4.7
1.4.6
1.4.5
1.4.4
1.4.3
1.4.2
1.4.1
1.4.0
1.3.1
1.3.0
1.2.0
1.1.0
1.0.1
1.0.0
Phoenix integration for Ash Authentication
Security advisory:
This version has known vulnerabilities.
View advisories
Current section
Files
Jump to
Current section
Files
lib/ash_authentication_phoenix_web.ex
defmodule AshAuthentication.Phoenix.Web do
@moduledoc false
alias AshAuthentication.Phoenix.{LayoutView, Utils.Flash, Web}
@doc false
def view do
quote do
use Phoenix.View,
root: "lib/ash_authentication_phoenix/templates",
namespace: Web
# Import convenience functions from controllers
import Phoenix.Controller,
only: [view_module: 1, view_template: 1]
use Phoenix.Component
end
end
@doc false
def live_view do
quote do
use Phoenix.LiveView, layout: {LayoutView, :live}
on_mount Flash
end
end
@doc false
def live_component do
quote do
use Phoenix.LiveComponent
import Flash, only: [put_flash!: 3]
end
end
@doc false
def component do
quote do
use Phoenix.Component
import Flash, only: [put_flash!: 3]
end
end
@doc """
Provide a `_gettext` macro for views to wrap around text. Output is a function call to `gettext_switch/3`.
"""
def maybe_translate do
quote do
@spec _gettext(String.t() | nil, Keyword.t()) :: any | no_return
defmacro _gettext(msgid, bindings \\ [])
defmacro _gettext(nil, bindings), do: ""
defmacro _gettext(msgid, bindings) do
gettext_fn =
cond do
Macro.Env.has_var?(__CALLER__, {:assigns, nil}) ->
quote do: var!(assigns)[:gettext_fn]
Macro.Env.has_var?(__CALLER__, {:socket, nil}) ->
quote do: var!(socket).assigns[:gettext_fn]
true ->
raise ~S{_gettext requires variable "socket" or "assigns" to exist and be set to a map}
end
quote generated: true do
Web.gettext_switch(unquote(gettext_fn), unquote(msgid), unquote(bindings))
end
end
defmacro _transform_errors do
quote do
alias AshPhoenix.FormData.Error
fn _source, error ->
if Error.impl_for(error) do
Error.to_form_error(error)
|> List.wrap()
# credo:disable-for-next-line Credo.Check.Refactor.Nesting
|> Enum.map(fn {field, message, vars} ->
{field, _gettext(message, vars), vars}
end)
else
error
end
end
end
end
end
end
@spec gettext_switch(
gettext_fn :: {module, atom} | nil,
msgid :: String.t(),
bindings :: keyword
) :: String.t()
@doc """
If a translation function is provided, we call that, otherwise return the input untranslated.
"""
def gettext_switch(_gettext_fn, nil, _bindings), do: ""
def gettext_switch({module, function}, msgid, bindings)
when is_atom(module) and is_atom(function) do
apply(module, function, [msgid, bindings])
end
def gettext_switch(nil, msgid, bindings) do
for {key, value} <- bindings, reduce: msgid do
acc ->
value =
if is_list(value) do
Enum.map_join(value, ", ", &to_string/1)
else
value
end
String.replace(acc, "%{#{key}}", to_string(value))
end
end
def gettext_switch(invalid, _msgid, _bindings) do
raise ArgumentError,
"gettext_fn: #{inspect(invalid)} is invalid - specify `{module, function}` " <>
"for a function with a `gettext/2` like signature"
end
@doc """
When used, dispatch to the appropriate controller/view/etc. and inject gettext helper.
"""
defmacro __using__(which) when is_atom(which) do
quote do
unquote(apply(__MODULE__, which, []))
unquote(maybe_translate())
end
end
end