diff --git a/app/api/v1/routers/CustomerAuthRouter.py b/app/api/v1/routers/CustomerAuthRouter.py index af35bc3..6ceeca6 100644 --- a/app/api/v1/routers/CustomerAuthRouter.py +++ b/app/api/v1/routers/CustomerAuthRouter.py @@ -61,6 +61,7 @@ class TokenResponse(BaseModel): customer_id: str email: str first_name: str + last_name: Optional[str] = None # --- Helper Functions --- @@ -106,7 +107,8 @@ def issue_tokens(db: Session, customer: EcomCustomer, response: Response, token_ access_token=access_token, customer_id=customer.customer_id, email=customer.email, - first_name=customer.first_name + first_name=customer.first_name, + last_name=customer.last_name ) def perform_guest_cart_merge(db: Session, customer_id: str, guest_session_id: Optional[str]): @@ -411,8 +413,16 @@ def google_oauth_callback( ) # First-time registration with Google - first_name = idinfo.get("given_name", "Google") - last_name = idinfo.get("family_name", "User") + full_name = idinfo.get("name") or "" + given = idinfo.get("given_name") + family = idinfo.get("family_name") + if not given and full_name: + parts = full_name.strip().split(" ", 1) + first_name = parts[0] + last_name = parts[1] if len(parts) > 1 else "" + else: + first_name = given or full_name or "Google" + last_name = family or "" customer = EcomCustomer( customer_id=str(ulid.ULID()), @@ -432,6 +442,18 @@ def google_oauth_callback( customer.last_login = datetime.now(timezone.utc).replace(tzinfo=None) if picture: customer.profile_picture = picture + given = idinfo.get("given_name") + family = idinfo.get("family_name") + full_name = idinfo.get("name") or "" + if given and (not customer.first_name or customer.first_name == "Google"): + customer.first_name = given + elif full_name and (not customer.first_name or customer.first_name == "Google"): + parts = full_name.strip().split(" ", 1) + customer.first_name = parts[0] + if len(parts) > 1 and not customer.last_name: + customer.last_name = parts[1] + if family and not customer.last_name: + customer.last_name = family db.commit() # Idempotent and transactional guest cart merge