-
-
Notifications
You must be signed in to change notification settings - Fork 152
/
base.ex
124 lines (98 loc) · 3.48 KB
/
base.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
defmodule Pow.Plug.Base do
@moduledoc """
This plug macro will set `:pow_config` as private, and attempt to fetch and
assign a user in the connection if it has not already been assigned. The user
will be assigned automatically in any of the operations.
## Example
defmodule MyAppWeb.Pow.CustomPlug do
use Pow.Plug.Base
@impl true
def fetch(conn, _config) do
user = fetch_user_from_cookie(conn)
{conn, user}
end
@impl true
def create(conn, user, _config) do
conn = update_cookie(conn, user)
{conn, user}
end
@impl true
def delete(conn, _config) do
delete_cookie(conn)
end
end
"""
alias Plug.Conn
alias Pow.{Config, Plug}
@callback init(Config.t()) :: Config.t()
@callback call(Conn.t(), Config.t()) :: Conn.t()
@callback fetch(Conn.t(), Config.t()) :: {Conn.t(), map() | nil}
@callback create(Conn.t(), map(), Config.t()) :: {Conn.t(), map()}
@callback delete(Conn.t(), Config.t()) :: Conn.t()
@doc false
defmacro __using__(_opts) do
quote do
alias Pow.Plug.Base
@behaviour Base
@doc false
def init(config), do: config
@doc """
Configures the connection for Pow, and fetches user.
`:plug` is appended to the passed configuration, so the current plug will
be used in any subsequent calls to create, update and delete user
credentials from the connection. The configuration is then set for the
conn with `Pow.Plug.put_config/2`.
If a user can't be fetched with `Pow.Plug.current_user/2`, `do_fetch/2`
will be called.
"""
def call(conn, config) do
config = put_plug(config)
conn = Plug.put_config(conn, config)
conn
|> Plug.current_user(config)
|> maybe_fetch_user(conn, config)
end
defp put_plug(config) do
config
|> Config.put(:plug, __MODULE__)
|> Config.put(:mod, __MODULE__) # TODO: Remove by 1.1.0, this is only for backwards compability
end
@doc """
Calls `fetch/2` and assigns the current user to the conn.
The user is assigned to the conn with `Pow.Plug.assign_current_user/3`.
"""
@spec do_fetch(Conn.t(), Config.t()) :: Conn.t()
def do_fetch(conn, config) do
conn
|> fetch(config)
|> assign_current_user(config)
end
@doc """
Calls `create/3` and assigns the current user.
The user is assigned to the conn with `Pow.Plug.assign_current_user/3`.
"""
@spec do_create(Conn.t(), map(), Config.t()) :: Conn.t()
def do_create(conn, user, config) do
conn
|> create(user, config)
|> assign_current_user(config)
end
@doc """
Calls `delete/2` and removes the current user assigned to the conn.
The user assigned is removed from the conn with
`Pow.Plug.assign_current_user/3`.
"""
@spec do_delete(Conn.t(), Config.t()) :: Conn.t()
def do_delete(conn, config) do
conn
|> delete(config)
|> remove_current_user(config)
end
defp maybe_fetch_user(nil, conn, config), do: do_fetch(conn, config)
defp maybe_fetch_user(_user, conn, _config), do: conn
defp assign_current_user({conn, user}, config), do: Plug.assign_current_user(conn, user, config)
defp remove_current_user(conn, config), do: Plug.assign_current_user(conn, nil, config)
defoverridable Base
end
end
end