Spaces:
Running
Running
Commit ·
80cb726
1
Parent(s): e33e681
- requirements.txt +1 -0
- routers/user_router.py +7 -2
- utils/common.py +33 -1
requirements.txt
CHANGED
|
@@ -2,6 +2,7 @@ fastapi
|
|
| 2 |
uvicorn[standard]
|
| 3 |
python-multipart
|
| 4 |
python-dotenv
|
|
|
|
| 5 |
pydantic
|
| 6 |
sqlalchemy
|
| 7 |
psycopg[binary]
|
|
|
|
| 2 |
uvicorn[standard]
|
| 3 |
python-multipart
|
| 4 |
python-dotenv
|
| 5 |
+
python-jose[cryptography]
|
| 6 |
pydantic
|
| 7 |
sqlalchemy
|
| 8 |
psycopg[binary]
|
routers/user_router.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
from fastapi import APIRouter, Form, File, UploadFile
|
| 2 |
-
from utils.common import CommonResponse
|
| 3 |
from utils.db import get_connection
|
| 4 |
|
| 5 |
router = APIRouter(
|
|
@@ -74,7 +74,12 @@ def login_register(provider: str = Form(""), firebase_uid: str = Form(""),
|
|
| 74 |
"created_at": row[9].isoformat() if row[9] else None,
|
| 75 |
"updated_at": row[10].isoformat() if row[10] else None
|
| 76 |
}
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
except Exception as e:
|
| 79 |
return CommonResponse(success=False, msg=str(e))
|
| 80 |
|
|
|
|
| 1 |
from fastapi import APIRouter, Form, File, UploadFile
|
| 2 |
+
from utils.common import CommonResponse, create_jwt_token
|
| 3 |
from utils.db import get_connection
|
| 4 |
|
| 5 |
router = APIRouter(
|
|
|
|
| 74 |
"created_at": row[9].isoformat() if row[9] else None,
|
| 75 |
"updated_at": row[10].isoformat() if row[10] else None
|
| 76 |
}
|
| 77 |
+
access_token = create_jwt_token(data)
|
| 78 |
+
return CommonResponse(success=True, data={
|
| 79 |
+
"access_token": access_token,
|
| 80 |
+
"token_type": "bearer",
|
| 81 |
+
"user": data
|
| 82 |
+
})
|
| 83 |
except Exception as e:
|
| 84 |
return CommonResponse(success=False, msg=str(e))
|
| 85 |
|
utils/common.py
CHANGED
|
@@ -1,6 +1,13 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from pydantic import BaseModel
|
| 3 |
|
|
|
|
|
|
|
| 4 |
T = TypeVar("T")
|
| 5 |
|
| 6 |
class CommonResponse(BaseModel, Generic[T]):
|
|
@@ -8,3 +15,28 @@ class CommonResponse(BaseModel, Generic[T]):
|
|
| 8 |
data: Optional[T] = None
|
| 9 |
msg: str = ""
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from datetime import date, datetime, timedelta, timezone
|
| 3 |
+
from typing import Any, Generic, Optional, TypeVar
|
| 4 |
+
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
from jose import jwt
|
| 7 |
from pydantic import BaseModel
|
| 8 |
|
| 9 |
+
load_dotenv()
|
| 10 |
+
|
| 11 |
T = TypeVar("T")
|
| 12 |
|
| 13 |
class CommonResponse(BaseModel, Generic[T]):
|
|
|
|
| 15 |
data: Optional[T] = None
|
| 16 |
msg: str = ""
|
| 17 |
|
| 18 |
+
|
| 19 |
+
def _json_default(value: Any):
|
| 20 |
+
if isinstance(value, (datetime, date)):
|
| 21 |
+
return value.isoformat()
|
| 22 |
+
raise TypeError(f"{type(value).__name__} is not JSON serializable")
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def _json_safe_dict(data: dict) -> dict:
|
| 26 |
+
return {
|
| 27 |
+
key: _json_default(value) if isinstance(value, (datetime, date)) else value
|
| 28 |
+
for key, value in data.items()
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
# = 60분 * 24시간 * 7일
|
| 32 |
+
def create_jwt_token(data: dict, expires_minutes: int = 60 * 24 * 365) -> str:
|
| 33 |
+
secret_key = os.getenv("JWT_SECRET_KEY", "change-this-jwt-secret-key")
|
| 34 |
+
algorithm = "HS256"
|
| 35 |
+
now = datetime.now(timezone.utc)
|
| 36 |
+
|
| 37 |
+
payload = {
|
| 38 |
+
**_json_safe_dict(data),
|
| 39 |
+
"iat": now,
|
| 40 |
+
"exp": now + timedelta(minutes=expires_minutes),
|
| 41 |
+
}
|
| 42 |
+
return jwt.encode(payload, secret_key, algorithm=algorithm)
|