Created
June 23, 2021 06:10
-
-
Save amalshaji/76c6b8dae65a1e9f74ee086f03e656c7 to your computer and use it in GitHub Desktop.
FastAPI HTTP Basic Logout demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from fastapi import Depends, FastAPI, status | |
from fastapi.responses import HTMLResponse | |
from fastapi.security import HTTPBasic, HTTPBasicCredentials | |
app = FastAPI() | |
security = HTTPBasic() | |
HTML = """ | |
<center> | |
<h1>Hello {username}</h1> | |
<form method="POST" action="/"> | |
<input type="submit" value="Log Out"> | |
</form> | |
</center> | |
""" | |
LOGOUT_HTML = """ | |
<center> | |
<h1>Hello User</h1> | |
<a href="/"><button>Log In</button></a> | |
</center> | |
""" | |
@app.get("/") | |
async def root(creds: HTTPBasicCredentials = Depends(security)): | |
return HTMLResponse(content=HTML.format(username=creds.username)) | |
@app.post("/") | |
async def logout(): | |
return HTMLResponse(content=LOGOUT_HTML, status_code=status.HTTP_401_UNAUTHORIZED) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment