การยืนยันตัวตนและการอนุญาตสำหรับ Channel
รักษาความปลอดภัยให้ Phoenix Channels ด้วยการตรวจสอบโทเค็นผู้ใช้ระหว่างขั้นตอนการเข้าร่วม และอนุญาตการเข้าถึงหัวข้อที่กำหนด
การยืนยันตัวตนและการอนุญาตสำหรับ Channel เป็นบทเรียน Elixir & Phoenix: Scalable Backend Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Elixir & Phoenix: Scalable Backend Development และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Elixir & Phoenix: Scalable Backend Development มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why Channels Need Auth
Channels are long-lived WebSocket connections. Without verification, anyone could join a private topic and read or send messages.
Authentication answers who is connecting, while authorization answers what topics they may join.
The Socket Connect Callback
Authentication starts in connect/3 of your UserSocket. It runs once when the WebSocket opens, before any channel is joined.
def connect(%{"token" => token}, socket, _connect_info) do
case verify_token(token) do
{:ok, user_id} -> {:ok, assign(socket, :user_id, user_id)}
:error -> :error
end
endGenerating a Token
Phoenix ships Phoenix.Token for signing short-lived tokens. The server signs the user id and the client passes it back when connecting.
token = Phoenix.Token.sign(MyAppWeb.Endpoint, "user socket", user.id)Verifying a Token
On connect, verify the token with the same salt. The max_age option rejects expired tokens.
Phoenix.Token.verify(MyAppWeb.Endpoint, "user socket", token, max_age: 86400)Rejecting Unauthorized Connections
Returning :error (or {:error, reason}) from connect/3 closes the socket immediately. The client never reaches any channel.
def connect(_params, _socket, _info), do: :errorAssigning the Current User
After verifying, store the identity on the socket with assign/3. Every channel on this socket can then read socket.assigns.user_id.
{:ok, assign(socket, :user_id, user_id)}Authorizing Channel Joins
Authorization happens in join/3. Compare the requested topic against the authenticated user before allowing the join.
def join("room:" <> room_id, _params, socket) do
if authorized?(socket.assigns.user_id, room_id) do
{:ok, socket}
else
{:error, %{reason: "unauthorized"}}
end
endScoping Private Topics
A common pattern uses the user id inside the topic name, like user:42. Reject the join if the topic id does not match the connected user.
def join("user:" <> id, _params, socket) do
if id == to_string(socket.assigns.user_id) do
{:ok, socket}
else
{:error, %{reason: "forbidden"}}
end
endClient-Side Token Passing
The JavaScript client sends the token as a connection param. The server reads it in connect/3.
let socket = new Socket('/socket', { params: { token: window.userToken } })
socket.connect()Handling Token Expiry
Tokens are short-lived on purpose. When a token expires the socket disconnects; the client should fetch a fresh token and reconnect.
- Keep
max_ageshort for sensitive apps - Refresh tokens before they expire
- Handle the socket
onErrorevent to reconnect
Security Best Practices
Strengthen channel security:
- Never trust client-supplied user ids — derive identity from the token
- Authorize every topic in
join/3 - Validate incoming payloads in
handle_in/3 - Use HTTPS/WSS so tokens travel encrypted
Quick Check
Test your channel security knowledge.
Recap
You secured Phoenix Channels end to end:
- Authenticate the socket in
connect/3usingPhoenix.Token - Assign the verified user onto the socket
- Authorize each topic in
join/3 - Reject expired tokens and reconnect with fresh ones
Proper auth keeps private real-time data safe.
คำถามที่พบบ่อย
บทเรียน “การยืนยันตัวตนและการอนุญาตสำหรับ Channel” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การยืนยันตัวตนและการอนุญาตสำหรับ Channel” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Elixir & Phoenix: Scalable Backend Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Elixir & Phoenix: Scalable Backend Development มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การยืนยันตัวตนและการอนุญาตสำหรับ Channel”
รักษาความปลอดภัยให้ Phoenix Channels ด้วยการตรวจสอบโทเค็นผู้ใช้ระหว่างขั้นตอนการเข้าร่วม และอนุญาตการเข้าถึงหัวข้อที่กำหนด คุณปฏิบัติ Elixir & Phoenix: Scalable Backend Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Elixir & Phoenix: Scalable Backend Development หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Elixir & Phoenix: Scalable Backend Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การยืนยันตัวตนและการอนุญาตสำหรับ Channel” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Elixir & Phoenix: Scalable Backend Development นี้ได้ไหม
ได้ บทเรียน Elixir & Phoenix: Scalable Backend Development ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- บทนำสู่ Phoenix Channels
- การกระจายข้อความและการรับส่งแบบ Pub/Sub
- การติดตามการมีอยู่และการอัปเดตข้อมูลสด
- การยืนยันตัวตนและการอนุญาตสำหรับ Channel