Packages
auth_plug
1.2.3
1.5.2
1.5.1
1.5.0
1.4.21
1.4.20
1.4.19
1.4.18
1.4.17
1.4.16
1.4.15
1.4.14
1.4.13
1.4.12
1.4.11
1.4.10
1.4.9
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.7
1.3.6
1.3.5
1.3.4
1.3.3
1.3.2
1.3.1
1.3.0
1.2.3
1.2.2
1.2.1
1.2.0
1.1.1
1.1.0
1.0.0
0.16.0
0.15.0
0.14.0
0.13.0
0.12.0
0.11.0
0.10.0
0.9.0
0.8.0
0.7.0
0.5.0
0.4.0
0.3.1
0.3.0
0.2.1
0.2.0
0.1.4
0.1.3
0.1.2
0.1.1
0.1.0
Turnkey Auth Plug lets you protect any route in an Elixir/Phoenix App.
Current section
Files
Jump to
Current section
Files
lib/helpers.ex
defmodule AuthPlug.Helpers do
@doc """
`get_baseurl_from_conn/1` derives the base URL from the conn struct
e.g: http://localhost:4000 or https://app.dwyl.com
"""
@spec get_baseurl_from_conn(Map) :: String.t()
def get_baseurl_from_conn(%{host: h, port: p}) when h == "localhost" do
"http://#{h}:#{p}"
end
def get_baseurl_from_conn(%{host: h}) do
"https://#{h}"
end
@doc """
`strip_struct_metadata/1` removes the Ecto Struct metadata from a struct.
This is essential before attempting to create a JWT as `Jason.encode/2`
chokes on any invalid data. See: github.com/dwyl/auth_plug/issues/16
"""
def strip_struct_metadata(struct) do
struct
|> Map.delete(:__meta__)
|> Map.delete(:__struct__)
# association
|> Map.delete(:statuses)
# association
|> Map.delete(:login_logs)
# binary
|> Map.delete(:email_hash)
end
@doc """
`check_environment_vars/0` displays a friendly error message
if the AUTH_API_KEY environment variable is not defined.
"""
def check_environment_vars do
key = AuthPlug.Token.api_key()
if is_nil(key) do
raise "No AUTH_API_KEY set, find out how at: https://git.io/JJ6sS"
else
key
end
end
end