mirror of
https://github.com/maxdorninger/MediaManager.git
synced 2026-04-27 00:05:12 +02:00
feat: implement OAuth2 login
This commit is contained in:
33
backend/src/auth/router.py
Normal file
33
backend/src/auth/router.py
Normal file
@@ -0,0 +1,33 @@
|
||||
from fastapi import APIRouter, Depends
|
||||
from fastapi import status
|
||||
from sqlalchemy import select
|
||||
|
||||
from auth.config import OAuth2Config
|
||||
from auth.db import User
|
||||
from auth.schemas import UserRead
|
||||
from auth.users import current_superuser
|
||||
from database import DbSessionDependency
|
||||
from auth.users import oauth_client
|
||||
|
||||
users_router = APIRouter()
|
||||
auth_metadata_router = APIRouter()
|
||||
oauth_enabled = oauth_client is not None
|
||||
if oauth_enabled:
|
||||
oauth_config = OAuth2Config()
|
||||
|
||||
|
||||
@users_router.get("/users/all", status_code=status.HTTP_200_OK, dependencies=[Depends(current_superuser)])
|
||||
def get_all_users(db: DbSessionDependency) -> list[UserRead]:
|
||||
stmt = select(User)
|
||||
result = db.execute(stmt).scalars().unique()
|
||||
return [UserRead.model_validate(user) for user in result]
|
||||
|
||||
|
||||
@auth_metadata_router.get("/auth/metadata", status_code=status.HTTP_200_OK)
|
||||
def get_auth_metadata() -> dict:
|
||||
if oauth_enabled:
|
||||
return {
|
||||
"oauth_name": oauth_config.name,
|
||||
}
|
||||
else:
|
||||
return {"oauth_name": None}
|
||||
Reference in New Issue
Block a user