Email Linking System

Overview

The email linking system allows users to authenticate with multiple email addresses that all resolve to the same account. This is particularly useful for developers with both work (@aethex.dev) and personal email addresses.

Architecture

Database Tables

Links multiple email addresses to a single user account.

- id: UUID (primary key)
- user_id: UUID (FK to user_profiles.user_id)
- email: TEXT (unique)
- is_primary: BOOLEAN (marks the main email)
- verified_at: TIMESTAMP
- linked_at: TIMESTAMP
- created_at: TIMESTAMP
- updated_at: TIMESTAMP

user_profiles (additions)

User Flows

Flow 1: Login with Linked Email

API Endpoints

POST /api/user/resolve-linked-email

Resolve a linked email to its primary email address.

Request:

Response (linked email):

Response (non-linked email):

POST /api/user/link-email

Link two existing user accounts by merging one into the other.

Request:

Response:

POST /api/user/link-mrpiglr-accounts (Admin Only)

Special endpoint to link the mrpiglr accounts.

Request:

Response:

Implementation Details

Authentication Resolution

When a user tries to sign in with a linked email:

  1. First attempt: Try with provided email (normal auth flow)

  2. If it fails with "invalid credentials":

    • Check /api/user/resolve-linked-email

    • If email is linked, get primary email

    • Retry with primary email

    • If successful, user is logged in

This is transparent to the user - they can log in with any linked email.

Data Transfer on Merge

When two accounts are merged:

  1. Achievements: Transferred to primary account

  2. Creator Profile: Transferred or preserved if not duplicate

  3. Applications: Transferred to primary account

  4. Discord Links: Transferred (avoiding duplicates)

  5. Web3 Wallets: Transferred (avoiding duplicates)

  6. Email Links: Both emails added to user_email_links table

  7. Profile: Source profile marked as merged_to_user_id

For Developers (Dev Accounts)

Developer accounts with @aethex.dev email:

  • is_dev_account = true

  • primary_email = "@aethex.dev email"

  • Public profile shows work email

  • Can link personal email for authentication convenience

Usage Examples

Check Email Resolution

Database Migration

Applied via: code/supabase/migrations/20250206_add_email_linking.sql

Includes:

  • user_email_links table with RLS policies

  • get_primary_user_by_email() function

  • Columns on user_profiles for dev accounts and primary email

  • Proper indexes for performance

Future Enhancements

  1. Email Verification: Add verification flow before linking new emails

  2. Settings UI: Add "Linked Emails" section in user settings

  3. Email Change Request: Allow users to change primary email

  4. Admin Dashboard: View all linked emails for a user

  5. Notification: Email both addresses when a new email is linked

Last updated