> ## Documentation Index
> Fetch the complete documentation index at: https://docs.highailabs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Authentication Flow

> Complete authentication and account management flows — sign-up, sign-in, session management, signed-out experience, and account deletion.

## Overview

Authentication in High IQ is handled by Clerk and supports Apple Sign-In, Google OAuth, and email/password. This flow documents every authentication state, the signed-out preview experience, and account lifecycle management.

## Authentication State Machine

```mermaid theme={null}
stateDiagram-v2
    [*] --> Anonymous
    Anonymous --> SigningUp: Tap Sign Up
    Anonymous --> SigningIn: Tap Sign In

    SigningUp --> ChoosingMethod: Show Auth Options
    SigningIn --> ChoosingMethod: Show Auth Options

    ChoosingMethod --> AppleAuth: Choose Apple
    ChoosingMethod --> GoogleAuth: Choose Google
    ChoosingMethod --> EmailAuth: Choose Email

    AppleAuth --> Authenticated: Success
    GoogleAuth --> Authenticated: Success
    EmailAuth --> PendingVerification: Account Created
    PendingVerification --> Authenticated: Email Verified

    Authenticated --> Active: First Launch
    Active --> Authenticated: Return Visit

    Authenticated --> Anonymous: Sign Out
    Authenticated --> AccountDeleted: Delete Account
    AccountDeleted --> [*]

    AppleAuth --> ChoosingMethod: Error/Cancel
    GoogleAuth --> ChoosingMethod: Error/Cancel
    EmailAuth --> ChoosingMethod: Error/Cancel
```

## Sign-Up Flows

### Apple Sign-In

```mermaid theme={null}
sequenceDiagram
    participant U as User
    participant App as High IQ
    participant Apple as Apple ID
    participant Clerk as Clerk Auth
    participant DB as Convex

    U->>App: Tap "Continue with Apple"
    App->>Apple: Request authentication
    Apple->>U: Face ID / Touch ID prompt

    alt Share Email
        U->>Apple: Allow sharing email
        Apple-->>Clerk: Name + Email + Token
    else Hide Email
        U->>Apple: Hide email
        Apple-->>Clerk: Name + Relay Email + Token
    end

    Clerk->>Clerk: Create user account
    Clerk-->>App: Auth session token
    App->>DB: Create user record
    DB-->>App: User created
    App->>U: Navigate to Dashboard
```

### Google OAuth

```mermaid theme={null}
sequenceDiagram
    participant U as User
    participant App as High IQ
    participant Google as Google
    participant Clerk as Clerk Auth
    participant DB as Convex

    U->>App: Tap "Continue with Google"
    App->>Google: OAuth consent screen
    Google->>U: Select account + permissions
    U->>Google: Authorize

    Google-->>Clerk: Email + Name + Token
    Clerk->>Clerk: Create user account
    Clerk-->>App: Auth session token

    App->>DB: Create user record
    DB-->>App: User created
    App->>U: Navigate to Dashboard

    Note over App,Google: Gmail access granted separately during Gmail Sync
```

### Email/Password

```mermaid theme={null}
sequenceDiagram
    participant U as User
    participant App as High IQ
    participant Clerk as Clerk Auth
    participant Email as Email Provider
    participant DB as Convex

    U->>App: Tap "Sign up with email"
    U->>App: Enter email + password
    App->>Clerk: Create account

    Clerk->>Email: Send verification email
    Email->>U: Verification link/code

    U->>App: Enter verification code
    App->>Clerk: Verify email
    Clerk-->>App: Account verified

    App->>DB: Create user record
    DB-->>App: User created
    App->>U: Navigate to Dashboard
```

## Signed-Out Preview Experience

```mermaid theme={null}
graph TD
    A[App Opened - Not Signed In] --> B[Preview Dashboard]

    B --> C[Real Layout - Mock Data]
    C --> C1[Mock Stash: 24.5g across 6 strains]
    C --> C2[Mock Stats: Sample metrics]
    C --> C3[Mock Collection: Preview]
    C --> C4[Trending Strains: Real data]
    C --> C5[Blog Articles: Real data]

    C --> D[Locked Overlay on Each Section]
    D --> D1[Glass-morphism blur effect]
    D --> D2[Lock icon]
    D --> D3[Sign In to Unlock label]

    D --> E{User Taps Locked Section}
    E --> F[Auth Modal Appears]
    F --> G[Sign Up or Sign In]

    C4 --> H[Strain Browse: Available Without Auth]
    C5 --> I[Blog: Available Without Auth]
```

## Session Management

```mermaid theme={null}
graph TD
    A[App Launch] --> B{Has Valid Session?}

    B -->|Yes| C[Load Dashboard]
    C --> C1[Clerk token refreshed silently]

    B -->|No| D{Has Stored Session?}
    D -->|Yes| E[Silent Re-auth]
    E --> E1{Success?}
    E1 -->|Yes| C
    E1 -->|No| F[Sign-In Screen]

    D -->|No| G[Preview Dashboard]
    G --> G1[Signed-out experience]

    F --> H{Auth Method}
    H --> C
```

## Account Deletion Flow

```mermaid theme={null}
graph TD
    A[Settings: Delete Account] --> B[Information Screen]
    B --> B1[What will be deleted]
    B --> B2[What cannot be recovered]

    B --> C[Export Data First Option]
    C --> C1{Export?}
    C1 -->|Yes| D[Download Data Package]
    C1 -->|No| E[Continue]

    D --> E

    E --> F[Confirmation Dialog]
    F --> F1[Type CONFIRM to proceed]
    F --> F2{Confirmed?}

    F2 -->|Yes| G[Delete Process]
    G --> G1[Delete Convex user data]
    G --> G2[Delete stash, orders, favorites]
    G --> G3[Delete notebooks, stats]
    G --> G4[Revoke Clerk session]
    G --> G5[Revoke Google tokens if connected]

    G5 --> H[Account Removed]
    H --> I[Redirect to Sign-Up Screen]

    F2 -->|No| J[Cancel - Return to Settings]
```

## Edge Cases

| Scenario                                | Behavior                                        |
| --------------------------------------- | ----------------------------------------------- |
| Apple hides email address               | Relay email works normally with Clerk           |
| Google account already linked           | Silent sign-in, no duplicate account            |
| Email already in use (different method) | "Account exists" with correct method shown      |
| Network failure during sign-up          | Error with retry, no partial account created    |
| Email verification link expired         | Resend option after 60 seconds                  |
| Force quit during authentication        | Resume at auth screen on next launch            |
| Sign out with unsaved data              | All data already synced to Convex, safe         |
| Delete account then re-signup           | Fresh account, no data recovery                 |
| Multiple devices signed in              | Session synced across all via Clerk             |
| Token refresh failure                   | Silent retry, then sign-in prompt if persistent |

## Related Flows

<CardGroup cols={2}>
  <Card title="Onboarding" icon="right-to-bracket" href="/help/user-flows/onboarding">
    What happens after authentication.
  </Card>

  <Card title="User Lifecycle" icon="arrows-spin" href="/help/user-flows/lifecycle">
    Authentication as the first lifecycle transition.
  </Card>

  <Card title="Master Journey" icon="route" href="/help/user-flows/master-journey">
    Auth in the context of the complete journey.
  </Card>

  <Card title="Gmail Sync" icon="envelope" href="/help/features/gmail-sync">
    Additional Google permissions for Gmail access.
  </Card>
</CardGroup>
