Spaces:
Running
Running
Commit ยท
fcc560d
1
Parent(s): c4d1e86
- prompt.txt +9 -0
- requirements.txt +2 -1
- routers/user_router.py +8 -2
- utils/common.py +34 -1
prompt.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
common.py
|
| 2 |
+
|
| 3 |
+
์ฌ๊ธฐ์ dictionary ์ ๋ณด๋ฅผ JWT๋ก ๋ง๋๋ ๋ชจ๋์ ๋ง๋ค์ด ์ค.
|
| 4 |
+
|
| 5 |
+
๋ด ์ต์ข
๋ชฉ์ ์
|
| 6 |
+
@router.post("/login_register")
|
| 7 |
+
api ์์
|
| 8 |
+
update insert๊ฐ ๋๋๋ฉด, ํด๋ผ์ด์ธํธํํ
|
| 9 |
+
์ ์ ์ ๋ณด๋ฅผ JWT ํ ํฐ์ผ๋ก ๋ง๋ค์ด์ ๋ณด๋ด๋๊ฑฐ์ผ.
|
requirements.txt
CHANGED
|
@@ -4,4 +4,5 @@ python-multipart
|
|
| 4 |
python-dotenv
|
| 5 |
pydantic
|
| 6 |
sqlalchemy
|
| 7 |
-
psycopg[binary]
|
|
|
|
|
|
| 4 |
python-dotenv
|
| 5 |
pydantic
|
| 6 |
sqlalchemy
|
| 7 |
+
psycopg[binary]
|
| 8 |
+
python-jose[cryptography]
|
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 |
|
|
@@ -91,3 +96,4 @@ async def upload_file(file: UploadFile = File(...)):
|
|
| 91 |
return CommonResponse(success=True, data=data)
|
| 92 |
except Exception as e:
|
| 93 |
return CommonResponse(success=False, msg=str(e))
|
|
|
|
|
|
| 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 |
|
|
|
|
| 96 |
return CommonResponse(success=True, data=data)
|
| 97 |
except Exception as e:
|
| 98 |
return CommonResponse(success=False, msg=str(e))
|
| 99 |
+
|
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,29 @@ 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)
|
| 43 |
+
|