Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
danschultzer committed Aug 5, 2018
1 parent 112242f commit 125ba74
Show file tree
Hide file tree
Showing 15 changed files with 106 additions and 19 deletions.
3 changes: 2 additions & 1 deletion lib/extensions/persistent_session/plug/base.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule PowPersistentSession.Plug.Base do
@moduledoc """
Base module for setting up persistent session plugs.
See PowPersistentSession.Plug.Cookie for an implementation example.
See `PowPersistentSession.Plug.Cookie` for an implementation example.
## Configuration options
Expand All @@ -21,6 +21,7 @@ defmodule PowPersistentSession.Plug.Base do
@callback authenticate(Conn.t(), Config.t()) :: Conn.t()
@callback create(Conn.t(), map(), Config.t()) :: Conn.t()

@doc false
defmacro __using__(_opts) do
quote do
@behaviour unquote(__MODULE__)
Expand Down
5 changes: 0 additions & 5 deletions lib/pow/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ defmodule Pow.Config do
defexception [:message]
end

@spec current_user_assigns_key(t()) :: atom()
def current_user_assigns_key(config) do
get(config, :current_user_assigns_key, :current_user)
end

@spec user_module(t()) :: atom()
def user_module(config) do
get(config, :user, nil) || raise_no_user_error()
Expand Down
33 changes: 31 additions & 2 deletions lib/pow/ecto/context.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ defmodule Pow.Ecto.Context do
@callback delete(user()) :: {:ok, user()} | {:error, Changeset.t()}
@callback get_by(Keyword.t() | map()) :: user() | nil

@doc false
defmacro __using__(config) do
quote do
@behaviour unquote(__MODULE__)
Expand Down Expand Up @@ -82,6 +83,11 @@ defmodule Pow.Ecto.Context do
end
end

@doc """
Finds a user based on the user id, and verifies the password on the user.
User schema module and repo module will be fetched from the config. The
user id field is fetched from the user schema module.
"""
@spec authenticate(Config.t(), map()) :: user() | nil
def authenticate(config, params) do
user_mod = user_schema_mod(config)
Expand All @@ -104,7 +110,8 @@ defmodule Pow.Ecto.Context do
end

@doc """
Creates a new user. User struct will be fetched from repo.
Creates a new user. User schema module and repo module will be fetched from
config.
"""
@spec create(Config.t(), map()) :: {:ok, user()} | {:error, Changeset.t()}
def create(config, params) do
Expand All @@ -116,7 +123,8 @@ defmodule Pow.Ecto.Context do
end

@doc """
updates a new user. User struct will be fetched from repo.
Updates the user. User schema module will be fetched from provided user and
repo will be fetched from the config.
"""
@spec update(Config.t(), user(), map()) :: {:ok, user()} | {:error, Changeset.t()}
def update(config, user, params) do
Expand All @@ -125,11 +133,18 @@ defmodule Pow.Ecto.Context do
|> do_update(config)
end

@doc """
Deletes the user. Repo module will be fetched from the config.
"""
@spec delete(Config.t(), user()) :: {:ok, user()} | {:error, Changeset.t()}
def delete(config, user) do
repo(config).delete(user)
end

@doc """
Retrieves an user by the provided clauses. User schema module and repo module
will be fetched from the config.
"""
@spec get_by(Config.t(), Keyword.t() | map()) :: user() | nil
def get_by(config, clauses) do
user_mod = user_schema_mod(config)
Expand All @@ -147,13 +162,21 @@ defmodule Pow.Ecto.Context do
end
end

@doc """
Inserts a changeset to the database. If succesful, the returned row will be
reloaded from the database.
"""
@spec do_insert(Changeset.t(), Config.t()) :: {:ok, user()} | {:error, Changeset.t()}
def do_insert(changeset, config) do
changeset
|> repo(config).insert()
|> reload_after_write(config)
end

@doc """
Updates a changeset in the database. If succesful, the returned row will be
reloaded from the database.
"""
@spec do_update(Changeset.t(), Config.t()) :: {:ok, user()} | {:error, Changeset.t()}
def do_update(changeset, config) do
changeset
Expand All @@ -170,11 +193,17 @@ defmodule Pow.Ecto.Context do
{:ok, user}
end

@doc """
Retrieves the repo module from the config, or raises an exception.
"""
@spec repo(Config.t()) :: atom() | no_return
def repo(config) do
Config.get(config, :repo, nil) || raise_no_repo_error()
end

@doc """
Retrieves the user schema module from the config, or raises an exception.
"""
@spec user_schema_mod(Config.t()) :: atom() | no_return
def user_schema_mod(config) do
Config.get(config, :user, nil) || raise_no_user_error()
Expand Down
9 changes: 5 additions & 4 deletions lib/pow/ecto/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ defmodule Pow.Ecto.Schema do
@callback changeset(Ecto.Schema.t() | Changeset.t(), map()) :: Changeset.t()
@callback verify_password(Ecto.Schema.t(), binary()) :: boolean()

@doc false
defmacro __using__(config) do
quote do
@behaviour unquote(__MODULE__)
Expand All @@ -62,7 +63,6 @@ defmodule Pow.Ecto.Schema do

@changeset_methods [:user_id_field_changeset, :password_changeset, :current_password_changeset]

@spec __pow_methods__() :: Macro.t()
defmacro __pow_methods__ do
quoted_changeset_methods = for method <- @changeset_methods do
pow_method_name = String.to_atom("pow_#{method}")
Expand Down Expand Up @@ -95,7 +95,10 @@ defmodule Pow.Ecto.Schema do
end
end

@spec pow_user_fields :: Macro.t()
@doc """
A macro to add fields from the `@pow_fields` module attribute generated in
`__using__/1`.
"""
defmacro pow_user_fields do
quote do
Enum.each(@pow_fields, fn
Expand All @@ -108,7 +111,6 @@ defmodule Pow.Ecto.Schema do
end
end

@spec __register_fields__() :: Macro.t()
defmacro __register_fields__ do
quote do
Module.register_attribute(__MODULE__, :pow_fields, accumulate: true)
Expand All @@ -118,7 +120,6 @@ defmodule Pow.Ecto.Schema do
end
end

@spec __register_user_id_field__() :: Macro.t()
defmacro __register_user_id_field__ do
quote do
@user_id_field unquote(__MODULE__).user_id_field(@pow_config)
Expand Down
1 change: 1 addition & 0 deletions lib/pow/extension/ecto/context/base.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ defmodule Pow.Extension.Ecto.Context.Base do
"""
alias Pow.Ecto.Context

@doc false
defmacro __using__(_opts) do
quote do
def user_schema_mod(config), do: Context.user_schema_mod(config)
Expand Down
4 changes: 1 addition & 3 deletions lib/pow/extension/ecto/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ defmodule Pow.Extension.Ecto.Schema do
defexception [:message]
end

@doc false
defmacro __using__(config) do
quote do
@pow_extension_config Config.merge(@pow_config, unquote(config))
Expand All @@ -42,7 +43,6 @@ defmodule Pow.Extension.Ecto.Schema do
end
end

@spec __register_extension_fields__() :: Macro.t()
defmacro __register_extension_fields__ do
quote do
config = Module.get_attribute(__MODULE__, :pow_extension_config)
Expand All @@ -54,7 +54,6 @@ defmodule Pow.Extension.Ecto.Schema do
end
end

@spec __pow_extension_methods__() :: Macro.t()
defmacro __pow_extension_methods__ do
quote do
@spec pow_extension_changeset(Changeset.t(), map()) :: Changeset.t()
Expand All @@ -64,7 +63,6 @@ defmodule Pow.Extension.Ecto.Schema do
end
end

@spec __register_after_compile_validation__() :: Macro.t()
defmacro __register_after_compile_validation__ do
quote do
def validate_after_compilation!(env, _bytecode) do
Expand Down
3 changes: 3 additions & 0 deletions lib/pow/extension/phoenix/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ defmodule Pow.Extension.Phoenix.Router do
end
end

@doc """
A macro that will call the router method in all extension router modules.
"""
defmacro pow_extension_routes do
router_methods = Module.concat(__CALLER__.module, RouterMethods).methods()

Expand Down
2 changes: 1 addition & 1 deletion lib/pow/operations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule Pow.Operations do
|> Config.user_module()
|> struct()

changeset(config, user, params)
changeset(config, user, params)
end

@spec changeset(Config.t(), map(), map()) :: map()
Expand Down
5 changes: 5 additions & 0 deletions lib/pow/phoenix/mailer/template.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ defmodule Pow.Phoenix.Mailer.Template do
end
end

@doc """
A macro that will compile a mailer template from the provided binaries, and
add the compiled versions to `render/2` methods. The `text/1` and `html/1`
outputs the binaries.
"""
@spec template(atom(), binary(), binary(), binary()) :: Macro.t()
defmacro template(action, subject, text, html) do
quoted_text = EEx.compile_string(text)
Expand Down
5 changes: 5 additions & 0 deletions lib/pow/phoenix/template.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ defmodule Pow.Phoenix.Template do
end
end

@doc """
A macro that will compile a phoenix view template from the provided binary,
and add the compiled version to a `render/2` method. The `html/1` method
outputs the binary.
"""
@spec template(atom(), atom(), binary() | {atom(), any()}) :: Macro.t()
defmacro template(action, :html, content) do
content = EEx.eval_string(content)
Expand Down
41 changes: 39 additions & 2 deletions lib/pow/plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,50 @@ defmodule Pow.Plug do
current_user(conn, fetch_config(conn))
end

@doc """
Get the current authenticated user.
"""
@spec current_user(Conn.t(), Config.t()) :: map() | nil
def current_user(%{assigns: assigns}, config) do
key = Config.current_user_assigns_key(config)
key = current_user_assigns_key(config)

Map.get(assigns, key, nil)
end

@doc """
Assign an authenticated user to the connection.
"""
@spec assign_current_user(Conn.t(), any(), Config.t()) :: Conn.t()
def assign_current_user(conn, user, config) do
key = Config.current_user_assigns_key(config)
key = current_user_assigns_key(config)

Conn.assign(conn, key, user)
end

defp current_user_assigns_key(config) do
Config.get(config, :current_user_assigns_key, :current_user)
end

@doc """
Put the provided config as a private key in the connection.
"""
@spec put_config(Conn.t(), Config.t()) :: Conn.t()
def put_config(conn, config) do
Conn.put_private(conn, @private_config_key, config)
end

@doc """
Fetch config from the connection. It'll raise an error if configuration
hasn't been set as a private key.
"""
@spec fetch_config(Conn.t()) :: Config.t() | no_return
def fetch_config(%{private: private}) do
private[@private_config_key] || no_config_error()
end

@doc """
Authenticates a user. If successful, a new session will be created.
"""
@spec authenticate_user(Conn.t(), map()) :: {:ok, map(), Conn.t()} | {:error, map(), Conn.t()} | no_return
def authenticate_user(conn, params) do
config = fetch_config(conn)
Expand All @@ -49,13 +69,19 @@ defmodule Pow.Plug do
|> maybe_create_auth(conn, config)
end

@doc """
Clears the user authentication from the session.
"""
@spec clear_authenticated_user(Conn.t()) :: {:ok, Conn.t()} | no_return
def clear_authenticated_user(conn) do
config = fetch_config(conn)

{:ok, get_mod(config).do_delete(conn)}
end

@doc """
Creates a changeset from the current authenticated user.
"""
@spec change_user(Conn.t(), map()) :: map()
def change_user(conn, params \\ %{}) do
config = fetch_config(conn)
Expand All @@ -66,6 +92,9 @@ defmodule Pow.Plug do
end
end

@doc """
Creates a new user. If successful, a new session will be created.
"""
@spec create_user(Conn.t(), map()) :: {:ok, map(), Conn.t()} | {:error, map(), Conn.t()} | no_return
def create_user(conn, params) do
config = fetch_config(conn)
Expand All @@ -75,6 +104,10 @@ defmodule Pow.Plug do
|> maybe_create_auth(conn, config)
end

@doc """
Updates the current authenticated user. If successful, a new session will be
created.
"""
@spec update_user(Conn.t(), map()) :: {:ok, map(), Conn.t()} | {:error, map(), Conn.t()} | no_return
def update_user(conn, params) do
config = fetch_config(conn)
Expand All @@ -85,6 +118,10 @@ defmodule Pow.Plug do
|> maybe_create_auth(conn, config)
end

@doc """
Deletes the current authenticated user. If successful, the user
authentication will be cleared from the session.
"""
@spec delete_user(Conn.t()) :: {:ok, map(), Conn.t()} | {:error, map(), Conn.t()} | no_return
def delete_user(conn) do
config = fetch_config(conn)
Expand Down
4 changes: 4 additions & 0 deletions lib/pow/uuid.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ defmodule Pow.UUID do
https://github.com/elixir-ecto/ecto/blob/v2.2.10/lib/ecto/uuid.ex#L1, and is
under Apache 2 license.
"""

@doc """
Generates a UUID binary.
"""
@spec generate :: binary()
def generate, do: encode(bingenerate())

Expand Down
4 changes: 4 additions & 0 deletions test/pow/operations_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defmodule Pow.OperationsTest do
use ExUnit.Case
doctest Pow.Operations
end
2 changes: 1 addition & 1 deletion test/pow/plug_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ defmodule Pow.PlugTest do
assert is_nil(Plug.current_user(conn, @admin_config))
end

test "assign_current_user/2" do
test "assign_current_user/3" do
user = %{id: 1}
conn = %Conn{assigns: %{}}
assert Plug.assign_current_user(conn, %{id: 1}, @default_config) == %Conn{assigns: %{current_user: user}}
Expand Down
4 changes: 4 additions & 0 deletions test/pow/uuid_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defmodule Pow.UUIDTest do
use ExUnit.Case
doctest Pow.UUID
end

0 comments on commit 125ba74

Please sign in to comment.