Add a Fully registration and authentication or authorization system to your FastAPI project. authx is designed to be as customizable and adaptable as possible.
Here is a simple way to kickstart your project with authx:
fromfastapiimportFastAPI,Depends,HTTPExceptionfromauthximportAuthX,AuthXConfig,RequestTokenapp=FastAPI()config=AuthXConfig(JWT_ALGORITHM="HS256",JWT_SECRET_KEY="SECRET_KEY",JWT_TOKEN_LOCATION=["headers"],)auth=AuthX(config=config)auth.handle_errors(app)@app.get('/login')deflogin(username:str,password:str):ifusername=="xyz"andpassword=="xyz":token=auth.create_access_token(uid=username)return{"access_token":token}raiseHTTPException(401,detail={"message":"Invalid credentials"})@app.get("/protected",dependencies=[Depends(auth.get_token_from_request)])defget_protected(token:RequestToken=Depends()):try:auth.verify_token(token=token)return{"message":"Hello world !"}exceptExceptionase:raiseHTTPException(401,detail={"message":str(e)})frome