Skip to content

Commit f7d4f5d

Browse files
authored
use single source of truth for socket token authn docs (phoenixframework#6242)
* use single source of truth for socket token authn docs * test generated socket.js file uses new token authn method * redact remaing traces of old token authn in docs * enable auth_token option in channels guide
1 parent a4ecd8e commit f7d4f5d

File tree

4 files changed

+15
-52
lines changed

4 files changed

+15
-52
lines changed

guides/real_time/channels.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ In your Phoenix app's `Endpoint` module, a `socket` declaration specifies which
107107
```elixir
108108
socket "/socket", HelloWeb.UserSocket,
109109
websocket: true,
110-
longpoll: false
110+
longpoll: false,
111+
auth_token: true
111112
```
112113

113114
Phoenix comes with two default transports: websocket and longpoll. You can configure them directly via the `socket` declaration.
@@ -117,7 +118,7 @@ Phoenix comes with two default transports: websocket and longpoll. You can confi
117118
On the client side, you will establish a socket connection to the route above:
118119

119120
```javascript
120-
let socket = new Socket("/socket", {params: {token: window.userToken}})
121+
let socket = new Socket("/socket", {authToken: window.userToken})
121122
```
122123

123124
On the server, Phoenix will invoke `HelloWeb.UserSocket.connect/2`, passing your parameters and the initial socket state. Within the socket, you can authenticate and identify a socket connection and set default socket assigns. The socket is also where you define your channel routes.

guides/real_time/presence.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ We can see presence working by adding the following to `assets/js/app.js`:
9393
```javascript
9494
import {Socket, Presence} from "phoenix"
9595

96-
let socket = new Socket("/socket", {params: {token: window.userToken}})
96+
let socket = new Socket("/socket", {authToken: window.userToken})
9797
let channel = socket.channel("room:lobby", {name: window.location.search.split("=")[1]})
9898
let presence = new Presence(channel)
9999

priv/templates/phx.gen.socket/socket.js

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,11 @@
55
import {Socket} from "phoenix"
66

77
// And connect to the path in "<%= web_prefix %>/endpoint.ex". We pass the
8-
// token for authentication. Read below how it should be used.
9-
let socket = new Socket("/socket", {params: {token: window.userToken}})
10-
11-
// When you connect, you'll often need to authenticate the client.
12-
// For example, imagine you have an authentication plug, `MyAuth`,
13-
// which authenticates the session and assigns a `:current_user`.
14-
// If the current user exists you can assign the user's token in
15-
// the connection for use in the layout.
16-
//
17-
// In your "<%= web_prefix %>/router.ex":
18-
//
19-
// pipeline :browser do
20-
// ...
21-
// plug MyAuth
22-
// plug :put_user_token
23-
// end
24-
//
25-
// defp put_user_token(conn, _) do
26-
// if current_user = conn.assigns[:current_user] do
27-
// token = Phoenix.Token.sign(conn, "user socket", current_user.id)
28-
// assign(conn, :user_token, token)
29-
// else
30-
// conn
31-
// end
32-
// end
33-
//
34-
// Now you need to pass this token to JavaScript. You can do so
35-
// inside a script tag in "<%= web_prefix %>/components/layouts/root.html.heex":
36-
//
37-
// <script>window.userToken = "<%%= assigns[:user_token] %>";</script>
38-
//
39-
// You will need to verify the user token in the "connect/3" function
40-
// in "<%= web_prefix %>/channels/user_socket.ex":
41-
//
42-
// def connect(%{"token" => token}, socket, _connect_info) do
43-
// # max_age: 1209600 is equivalent to two weeks in seconds
44-
// case Phoenix.Token.verify(socket, "user socket", token, max_age: 1_209_600) do
45-
// {:ok, user_id} ->
46-
// {:ok, assign(socket, :user, user_id)}
47-
//
48-
// {:error, reason} ->
49-
// :error
50-
// end
51-
// end
8+
// token for authentication.
529
//
53-
// Finally, connect to the socket:
10+
// Read the [`Using Token Authentication`](https://hexdocs.pm/phoenix/channels.html#using-token-authentication)
11+
// section to see how the token should be used.
12+
let socket = new Socket("/socket", {authToken: window.userToken})
5413
socket.connect()
5514

5615
// Now that you are connected, you can join channels with a topic.

test/mix/tasks/phx.gen.socket_test.exs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ defmodule Mix.Tasks.Phx.Gen.SocketTest do
3333
assert file =~ ~S|// you uncomment its entry in "assets/js/app.js".|
3434

3535
assert file =~ ~S|// And connect to the path in "lib/phoenix_web/endpoint.ex".|
36-
assert file =~ ~S|let socket = new Socket("/socket", {params: {token: window.userToken}})|
36+
assert file =~ ~S|let socket = new Socket("/socket", {authToken: window.userToken})|
3737

3838
assert file =~ ~S|let channel = socket.channel("room:42", {})|
3939
assert file =~ ~S|channel.join()|
@@ -74,7 +74,9 @@ defmodule Mix.Tasks.Phx.Gen.SocketTest do
7474
assert file =~ ~S|import {Socket} from "phoenix"|
7575

7676
assert file =~ ~S|// And connect to the path in "lib/phoenix/endpoint.ex".|
77-
assert file =~ ~S|In your "lib/phoenix/router.ex":|
77+
78+
assert file =~
79+
~S|Read the [`Using Token Authentication`](https://hexdocs.pm/phoenix/channels.html#using-token-authentication)|
7880

7981
assert file =~ ~S|let channel = socket.channel("room:42", {})|
8082
assert file =~ ~S|channel.join()|
@@ -101,9 +103,10 @@ defmodule Mix.Tasks.Phx.Gen.SocketTest do
101103
assert file =~ ~S|import {Socket} from "phoenix"|
102104

103105
assert file =~ ~S|// And connect to the path in "lib/phoenix_web/endpoint.ex".|
104-
assert file =~ ~S|let socket = new Socket("/socket", {params: {token: window.userToken}})|
106+
assert file =~ ~S|let socket = new Socket("/socket", {authToken: window.userToken})|
105107

106-
assert file =~ ~S|In your "lib/phoenix_web/router.ex":|
108+
assert file =~
109+
~S|Read the [`Using Token Authentication`](https://hexdocs.pm/phoenix/channels.html#using-token-authentication)|
107110

108111
assert file =~ ~S|let channel = socket.channel("room:42", {})|
109112
assert file =~ ~S|channel.join()|

0 commit comments

Comments
 (0)