Skip to content

Instantly share code, notes, and snippets.

@amalshaji
Created June 23, 2021 06:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amalshaji/76c6b8dae65a1e9f74ee086f03e656c7 to your computer and use it in GitHub Desktop.
Save amalshaji/76c6b8dae65a1e9f74ee086f03e656c7 to your computer and use it in GitHub Desktop.
FastAPI HTTP Basic Logout demo
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