Skip to content

Commit 261ecd3

Browse files
authored
Merge pull request #53 from Tech-With-Tim/feature/users
`/users/@me` endpoint
2 parents 5d49f7f + b973e8a commit 261ecd3

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

api/versions/v1/routers/router.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from fastapi import APIRouter
22

3-
from . import auth
4-
from . import roles
3+
from . import auth, roles, users
54

65
router = APIRouter(prefix="/v1")
76

87
router.include_router(auth.router)
98
router.include_router(roles.router)
9+
router.include_router(users.router)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .routes import router
2+
3+
__all__ = (router,)
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from typing import List
2+
from pydantic import BaseModel
3+
4+
5+
class UserResponse(BaseModel):
6+
id: str
7+
username: str
8+
discriminator: str
9+
avatar: str
10+
app: bool
11+
roles: List[str]
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from fastapi import APIRouter
2+
3+
from .models import UserResponse
4+
5+
from api.models import User, UserRole
6+
from api.dependencies import authorization
7+
8+
9+
router = APIRouter(prefix="/users")
10+
11+
12+
@router.get(
13+
"/@me",
14+
response_model=UserResponse,
15+
responses={401: {"description": "Unauthorized"}},
16+
)
17+
async def get_current_user(user: User = authorization()):
18+
query = """SELECT role_id FROM userroles WHERE user_id = $1;"""
19+
roles = [record["role_id"] for record in await UserRole.pool.fetch(query, user.id)]
20+
21+
return {**user.as_dict(), "roles": roles}

0 commit comments

Comments
 (0)