Overview
Account linking allows you to connect multiple authentication methods (login methods) to a single user account. For example, a user could sign up with email/password and later link their Google account to the same profile.POST /recipe/accountlinking/user/link
Link a recipe user to a primary user account.Request Body
Parameters
- recipeUserId (string, required): The user ID of the account to link (e.g., a Google login user ID)
- primaryUserId (string, required): The primary user ID to link to
Response
Success Response
Error Responses
Account Info Already AssociatedExample
GET /recipe/accountlinking/user/link/check
Check if two accounts can be linked without actually linking them.Query Parameters
- recipeUserId (string, required): The user ID to potentially link
- primaryUserId (string, required): The primary user ID to potentially link to
Response
Success Response
Error Responses
Same error responses as the link endpoint (see above).Example
POST /recipe/accountlinking/user/unlink
Unlink a recipe user from its primary user account.Request Body
Parameters
- recipeUserId (string, required): The recipe user ID to unlink
Response
Response Fields
- wasRecipeUserDeleted:
trueif the recipe user was deleted after unlinking - wasLinked:
trueif the user was linked before this operation,falseif it wasn’t linked
Example
Implementation Details
- Link API located in:
src/main/java/io/supertokens/webserver/api/accountlinking/LinkAccountsAPI.java:47 - Can Link API located in:
src/main/java/io/supertokens/webserver/api/accountlinking/CanLinkAccountsAPI.java:44 - Unlink API located in:
src/main/java/io/supertokens/webserver/api/accountlinking/UnlinkAccountAPI.java:40 - Recipe:
ACCOUNT_LINKING - All endpoints are app-specific
- Support user ID mapping (accepts external user IDs)
- Users must be in the same database/user pool to be linked
- Active user timestamps are updated when accounts are linked
Account Linking Rules
Prerequisites
- The
primaryUserIdmust belong to a primary user - The
recipeUserIdmust not already be linked to another primary user - The account info (email/phone) must not already be associated with another primary user
- Both users must be in the same user pool (same database)
Making a User Primary
Before linking, ensure the primary user is marked as a primary user:Use Cases
Link Social Login to Existing Account
User signs up with email/password, later adds Google login:Check Before Linking
Validate accounts can be linked before showing “Link Account” button:Remove Linked Social Login
User wants to remove Google login but keep email/password:Merge Duplicate Accounts
User accidentally created two accounts with different emails:Error Handling
Account Info Conflict
If the recipe user’s email is already associated with another primary user:Already Linked
If the recipe user is already linked to a different primary user:Not a Primary User
If you try to link to a user that isn’t a primary user:Different User Pools
If users are in different databases:Best Practices
- Check before linking: Use the
/link/checkendpoint before showing link UI - Verify ownership: Ensure the user owns both accounts (e.g., verify email)
- Primary user selection: Choose the user with more data/history as primary
- Handle conflicts: Provide clear error messages when linking fails
- Session management: Invalidate old sessions after linking
- User communication: Notify users when accounts are linked
- Audit trail: Log account linking operations for security
Security Considerations
- Verify the user owns both accounts before linking
- Require re-authentication before linking accounts
- Log all account linking operations
- Implement rate limiting on link operations
- Consider requiring MFA for account linking
- Notify users via email when accounts are linked
Related Endpoints
- Get User by ID - View all linked accounts for a user
- Create Primary User - Convert user to primary user
- Delete User - Delete user and optionally all linked accounts