0Pricing
Flask Academy · درس

رموز التحديث وانتهاء الصلاحية

بدّل رموز الوصول دون تسجيل الدخول مجددًا.

رموز التحديث وانتهاء الصلاحية درس مجاني في Flask Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Flask Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Flask Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Why Tokens Expire

A stolen token is dangerous only while it works. Giving every access token a short expiry shrinks that window of risk. ⏳

The exp Claim

Expiry lives in the token's exp claim, a timestamp. Once the clock passes it, the token is rejected no matter what.

Set the Lifetime

Control how long access tokens last with JWT_ACCESS_TOKEN_EXPIRES. A short span like 15 minutes is a sensible default.

from datetime import timedelta
app.config["JWT_ACCESS_TOKEN_EXPIRES"] = timedelta(minutes=15)

The Re-Login Problem

Short expiry is safe but annoying if users must type their password every 15 minutes. The refresh token solves exactly this.

Two Tokens, Two Jobs

A short access token calls your API; a long-lived refresh token does nothing but request fresh access tokens.

Issue Both at Login

At login, mint an access token and a refresh token together, then return both to the client.

from flask_jwt_extended import create_refresh_token
rt = create_refresh_token(identity=user.id)

A Refresh Endpoint

Add a /refresh route guarded by jwt_required(refresh=True) so only a valid refresh token can reach it.

@app.post("/refresh")
@jwt_required(refresh=True)
def refresh():
    ...

Hand Back a New Access Token

Inside /refresh, read the identity and mint a brand-new access token. The user keeps going without retyping a password. 🔄

new = create_access_token(identity=get_jwt_identity())
return {"access_token": new}

Refresh Tokens Live Longer

Set JWT_REFRESH_TOKEN_EXPIRES to days or weeks. It is exposed less often, so a longer life is an acceptable trade.

app.config["JWT_REFRESH_TOKEN_EXPIRES"] = timedelta(days=30)

Store the Refresh Token Safely

Because it is powerful, keep the refresh token in secure storage, never in plain JavaScript-readable space, and send it only to /refresh.

Revoking Tokens

To truly log someone out, add a blocklist of token ids the server refuses. This is the one bit of state stateless auth sometimes needs.

Quick Check

Recall the job each token type does.

Recap

Keep access tokens short and pair them with a long-lived refresh token that buys new ones at /refresh. Add a blocklist to revoke. ✅

الأسئلة الشائعة

هل درس «رموز التحديث وانتهاء الصلاحية» مجاني؟

نعم — نص درس «رموز التحديث وانتهاء الصلاحية» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Flask Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Flask Academy 4 دروس في المجموع.

ماذا ستتعلم في «رموز التحديث وانتهاء الصلاحية»؟

بدّل رموز الوصول دون تسجيل الدخول مجددًا. تتمرن على Flask Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Flask Academy؟

لا تُشترط خبرة سابقة. Flask Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «رموز التحديث وانتهاء الصلاحية»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Flask Academy هذا؟

نعم. كل درس في Flask Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. الجلسات مقابل الرموز عديمة الحالة
  2. إصدار رموز الوصول عند تسجيل الدخول
  3. حماية نقاط النهاية باستخدام jwt_required
  4. رموز التحديث وانتهاء الصلاحية
← العودة إلى Flask Academy