feat: 구글 로그인 — 로그인 창에 구글로 계속하기 버튼, 서버가 구글 ID 토큰 서명·클라이언트 ID·만료를 확인, 처음이면 자동 닉네임으로 가입(구글 이름·이메일은 저장 안 함)

This commit is contained in:
minaChoi committed 2026-10-21 08:52:34 +09:00
1 parent eb2151164f
commit 4e9b0bacba
15 files changed
+407 -13

No files matched your search

+31
View File
@@ -1,4 +1,5 @@
import re
import secrets
import uuid
from typing import Optional
@@ -6,6 +7,7 @@ from fastapi import Depends
from common.auth import PASSWORD_MAX_BYTES, decode, hash_password, issue_tokens, verify_password
from common.database.db_session_manager import DB_SESSION_MNG
from common.google_id import verify as verify_google
from common.database.model.models import User
from common.enums import ErrorType
from config.settings import settings
@@ -19,6 +21,15 @@ COLORS = ("orange", "blue", "green", "purple", "pink", "brown")
MOODS = ("clear", "partly", "cloudy", "rain", "thunder", "snow", "fog", "rainbow")
_NICK_WORDS = ("맑은", "포근한", "산들", "반짝", "느긋한", "보슬", "선선한", "따끈한")
_NICK_ANIMALS = {"rabbit": "토끼", "bear": "곰", "dog": "강아지", "sun": "해님", "moon": "달님", "cloud": "구름"}
def auto_nickname() -> str:
"""구글로 처음 들어온 사람의 이름: 맑은토끼 482. 내 정보에서 바꿀 수 있다."""
return f"{secrets.choice(_NICK_WORDS)}{secrets.choice(list(_NICK_ANIMALS.values()))} {secrets.randbelow(900) + 100}"
class AuthService:
def __init__(self, crud: UserCRUD = Depends(UserCRUD)) -> None:
self.crud = crud
@@ -47,6 +58,26 @@ class AuthService:
return ErrorType.AUTH_FAILED, None
return ErrorType.SUCCESS, user
async def google(self, credential: str) -> tuple[ErrorType, Optional[User]]:
"""구글 ID 토큰으로 로그인. 처음이면 자동 닉네임으로 계정을 만든다. 구글 이름·이메일은 저장하지 않는다."""
sub = await verify_google(credential)
if sub is None:
return ErrorType.AUTH_FAILED, None
login_id = f"google_{sub}"
err, user = await DB_SESSION_MNG.execute_lambda(lambda s: self.crud.by_login_id(s, login_id))
if err is not ErrorType.SUCCESS:
return err, None
if user is None:
err, user = await DB_SESSION_MNG.execute_lambda_write(lambda s: self.crud.create_google(s, sub, auto_nickname()))
if err is not ErrorType.SUCCESS:
# 같은 계정이 동시에 처음 들어온 경우엔 먼저 만들어진 쪽을 쓴다
err, user = await DB_SESSION_MNG.execute_lambda(lambda s: self.crud.by_login_id(s, login_id))
if err is not ErrorType.SUCCESS or user is None:
return ErrorType.DB_ERROR, None
if user.status != 1:
return ErrorType.AUTH_FAILED, None
return ErrorType.SUCCESS, user
async def refresh(self, refresh_token: str) -> tuple[ErrorType, Optional[User]]:
claims = decode(refresh_token, settings.jwt_refresh_secret, "refresh")
if claims is None: