Package: fastapi-authtools
FastAPI auth library.It`s simple to connect to your project. Just make user_data verification, and library will manage JWT-tokens.InstallationInstall package with pip:pip install fastapi-authtools...or with poetry:poetry add fastapi-authtoolsUsageYou can you it with JWT-token (default in you headers).fromfastapiimportFastAPI,Request,Bodyfromfastapi_authtoolsimportAuthManager,login_requiredfromfastapi_authtools.modelsimportUsernamePasswordToken,UserModelapp=FastAPI()# JWT token settingsSECRET_KEY='secretSERCRET007'EXPIRE_MINUTES=60*40ALGORITHM="HS256"# create login managerauth_manager=AuthManager(app=app,secret_key=SECRET_KEY,algorithm=ALGORITHM,expire_minutes=EXPIRE_MINUTES)# now you can use login_manager directly or py adding it to the application statement# it`s comfortable while dealing with APIRoutersapp.state.auth_manager=auth_manager@app.get("/")@login_required# make this endpoint allowed only for authenticated usersasyncdefhomepage(request:Request):current_user=request.userreturn{"current_user":current_user}@app.post("/auth/token",status_code=201)asyncdefget_access_token(request:Request,user_data:UsernamePasswordToken=Body()):# ... here goes db user verification and getting user information# user = get_login_user(user_data)user=UserModel(email="suslanchikmol@gmail.con",username="michael7nightingale")token=request.app.state.auth_manager.create_token(user)return{"access_token":token}But you can still use cookies to save token, just defineuser_cookiesas True when initialize AuthManager.To use templates and form data you should installjinja2andpython-multipart.fromfastapiimportFastAPI,Requestfromfastapi.responsesimportRedirectResponsefromfastapi.templatingimportJinja2Templatesfromfastapi_authtoolsimportAuthManager,login_requiredfromfastapi_authtools.modelsimportUserModelapp=FastAPI()templates=Jinja2Templates(directory="templates")# JWT token settingsSECRET_KEY='secretSERCRET007'EXPIRE_MINUTES=60*40ALGORITHM="HS256"# create login managerauth_manager=AuthManager(app=app,use_cookies=True,secret_key=SECRET_KEY,algorithm=ALGORITHM,expire_minutes=EXPIRE_MINUTES)# now you can use login_manager directly or py adding it to the application statement# it`s comfortable while dealing with APIRoutersapp.state.auth_manager=auth_manager@app.get("/")@login_required# make this endpoint allowed only for authenticated usersasyncdefhomepage(request:Request):returntemplates.TemplateResponse(name="homepage.html",context={"request":request,"current_user":request.user})@app.get('/login')asyncdeflogin_get(request:Request):returntemplates.TemplateResponse(name='login.html',context={"request":request})@app.post("/login",status_code=201)asyncdeflogin_post(request:Request):user_data=awaitrequest.form()# ... here goes db user verification and getting user information# user = get_login_user(user_data)user=UserModel(email="suslanchikmol@gmail.con",username="michael7nightingale")response=RedirectResponse(app.url_path_for("homepage"),status_code=303)app.state.auth_manager.login(response,user)returnresponseAuth manager adds authentication middleware to your application instance and uses authentication backends to treat token and
request user instance.