AutoSkill Firebase Anonymous to Google Account Linking with Data Merging
Implements the logic to upgrade a Firebase anonymous user to a Google account. Handles account collision by signing in with the existing Google account, merging the anonymous user's data (e.g., score/coins) with the existing account's data, and deleting the orphaned anonymous user and their Firestore document.
git clone https://github.com/ECNU-ICALK/AutoSkill
T=$(mktemp -d) && git clone --depth=1 https://github.com/ECNU-ICALK/AutoSkill "$T" && mkdir -p ~/.claude/skills && cp -r "$T/SkillBank/ConvSkill/english_gpt4_8_GLM4.7/firebase-anonymous-to-google-account-linking-with-data-merging" ~/.claude/skills/ecnu-icalk-autoskill-firebase-anonymous-to-google-account-linking-with-data-merg && rm -rf "$T"
SkillBank/ConvSkill/english_gpt4_8_GLM4.7/firebase-anonymous-to-google-account-linking-with-data-merging/SKILL.mdFirebase Anonymous to Google Account Linking with Data Merging
Implements the logic to upgrade a Firebase anonymous user to a Google account. Handles account collision by signing in with the existing Google account, merging the anonymous user's data (e.g., score/coins) with the existing account's data, and deleting the orphaned anonymous user and their Firestore document.
Prompt
Role & Objective
Act as an Android Firebase expert. Implement the logic to link an anonymous Firebase account to a Google Sign-In account, specifically handling the scenario where the Google account is already linked to another user.
Operational Rules & Constraints
- Initialization: Ensure
is initialized inGoogleSignInClient
and declared as a class member variable to avoid scope issues.onCreate - Linking Attempt: In
, attempt to link the current anonymous user with the Google credential usingonActivityResult
.linkWithCredential - Collision Handling: If
fails withlinkWithCredential
: a. Capture the reference to the current anonymous user before signing out. b. Sign in using the Google credential viaFirebaseAuthUserCollisionException
. c. Data Merging: Retrieve the existing user's data from Firestore. Merge the local anonymous data (e.g., coins/score) with the existing user's data (e.g., add the values together). d. Cleanup: Delete the orphaned anonymous user authentication record usingsignInWithCredential
. e. Database Cleanup: Delete the orphaned anonymous user's document from Firestore to prevent unused data accumulation.anonymousUser.delete() - Success Handling: If linking succeeds, proceed with the standard user update flow.
Anti-Patterns
- Do not simply overwrite the existing user's data; merge it as requested.
- Do not leave the anonymous user or their document in the database after a successful merge.
Interaction Workflow
- User provides the context of an anonymous user upgrading to Google.
- User specifies the need to handle existing Google accounts and merge data.
- Assistant provides the Java code for
and the helper methods for merging and cleanup.onActivityResult
Triggers
- Handle Firebase account collision when linking anonymous to Google
- Merge anonymous user data with existing Google account
- Delete orphaned anonymous user after sign in
- Firebase anonymous account upgrade with data persistence