0Pricing
Flask Academy · Lezione

Refresh token e scadenza

Ruoti gli access token senza richiedere un nuovo login.

Refresh token e scadenza è una lezione Flask Academy gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Flask Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Flask Academy include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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. ✅

Domande Frequenti

La lezione «Refresh token e scadenza» è gratuita?

Sì — il testo completo di «Refresh token e scadenza» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Flask Academy, passa a CoddyKit PRO. Il corso Flask Academy include 4 lezioni in totale.

Cosa imparerò in «Refresh token e scadenza»?

Ruoti gli access token senza richiedere un nuovo login. Eserciti Flask Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Flask Academy?

Non è richiesta alcuna esperienza precedente. Flask Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «Refresh token e scadenza»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Flask Academy?

Sì. Ogni lezione Flask Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Sessioni e token stateless
  2. Emettere access token al login
  3. Proteggere gli endpoint con jwt_required
  4. Refresh token e scadenza
← Torna a Flask Academy