fix: 마이페이지 카카오 로그인 시 계정 머지(연동) 누락 수정

- _handle_mypage_kakao_callback()에서 link_kakao_identity() 호출 추가
- 키오스크(번호) → 알림톡 → 카카오 로그인 시 자동 머지
- "고객" 이름 → 카카오 실명으로 자동 업데이트
- 케이스별 시나리오 문서 추가 (docs/user-identity-merge.md)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
thug0bin
2026-02-25 17:25:46 +09:00
parent e7c529c22c
commit 2625430ca5
2 changed files with 214 additions and 11 deletions

View File

@@ -791,7 +791,15 @@ def claim_kakao_start():
def _handle_mypage_kakao_callback(code, kakao_client):
"""마이페이지 카카오 콜백 처리 - 카카오 ID로 유저 조회 후 마이페이지 이동"""
"""
마이페이지 카카오 콜백 처리 - 카카오 연동(머지) + 마이페이지 이동
케이스별 동작:
A) 카카오 ID가 이미 연결된 유저 → 그 유저의 마이페이지로 이동
B) 미연결 + 카카오 전화번호로 기존 유저 발견 → 카카오 연동 후 이동
C) 미연결 + 기존 유저 없음 → 신규 생성 + 카카오 연동
D) 전화번호 없음 → 에러 안내
"""
success, token_data = kakao_client.get_access_token(code)
if not success:
return render_template('error.html', message="카카오 인증에 실패했습니다.")
@@ -804,24 +812,51 @@ def _handle_mypage_kakao_callback(code, kakao_client):
kakao_id = user_info.get('kakao_id')
kakao_phone_raw = user_info.get('phone_number')
kakao_phone = normalize_kakao_phone(kakao_phone_raw)
kakao_name = user_info.get('name') or user_info.get('nickname', '고객')
# 1) 카카오 ID로 기존 유저 조회
conn = db_manager.get_sqlite_connection()
cursor = conn.cursor()
# Case A: 카카오 ID로 이미 연결된 유저 있음
existing_user_id = find_user_by_kakao_id(kakao_id)
if existing_user_id:
conn = db_manager.get_sqlite_connection()
cursor = conn.cursor()
# "고객" 이름이면 카카오 실명으로 업데이트
if kakao_name and kakao_name != '고객':
cursor.execute(
"UPDATE users SET nickname = ? WHERE id = ? AND nickname = '고객'",
(kakao_name, existing_user_id))
conn.commit()
cursor.execute("SELECT phone FROM users WHERE id = ?", (existing_user_id,))
row = cursor.fetchone()
if row:
if row and row['phone']:
return redirect(f"/my-page?phone={row['phone']}")
# 2) 카카오에서 전화번호를 받은 경우
if kakao_phone:
return redirect(f"/my-page?phone={kakao_phone}")
# 전화번호 없으면 연동 불가
if not kakao_phone:
return render_template('error.html',
message="카카오 계정에 전화번호 정보가 없습니다. 카카오 설정에서 전화번호를 등록해주세요.")
# 3) 둘 다 없으면 전화번호 입력 안내
return render_template('error.html',
message="카카오 계정에 연결된 적립 정보가 없습니다. 전화번호로 조회해주세요.")
# Case B/C: 전화번호로 기존 유저 조회
cursor.execute("SELECT id, nickname FROM users WHERE phone = ?", (kakao_phone,))
phone_user = cursor.fetchone()
if phone_user:
# Case B: 기존 전화번호 유저 → 카카오 연동 (머지)
user_id = phone_user['id']
link_kakao_identity(user_id, kakao_id, user_info)
# "고객" 이름이면 카카오 실명으로 업데이트
if phone_user['nickname'] == '고객' and kakao_name and kakao_name != '고객':
cursor.execute("UPDATE users SET nickname = ? WHERE id = ?",
(kakao_name, user_id))
conn.commit()
logging.info(f"마이페이지 카카오 머지: user_id={user_id}, kakao_id={kakao_id}")
else:
# Case C: 신규 유저 생성 + 카카오 연동
user_id, _ = get_or_create_user(kakao_phone, kakao_name)
link_kakao_identity(user_id, kakao_id, user_info)
logging.info(f"마이페이지 카카오 신규: user_id={user_id}, kakao_id={kakao_id}")
return redirect(f"/my-page?phone={kakao_phone}")
@app.route('/claim/kakao/callback')