kimsaeromi commited on
Commit
fcc560d
ยท
1 Parent(s): c4d1e86
Files changed (4) hide show
  1. prompt.txt +9 -0
  2. requirements.txt +2 -1
  3. routers/user_router.py +8 -2
  4. 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
- return CommonResponse(success=True, data=data)
 
 
 
 
 
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
- from typing import Generic, TypeVar, Any, Optional
 
 
 
 
 
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
+