# Social authentication setup

Guide to enable Google, Apple, and Facebook login in your project.

---

## Google Sign-In

The CLI handles the technical setup automatically when generating the project:
- Reads the `REVERSED_CLIENT_ID` from `GoogleService-Info.plist`
- Registers the URL scheme in `ios/Runner/Info.plist`
- Writes `lib/google_auth_options.dart` with the Web Client ID

**What you need to verify once:**

1. [Firebase Console → Authentication → Sign-in method → Google](https://console.firebase.google.com/project/_/authentication/providers) → enable
2. The CLI automatically adds the debug keystore SHA-1 for Android. If you use a new computer or a different keystore, verify with:
```bash
kasy doctor
```

---

## Apple Sign-In

Requires an [Apple Developer](https://developer.apple.com) account (paid).

### Step 1 — Enable capability on Bundle ID

1. Open [Identifiers](https://developer.apple.com/account/resources/identifiers/list)
2. Select your Bundle ID
3. Enable **Sign In with Apple** → Enable as a primary App ID → **Save**

### Step 2 — Create a key

1. Open [Keys](https://developer.apple.com/account/resources/authkeys/list)
2. Click **+** → enter a name (e.g. `Firebase Sign In with Apple`)
3. Enable **Sign In with Apple** → Configure → select your Bundle ID → Save
4. Register → **download the `.p8` file** (can only be downloaded once — store it safely)
5. Note the **Key ID** (e.g. `6RR89XG535`)

### Step 3 — Create a Services ID

1. Open [Services IDs](https://developer.apple.com/account/resources/identifiers/list/serviceId)
2. Click **+** → select **Services IDs** → Continue
3. Fill in:
   - **Description**: `Firebase Sign In with Apple`
   - **Identifier**: `YOUR_BUNDLE_ID.signin` (e.g. `com.company.app.signin`)
4. Register → click the new Services ID → enable **Sign In with Apple** → Configure
5. Fill in:
   - **Primary App ID**: your Bundle ID
   - **Domains**: `YOUR_PROJECT.firebaseapp.com`
   - **Return URLs**: `https://YOUR_PROJECT.firebaseapp.com/__/auth/handler`
6. Next → Done → Continue → **Save**

### Step 4 — Configure in Firebase

1. Open [Firebase Console → Authentication → Apple](https://console.firebase.google.com/project/_/authentication/providers)
2. Enable the Apple provider
3. Fill in under **OAuth code flow configuration**:
   - **Services ID**: the identifier from Step 3 (e.g. `com.company.app.signin`)
   - **Team ID**: found at [Membership Details](https://developer.apple.com/account#MembershipDetailsCard)
   - **Key ID**: the Key ID from Step 2
   - **Private key**: full content of the `.p8` file, including the `-----BEGIN PRIVATE KEY-----` and `-----END PRIVATE KEY-----` lines
4. **Save**

### Step 5 — Enable capability in Xcode

1. Open `ios/Runner.xcworkspace` in Xcode
2. Target **Runner** → **Signing & Capabilities** → **+ Capability** → add **Sign In with Apple**

> **Android and Web**: no additional setup needed. The Firebase SDK handles the flow on Android. For Web, the Services ID (Step 3) already covers the redirect flow via `firebaseapp.com`.

---

## Facebook Sign-In

Requires a [Meta for Developers](https://developers.facebook.com) account.

### Step 1 — Create an app on Meta

1. Open [Meta for Developers → My Apps](https://developers.facebook.com/apps)
2. Click **Create App** → select **Consumer** → Next
3. Enter the app name → Create App
4. In the app dashboard, note the **App ID** and **Client Token** (Settings → Advanced → Client Token)

### Step 2 — Enable Facebook Login

1. In the Meta app dashboard → Add Product → **Facebook Login** → Set Up → iOS/Android as needed
2. iOS: provide your app's Bundle ID

### Step 3 — iOS: update Info.plist

Edit `ios/Runner/Info.plist` and replace the placeholders:

```xml
<key>FacebookAppID</key>
<string>YOUR_APP_ID</string>
<key>FacebookClientToken</key>
<string>YOUR_CLIENT_TOKEN</string>
<key>FacebookDisplayName</key>
<string>Your app name</string>
```

And the URL scheme (inside `CFBundleURLTypes`):
```xml
<string>fbYOUR_APP_ID</string>
```

### Step 4 — Android: update strings.xml

Edit `android/app/src/main/res/values/strings.xml` and replace the placeholders:

```xml
<string name="facebook_app_id">YOUR_APP_ID</string>
<string name="facebook_client_token">YOUR_CLIENT_TOKEN</string>
```

### Step 5 — Web: add domain in Meta

1. In the Meta app dashboard → Facebook Login → Settings
2. Under **Valid OAuth Redirect URIs**, add `https://YOUR_PROJECT.firebaseapp.com/__/auth/handler`
3. Under **Allowed Domains for the JavaScript SDK**, add `YOUR_PROJECT.firebaseapp.com`

Verify with:
```bash
kasy doctor
```

---

## Supabase

For projects using the Supabase backend, the Apple and Meta setup is identical. What changes is where you register the credentials:

| Provider | Where to configure |
|----------|--------------------|
| Google | Supabase Dashboard → Auth → Providers → Google |
| Apple | Supabase Dashboard → Auth → Providers → Apple (Services ID required) |
| Facebook | Supabase Dashboard → Auth → Providers → Facebook |

For Apple with Supabase, the Services ID **Return URL** must be:
```
https://YOUR_PROJECT.supabase.co/auth/v1/callback
```
