FastAPI authentication लागू करने के लिए built-in tools (OAuth2PasswordBearer, security utilities) प्रदान करता है, जो आमतौर पर JWT tokens के साथ OAuth2 password flow का उपयोग करता है। यह pattern token issuance (login) को एक ऐसी dependency के साथ जोड़ता है जो protected routes पर token को validate करती है।
passwords को hash करना और login पर JWT जारी करना
passlib.context CryptContext
jose jwt
datetime datetime, timedelta
pwd = CryptContext(schemes=[])
():
user = get_user(form.username)
user pwd.verify(form.password, user.hashed_password):
HTTPException(, )
token = jwt.encode(
{: user.username, : datetime.utcnow() + timedelta(minutes=)},
SECRET_KEY, algorithm=,
)
{: token, : }
