You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add Assent to your list of dependencies in `mix.exs`:
47
-
48
-
```elixir
49
-
defpdepsdo
50
-
[
51
-
# ...
52
-
{:assent, "~> 0.3.0"}
53
-
]
54
-
end
55
-
```
42
+
## Usage
56
43
57
-
Run `mix deps.get` to install it.
58
-
59
-
#### HTTP client installation
44
+
A strategy consists of two phases; request and callback. In the request phase, the user will be redirected to the auth provider for authentication and then returned to initiate the callback phase.
60
45
61
-
By default, `Req` is used if you have it in your dependency list. If not, Erlang's `:httpc` will be used instead.
46
+
### Single provider
62
47
63
-
If you are using `:httpc` you should add the following dependencies to enable SSL validation:
48
+
This is an example using the `Assent.Strategy.Github` strategy with `Plug`:
64
49
65
50
```elixir
66
-
defpdepsdo
67
-
[
68
-
# ...
69
-
# Required for SSL validation when using the `:httpc` adapter
70
-
{:certifi, "~> 2.4"},
71
-
{:ssl_verify_fun, "~> 1.1"}
72
-
]
73
-
end
74
-
```
75
-
76
-
You must also add `:inets` to `:extra_applications` in `mix.exs`:
77
-
78
-
```elixir
79
-
defapplicationdo
80
-
[
81
-
# ...
82
-
extra_applications: [
83
-
# ...
84
-
:inets
85
-
]
86
-
]
87
-
end
88
-
```
89
-
90
-
This is not necessary if you use another HTTP adapter like `Req` or `Finch`.
91
-
92
-
## Getting started
93
-
94
-
A strategy consists of two phases; request and callback. In the request phase, the user would normally be redirected to the provider for authentication and then returned to initiate the callback phase.
# Session params should be added to the config so the strategy can use them
144
101
|>Keyword.put(:session_params, session_params)
145
102
|>Github.callback(params)
146
103
|>casedo
147
104
{:ok, %{user: user, token: token}} ->
148
105
# Authorization succesful
106
+
conn
107
+
|>put_session(:user, user)
108
+
|>put_session(:token, token)
109
+
|>put_resp_header("location", "/")
110
+
|>send_resp(302, "")
149
111
150
-
{:error, error} ->
112
+
{:error, _error} ->
151
113
# Authorizaiton failed
114
+
send_resp(conn, 500, "Failed authorization")
152
115
end
153
116
end
154
117
end
155
118
```
156
119
157
-
### Multi-provider example
120
+
### Multi-provider
158
121
159
-
This is a generalized flow that's similar to what's used in [PowAssent](https://github.com/danschultzer/pow_assent).
122
+
All assent strategies work the same way, so if you have more than one strategy you may want to set up a single module to handle any of the auth strategies. This example is a generalized flow that's similar to what's used in `PowAssent`.
Assent supports [`Req`](https://github.com/wojtekmach/req), [`Finch`](https://github.com/sneako/finch), and [`:httpc`](https://www.erlang.org/doc/man/httpc.html) out of the box. The `Req` HTTP client adapter will be used by default if enabled, otherwise Erlang's `:httpc` adapter will be included.
235
+
Assent supports [`Req`](https://github.com/wojtekmach/req), [`Finch`](https://github.com/sneako/finch), and [`:httpc`](https://www.erlang.org/doc/man/httpc.html) out of the box. The `Req` HTTP client adapter will be used by default if enabled, otherwise Erlang's `:httpc` adapter will be used.
265
236
266
237
You can explicitly set the HTTP client adapter in the configuration:
Req doesn't require any additional configuration and will work out of the box:
255
+
`Req` doesn't require any additional configuration and will work out of the box:
285
256
286
257
```elixir
287
258
defpdepsdo
@@ -292,7 +263,7 @@ defp deps do
292
263
end
293
264
```
294
265
295
-
### `:httpc`
266
+
### :httpc
296
267
297
268
If `Req` is not available, Erlangs built-in `:httpc` is used for requests. SSL verification is automatically enabled when `:certifi` and `:ssl_verify_fun` packages are available. `:httpc` only supports HTTP/1.1.
298
269
@@ -311,7 +282,7 @@ You must include `:inets` to `:extra_applications` to include `:httpc` in your r
311
282
312
283
### Finch
313
284
314
-
Finch will require a supervisor in your application.
285
+
`Finch` will require a supervisor in your application.
0 commit comments