Download app/routers/admin/auth.py from binhugg2/api: direct link, hf CLI and curl.
- Browser
- Download file 2.29 kB
-
https://huggingface.co/spaces/binhugg2/api/resolve/main/app/routers/admin/auth.py
- Command line
-
hf download hf://spaces/binhugg2/api/app/routers/admin/auth.py
-
curl -L -o auth.py https://huggingface.co/spaces/binhugg2/api/resolve/main/app/routers/admin/auth.py
2.29 kB
| from fastapi import APIRouter, Depends, Request | |
| from sqlalchemy.ext.asyncio import AsyncSession | |
| from app.database import get_db | |
| from app.dependencies import get_current_admin | |
| from app.limiter import limiter | |
| from app.schemas.admin.auth import LoginRequest, LoginResponse, UserInfoResponse | |
| from app.schemas.common import ApiResponse | |
| from app.services import auth_admin | |
| from app.utils.auth import create_access_token, verify_password | |
| router = APIRouter() | |
| async def login(request: Request, req: LoginRequest, db: AsyncSession = Depends(get_db)): | |
| admin = await auth_admin.authenticate_admin(db, req.username, req.password) | |
| if not admin: | |
| return ApiResponse.fail(40001, "用户名或密码错误") | |
| token = create_access_token(str(admin.id), "admin") | |
| return ApiResponse.ok(LoginResponse(accessToken=token, expires_in=1440).model_dump()) | |
| async def logout(admin=Depends(get_current_admin)): | |
| return ApiResponse.ok() | |
| async def get_user_info( | |
| admin=Depends(get_current_admin), | |
| db: AsyncSession = Depends(get_db), | |
| ): | |
| roles = await auth_admin.get_admin_roles(db, admin.id) | |
| return ApiResponse.ok( | |
| UserInfoResponse( | |
| userId=str(admin.id), | |
| username=admin.username, | |
| realName=admin.nickname, | |
| roles=roles, | |
| avatar=admin.avatar, | |
| ).model_dump() | |
| ) | |
| async def get_access_codes( | |
| admin=Depends(get_current_admin), | |
| db: AsyncSession = Depends(get_db), | |
| ): | |
| codes = await auth_admin.get_admin_permissions(db, admin.id) | |
| return ApiResponse.ok(codes) | |
| async def get_menus( | |
| admin=Depends(get_current_admin), | |
| db: AsyncSession = Depends(get_db), | |
| ): | |
| menus = await auth_admin.get_menu_tree(db, admin.id) | |
| return ApiResponse.ok(menus) | |
| async def get_permissions( | |
| admin=Depends(get_current_admin), | |
| db: AsyncSession = Depends(get_db), | |
| ): | |
| tree = await auth_admin.get_permission_tree(db) | |
| return ApiResponse.ok(tree) |