diff --git a/backend/app.py b/backend/app.py index c2191c0..b349cb3 100644 --- a/backend/app.py +++ b/backend/app.py @@ -649,7 +649,15 @@ def claim(): except Exception as e: logging.warning(f"품목 조회 실패 (transaction_id={transaction_id}): {e}") - return render_template('claim_form.html', token_info=token_info, sale_items=sale_items) + # JS SDK용 카카오 state 생성 (CSRF 보호) + csrf_token = secrets.token_hex(16) + state_data = {'t': token_param, 'csrf': csrf_token} + kakao_state = base64.urlsafe_b64encode( + json.dumps(state_data).encode() + ).decode() + session['kakao_csrf'] = csrf_token + + return render_template('claim_form.html', token_info=token_info, sale_items=sale_items, kakao_state=kakao_state) @app.route('/api/claim', methods=['POST']) @@ -876,6 +884,11 @@ def claim_kakao_callback(): kakao_phone_raw = user_info.get('phone_number') kakao_phone = normalize_kakao_phone(kakao_phone_raw) + # 카카오에서 받은 생년월일 조합 (YYYY-MMDD) + kakao_birthday = None + if user_info.get('birthyear') and user_info.get('birthday'): + kakao_birthday = f"{user_info['birthyear']}-{user_info['birthday'][:2]}-{user_info['birthday'][2:]}" + # 7. 분기: 전화번호가 있으면 자동 적립, 없으면 폰 입력 폼 if kakao_phone: # 자동 적립 @@ -883,8 +896,13 @@ def claim_kakao_callback(): if existing_user_id: user_id = existing_user_id is_new = False + # 생년월일이 있으면 업데이트 + if kakao_birthday: + conn = db_manager.get_sqlite_connection() + conn.cursor().execute("UPDATE users SET birthday = ? WHERE id = ? AND birthday IS NULL", (kakao_birthday, user_id)) + conn.commit() else: - user_id, is_new = get_or_create_user(kakao_phone, kakao_name) + user_id, is_new = get_or_create_user(kakao_phone, kakao_name, birthday=kakao_birthday) link_kakao_identity(user_id, kakao_id, user_info) @@ -1006,7 +1024,14 @@ def my_page(): phone = request.args.get('phone', '') if not phone: - return render_template('my_page_login.html') + # JS SDK용 카카오 state 생성 + csrf_token = secrets.token_hex(16) + state_data = {'purpose': 'mypage', 'csrf': csrf_token} + kakao_state = base64.urlsafe_b64encode( + json.dumps(state_data).encode() + ).decode() + session['kakao_csrf'] = csrf_token + return render_template('my_page_login.html', kakao_state=kakao_state) # 전화번호로 사용자 조회 phone = phone.replace('-', '').replace(' ', '') diff --git a/backend/services/kakao_client.py b/backend/services/kakao_client.py index 17f1009..a6313ad 100644 --- a/backend/services/kakao_client.py +++ b/backend/services/kakao_client.py @@ -39,7 +39,7 @@ class KakaoAPIClient: 'client_id': self.client_id, 'redirect_uri': self.redirect_uri, 'response_type': 'code', - 'scope': 'profile_nickname,profile_image,account_email,name,phone_number,birthday,birthyear' + 'scope': 'profile_nickname,profile_image,account_email,name,phone_number,birthday' } if state: diff --git a/backend/templates/claim_form.html b/backend/templates/claim_form.html index e24786f..a0a96ed 100644 --- a/backend/templates/claim_form.html +++ b/backend/templates/claim_form.html @@ -569,17 +569,17 @@
- 카카오로 적립하기 - + @@ -732,6 +732,28 @@ successScreen.style.display = 'block'; } + + + +