المصادقة باستخدام Token وJWT
مصادقة عملاء API بأمان
المصادقة باستخدام Token وJWT درس مجاني في Django Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Django Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Django Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
APIs Need Stateless Auth
Browsers use sessions, but API clients often have no cookies. Token authentication lets a client prove who it is on every request instead.
How a Token Works
The user logs in once and gets a token string. They send it with each request, and DRF maps it back to the right user.
Enabling TokenAuthentication
Add the authtoken app and turn on TokenAuthentication so DRF knows to read tokens from incoming requests.
INSTALLED_APPS += ['rest_framework.authtoken']
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.TokenAuthentication',
],
}Issuing a Token
DRF gives you a built-in obtain_auth_token view. Post a username and password to it, and it returns that user's token.
from rest_framework.authtoken.views import obtain_auth_token
urlpatterns = [
path('api-token-auth/', obtain_auth_token),
]Sending the Token
The client puts the token in the Authorization header, prefixed with the word Token, on every protected call. 🔑
Authorization: Token 9944b09199c62bcf9418ad846dd0e4The Limit of Simple Tokens
A DRF token never expires and is just a database lookup. For larger systems, a JWT adds expiry and self-contained data.
What a JWT Carries
A JSON Web Token is a signed string holding claims like the user id and an expiry time. The server trusts it without a DB hit.
Adding SimpleJWT
Install djangorestframework-simplejwt and register its authentication class to swap tokens for JWTs.
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework_simplejwt.authentication.JWTAuthentication',
],
}Access and Refresh Tokens
SimpleJWT gives two tokens: a short-lived access token for requests and a longer refresh token to get a new access token.
JWT Login Endpoints
Wire up TokenObtainPairView to log in and TokenRefreshView to renew, and your JWT flow is ready.
from rest_framework_simplejwt.views import (
TokenObtainPairView, TokenRefreshView)
urlpatterns = [
path('token/', TokenObtainPairView.as_view()),
path('token/refresh/', TokenRefreshView.as_view()),
]Sending a Bearer Token
JWT clients use the Bearer scheme in the Authorization header instead of the Token word DRF tokens use. 🎟️
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9Quick Check
One key difference separates DRF tokens from JWTs.
Recap: Proving Identity
You learned to authenticate API clients with DRF tokens for simplicity and JWTs for expiry and scale, both sent in the Authorization header. 🎉
الأسئلة الشائعة
هل درس «المصادقة باستخدام Token وJWT» مجاني؟
نعم — نص درس «المصادقة باستخدام Token وJWT» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Django Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Django Academy 4 دروس في المجموع.
ماذا ستتعلم في «المصادقة باستخدام Token وJWT»؟
مصادقة عملاء API بأمان تتمرن على Django Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Django Academy؟
لا تُشترط خبرة سابقة. Django Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «المصادقة باستخدام Token وJWT»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Django Academy هذا؟
نعم. كل درس في Django Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- ModelViewSet وأجهزة التوجيه
- الأذونات وتحديد معدل الطلبات
- المصادقة باستخدام Token وJWT
- التصفية والبحث وتقسيم الصفحات