forked from spacebar/SplatNet-Backend
Add logging out
This commit is contained in:
parent
ba581c4ec5
commit
b131e78e90
2 changed files with 50 additions and 0 deletions
48
routes/logout.py
Normal file
48
routes/logout.py
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
from fastapi import APIRouter, Request, Form, Response, Depends
|
||||
from fastapi.responses import RedirectResponse
|
||||
from sqlalchemy.orm import Session as DBSession
|
||||
from database import SessionLocal, User, Session
|
||||
from config import settings, cipher
|
||||
from services import auth
|
||||
from typing import Optional
|
||||
import uuid
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
def get_db():
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@router.post('/spfn/logout')
|
||||
async def logout(
|
||||
request: Request,
|
||||
frontend_origin: Optional[str] = Form(None),
|
||||
db: DBSession = Depends(get_db)
|
||||
):
|
||||
host = (frontend_origin or request.headers.get("referer") or "/").rstrip('/')
|
||||
redirect_url = f"{host}/sign_in/"
|
||||
|
||||
response = RedirectResponse(url=redirect_url, status_code=303)
|
||||
session_id = request.cookies.get("session_id")
|
||||
|
||||
if session_id:
|
||||
try:
|
||||
db.query(Session).filter(Session.id == session_id).delete()
|
||||
db.commit()
|
||||
print(f"Session {session_id} deleted from database.")
|
||||
except Exception as e:
|
||||
print(f"Error deleting session: {e}")
|
||||
db.rollback()
|
||||
|
||||
response.delete_cookie(
|
||||
key="session_id",
|
||||
path="/",
|
||||
domain=None,
|
||||
httponly=settings.cookie_httponly,
|
||||
samesite="lax"
|
||||
)
|
||||
|
||||
return response
|
||||
Loading…
Reference in a new issue