0Pricing
Flask Academy · Lezione

Emettere access token al login

Firmi un JWT e lo restituisca al client.

Emettere access token al login è una lezione Flask Academy gratuita su CoddyKit. Questa è la lezione 2 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.

The Login Handshake

Login is a trade: the client sends valid credentials, and your server hands back a signed access token it can use on every later call.

Install the Extension

The Flask-JWT-Extended library does the signing and checking for you. One pip install and you are ready to mint tokens.

pip install flask-jwt-extended

Set the Signing Key

Tokens are signed with a secret. Configure JWT_SECRET_KEY so only your server can produce valid tokens. Keep it out of source code.

app.config["JWT_SECRET_KEY"] = "change-me"

Create the JWTManager

Wrap your app in a JWTManager. This one object hooks Flask-JWT-Extended into request handling so the helpers work.

from flask_jwt_extended import JWTManager
jwt = JWTManager(app)

A Route to Log In

Add a POST route at /login. It will read the submitted username and password from the request body.

@app.post("/login")
def login():
    ...

Verify Credentials First

Never skip the check: confirm the password matches your stored hash before issuing anything. No valid login, no token.

Mint the Token

Call create_access_token with an identity, usually the user id. That id becomes the token's subject claim.

from flask_jwt_extended import create_access_token
token = create_access_token(identity=user.id)

Return It as JSON

Send the token back in a JSON body. The client stores it and replays it on protected calls. 🔑

return {"access_token": token}, 200

Identity Drives Claims

The identity you pass is what later requests will read back. Keep it small and stable, like a numeric user id.

Add Extra Claims

Need a role or plan in the token? Pass additional_claims so the data rides along and is available without a database hit.

create_access_token(identity=uid, additional_claims={"role": "admin"})

Fail Logins Cleanly

If credentials are wrong, return 401 Unauthorized and no token. Clear, consistent failures keep your API predictable.

return {"msg": "Bad credentials"}, 401

Quick Check

Recall which call actually produces the token.

Recap

Verify credentials, then create_access_token with the user id and return it as JSON. Set JWT_SECRET_KEY and a JWTManager first. ✅

Domande Frequenti

La lezione «Emettere access token al login» è gratuita?

Sì — il testo completo di «Emettere access token al login» è 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 «Emettere access token al login»?

Firmi un JWT e lo restituisca al client. 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 2 di 4.

Quanto tempo richiede la lezione «Emettere access token al login»?

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