Skip to content

Commit 9e3ed2a

Browse files
ImTotemclaude
andcommitted
fix(auth): link Google account to existing member by school_email
When registering with a school_email that already exists, instead of rejecting with CONFLICT, link the new Google account to the existing member and log them in. This enables multi-Google-account support. Flow: - New school_email → create member + link Google account - Existing school_email → link Google account to existing member Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 0f3e4ed commit 9e3ed2a

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

src/bcsd_api/auth/service.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,40 @@ def register(
4242
) -> tuple[str, str]:
4343
profile = google_auth.verify_token(google_token, settings.google_client_id)
4444
_check_google(profile["email"], repo)
45-
_check_school_email(school_email, repo)
45+
existing = repo.find_by_school_email(school_email)
46+
if existing:
47+
return _link_account(existing, profile["email"], settings, repo, conn)
48+
return _create_member(
49+
profile["email"], name, department, student_id,
50+
school_email, phone, track, grade, settings, repo, conn,
51+
)
52+
53+
54+
def _link_account(
55+
member: dict, google_email: str,
56+
settings: Settings, repo: PgMemberRepository, conn: Connection,
57+
) -> tuple[str, str]:
58+
repo.add_account(generate_id("MA"), member["id"], "google", google_email, _now_kst())
59+
routing = _resolve_routing(member.get("grade", ""), conn)
60+
token = _issue_jwt({"sub": member["id"], "email": google_email}, settings)
61+
return token, routing
62+
63+
64+
def _create_member(
65+
google_email: str, name: str, department: str,
66+
student_id: str, school_email: str, phone: str,
67+
track: str | None, grade: str,
68+
settings: Settings, repo: PgMemberRepository, conn: Connection,
69+
) -> tuple[str, str]:
4670
member_id = generate_id("M")
47-
now = _now_kst()
4871
row = _build_row(
49-
member_id, name, profile["email"],
72+
member_id, name, google_email,
5073
department, student_id, school_email, phone, track, grade,
5174
)
5275
repo.create(row)
53-
repo.add_account(generate_id("MA"), member_id, "google", profile["email"], now)
76+
repo.add_account(generate_id("MA"), member_id, "google", google_email, _now_kst())
5477
routing = _resolve_routing(grade, conn)
55-
token = _issue_jwt({"sub": member_id, "email": profile["email"]}, settings)
78+
token = _issue_jwt({"sub": member_id, "email": google_email}, settings)
5679
return token, routing
5780

5881

@@ -68,11 +91,6 @@ def _check_google(email: str, repo: PgMemberRepository) -> None:
6891
raise Conflict("이미 가입된 Google 계정입니다")
6992

7093

71-
def _check_school_email(school_email: str, repo: PgMemberRepository) -> None:
72-
if repo.find_by_school_email(school_email):
73-
raise Conflict("이미 가입된 학교 이메일입니다")
74-
75-
7694
def _now_kst() -> str:
7795
return datetime.now(KST).strftime("%Y-%m-%d %H:%M:%S")
7896

0 commit comments

Comments
 (0)