FastAPI built-in tools فراہم کرتا ہے (OAuth2PasswordBearer, security utilities) authentication implement کرنے کے لیے، عام طور پر OAuth2 password flow with JWT tokens استعمال کرتے ہوئے۔ یہ pattern token issuance (login) کو ایک dependency کے ساتھ جوڑتا ہے جو protected routes پر token کی تصدیق کرتا ہے۔
Login پر passwords کو hash کرنا اور 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, : }
