0Pricing
Elixir & Phoenix: Scalable Backend Development · Ders

Kanal Kimlik Doğrulama ve Yetkilendirme

Katılma el sıkışması sırasında kullanıcı belirteçlerini doğrulayarak ve belirli konulara erişimi yetkilendirerek Phoenix Channels'ınızı güvence altına alın.

Kanal Kimlik Doğrulama ve Yetkilendirme, CoddyKit'te ücretsiz bir Elixir & Phoenix: Scalable Backend Development dersidir. Bu, 4 dersinin 4. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Elixir & Phoenix: Scalable Backend Development öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Elixir & Phoenix: Scalable Backend Development kursu toplamda 4 dersten oluşur.

Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.

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
end

Generating 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: :error

Assigning 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
end

Scoping 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
end

Client-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_age short for sensitive apps
  • Refresh tokens before they expire
  • Handle the socket onError event 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/3 using Phoenix.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.

Sıkça Sorulan Sorular

“Kanal Kimlik Doğrulama ve Yetkilendirme” dersi ücretsiz mi?

Evet — “Kanal Kimlik Doğrulama ve Yetkilendirme” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Elixir & Phoenix: Scalable Backend Development kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Elixir & Phoenix: Scalable Backend Development kursu toplamda 4 dersten oluşur.

“Kanal Kimlik Doğrulama ve Yetkilendirme” dersinde ne öğreneceğim?

Katılma el sıkışması sırasında kullanıcı belirteçlerini doğrulayarak ve belirli konulara erişimi yetkilendirerek Phoenix Channels'ınızı güvence altına alın. Elixir & Phoenix: Scalable Backend Development ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.

Elixir & Phoenix: Scalable Backend Development öğrenmeye başlamak için deneyim gerekli mi?

Önceden deneyim gerekmez. CoddyKit'te Elixir & Phoenix: Scalable Backend Development, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 4. dersidir.

“Kanal Kimlik Doğrulama ve Yetkilendirme” dersi ne kadar sürer?

Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.

Bu Elixir & Phoenix: Scalable Backend Development dersinde kod yazıp çalıştırabilir miyim?

Evet. Her Elixir & Phoenix: Scalable Backend Development dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.

Bu kursun tüm dersleri

  1. Phoenix Channels'a Giriş
  2. Yayınlama ve Pub/Sub Mesajlaşması
  3. Mevcudiyet ve Canlı Veri Güncellemeleri
  4. Kanal Kimlik Doğrulama ve Yetkilendirme
← Elixir & Phoenix: Scalable Backend Development Sayfasına Dön