Skip to main content

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 Associated
Recipe User Already Linked
Not a Primary User

Example

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: true if the recipe user was deleted after unlinking
  • wasLinked: true if the user was linked before this operation, false if 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

  1. The primaryUserId must belong to a primary user
  2. The recipeUserId must not already be linked to another primary user
  3. The account info (email/phone) must not already be associated with another primary user
  4. 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

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:
Solution: Convert the user to a primary user first.

Different User Pools

If users are in different databases:

Best Practices

  1. Check before linking: Use the /link/check endpoint before showing link UI
  2. Verify ownership: Ensure the user owns both accounts (e.g., verify email)
  3. Primary user selection: Choose the user with more data/history as primary
  4. Handle conflicts: Provide clear error messages when linking fails
  5. Session management: Invalidate old sessions after linking
  6. User communication: Notify users when accounts are linked
  7. 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