> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# LynxAppleSignIn

Signs the person in with Sign in with Apple's own sheet on iOS, and gives you the identity token and authorization code your server checks with Apple. `import { LynxAppleSignIn, AppleSignInError } from '@blotch/angular-lynx';`

## Turn it on

Add `signInWithApple` to the `build` target's options in angular.json, with your team's `ios.teamId`:

```json title="angular.json"
"options": {
  "signInWithApple": true,
  "ios": { "teamId": "ABCDE12345" }
}
```

What it adds:

- **iOS:** the `com.apple.developer.applesignin` [entitlement](/guide/glossary.md#entitlement), the native code behind the sheet, and iOS's own [`<apple-sign-in-button>`](/api/elements/apple-sign-in-button.md). Xcode turns on Sign in with Apple for the [App ID](/guide/glossary.md#app-id) when it makes the [provisioning profile](/guide/glossary.md#provisioning-profile).
- **Android:** nothing. The sheet is iOS's alone.

Without it, the entitlement and the native code are left out. `isAvailable()` resolves `false`, and `signIn()` rejects with `unsupported`.

:::warning
**Signing the entitlement needs a team in the paid Apple Developer Program.** A free [Personal Team](/guide/glossary.md#personal-team) can't sign it, and the build says so. The Simulator app needs no team.
:::

See [Turn on device features](/guide/native-apps/device-features.md) for every setting.

## Usage

```typescript
import { Component, inject, signal } from '@angular/core';
import {
  AppleSignInError,
  LYNX_ELEMENTS,
  LynxAppleSignIn,
} from '@blotch/angular-lynx';
import { Auth } from './auth';

@Component({
  selector: 'app-sign-in',
  imports: [LYNX_ELEMENTS],
  template: `
    @if (appleAvailable()) {
      <apple-sign-in-button class="h-12 w-full" (bindtap)="signInWithApple()" />
    }
  `,
})
export class SignIn {
  readonly #appleSignIn = inject(LynxAppleSignIn);
  readonly #auth = inject(Auth);
  readonly appleAvailable = signal(false);

  constructor() {
    void this.#appleSignIn
      .isAvailable()
      .then((available) => this.appleAvailable.set(available));
  }

  async signInWithApple() {
    try {
      const result = await this.#appleSignIn.signIn({
        scopes: ['email', 'fullName'],
      });
      // The server verifies the token, and keeps the email and name: Apple
      // sends them on the first sign-in only.
      await this.#auth.signInWithApple(result);
    } catch (error) {
      if (error instanceof AppleSignInError && error.code === 'canceled') {
        return;
      }
      throw error;
    }
  }
}
```

`Auth` stands for the app's own service, which sends the result to your server.

**Use [`isAvailable()`](#isavailable) to choose a sign-in.** In the Android app, sign in with Apple's web flow through [`LynxWebAuthSession`](/api/angular-lynx/accounts/web-auth-session.md) instead. The [web preview](/guide/glossary.md#web-preview) and [Lynx Explorer](/guide/glossary.md#lynx-explorer) have neither.

**Start it from [`<apple-sign-in-button>`](/api/elements/apple-sign-in-button.md)**, which App Review checks against Apple's [Sign in with Apple guidelines](https://developer.apple.com/design/human-interface-guidelines/sign-in-with-apple).

- In the iOS app it's iOS's own button, with the logo, the title in the phone's language, and Apple's colors and corners.
- Elsewhere the runtime draws the same button.
- Keep it at least as prominent as the other logins.

### Check the result on your server

The app can't trust what it gets back by itself. Send `identityToken` and `authorizationCode` to your server, which:

1. **Verifies `identityToken`**, a JWT, against Apple's public keys (`https://appleid.apple.com/auth/keys`). It checks that:

   - its `iss` is `https://appleid.apple.com`
   - its `aud` is your app ID
   - its `nonce` is the hash of yours, if you sent one

   The token's `sub` is the person's ID. Key the account on it, never on a `user` the app sends alongside, which anyone can change.

2. **Exchanges `authorizationCode`** at `https://appleid.apple.com/auth/token` for a refresh token. Keep it: revoking it is how the server deletes the person's Apple sign-in when they delete their account. Guideline 5.1.1(v) requires that.

**Services such as Firebase Authentication and Supabase do step 1.** Pass them the token, with the raw nonce if you sent one.

- **Their user has its own ID,** which they link to the token's `sub`. Key the account on theirs.
- **They don't keep Apple's refresh token,** so plan step 2 yourself. Firebase, for one, revokes with `revokeToken(withAuthorizationCode:)`, which needs a fresh code from a new sign-in. Check your service's account deletion guide.

### Use a nonce

A **nonce** ties the identity token to one sign-in, so a token someone copies can't be replayed:

1. Make a random string.
2. Pass its SHA-256 as `nonce`.
3. Send the string itself to the server. It checks that the token's `nonce` claim is the string's hash.

That's the form Firebase and Supabase take.

### Check the sign-in at launch

The person can stop using their Apple ID with your app in Settings. Apple asks apps to check at launch, and sign the person out when it's `revoked`.

The check takes Apple's `user` from the sign-in. So keep it on the device, even when your account uses another ID, such as a Firebase UID:

```typescript
const state = await appleSignIn.getCredentialState(appleUser);
if (state === 'revoked' || state === 'notFound') {
  await auth.signOut();
}
```

## When App Review asks for it

**App Review usually asks for it beside other social logins.** Under Apple's guideline 4.8, an app whose main login is Google, Facebook or another third-party service must also offer a login that:

- limits what it collects to a name and email
- lets the person hide their email
- doesn't track them for ads

Sign in with Apple meets that. The guideline has exceptions, such as an app that signs in only with its own accounts, or with a company's.

The native sheet is also the one people expect on iOS: Face ID, then done, with no web page.

## Platforms

| Platform                                          | `signIn()`                                                                                                                          |
| ------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
| iOS, with `signInWithApple`                       | Apple's sheet (`ASAuthorizationAppleIDProvider`), over the app                                                                      |
| iOS, without `signInWithApple`                    | Rejects with `unsupported`                                                                                                          |
| Android                                           | Rejects with `unsupported`. Use Apple's web flow with [`LynxWebAuthSession`](/guide/web-sign-in.md) and a Services ID               |
| [Web preview](/guide/glossary.md#web-preview)     | Rejects with `unsupported`                                                                                                          |
| Unit tests with the testing library               | What [`setAppleSignInResult()`](/api/testing-library/fakes/accounts.md#setapplesigninresult) says, and `canceled` until a spec says |
| [Lynx Explorer](/guide/glossary.md#lynx-explorer) | Rejects with `unsupported`                                                                                                          |

On a phone or Simulator signed in to no Apple Account, iOS offers Settings instead of the sheet. Closing that rejects with `failed`.

## API

### `LynxAppleSignIn`

```typescript
@Injectable({ providedIn: 'root' })
class LynxAppleSignIn {
  isAvailable(): Promise<boolean>;
  signIn(options?: AppleSignInOptions): Promise<AppleSignInResult>;
  getCredentialState(user: string): Promise<AppleCredentialState>;
}
```

### `isAvailable()`

Resolves `true` where `signIn()` opens the native sheet: an iOS app built with `signInWithApple`. `false` everywhere else. It never rejects.

### `signIn()`

Opens the sheet and resolves with the credential once the person signs in. Rejects with an [`AppleSignInError`](#applesigninerror).

```typescript
type AppleSignInOptions = {
  /** What to ask the person to share. None by default. */
  scopes?: readonly ('email' | 'fullName')[];
  /** Put in the identity token's `nonce` claim as given. */
  nonce?: string;
  /** Returned as the result's `state`, unchanged. */
  state?: string;
};

type AppleSignInResult = {
  /** The person's ID for your team: the same on every sign-in and device. */
  user: string;
  /** A JWT signed by Apple, for your server to verify. */
  identityToken: string;
  /** A short-lived code your server exchanges for a refresh token. */
  authorizationCode: string;
  /** The first sign-in with the `email` scope only. Can be a private relay address. */
  email?: string;
  /** The first sign-in with the `fullName` scope only, with the parts the person shared. */
  fullName?: AppleSignInFullName;
  state?: string;
  /** Whether Apple thinks a real person signed in. */
  realUserStatus: 'likelyReal' | 'unknown' | 'unsupported';
};

type AppleSignInFullName = {
  givenName?: string;
  familyName?: string;
  middleName?: string;
  nickname?: string;
  namePrefix?: string;
  nameSuffix?: string;
};
```

**Apple shares the email and name once**, on the person's first sign-in to your app. Later sign-ins return `user` and the tokens alone, so save them the first time. Key the account on this ID, not the email, which the person can hide behind a relay address, and change. Your server reads the ID from the verified token's `sub`, which equals `user`.

### `getCredentialState()`

Asks Apple whether `user`, from an earlier sign-in, is still signed in. Rejects with an `AppleSignInError`: `unsupported` where `signIn()` would.

```typescript
type AppleCredentialState =
  | 'authorized' // Still signed in
  | 'revoked' // The person stopped using their Apple ID with your app
  | 'notFound' // No sign-in for it on this device
  | 'transferred'; // The app moved to another developer team
```

### `AppleSignInError`

```typescript
class AppleSignInError extends Error {
  readonly code: 'canceled' | 'unsupported' | 'failed';
}
```

| `code`        | Why                                                                                                                  |
| ------------- | -------------------------------------------------------------------------------------------------------------------- |
| `canceled`    | The person closed the sheet                                                                                          |
| `unsupported` | The host has no native sheet: Android, the web preview, Lynx Explorer, or an iOS app built without `signInWithApple` |
| `failed`      | Apple couldn't sign the person in, a sign-in was already open, or Apple returned no token                            |

A `failed` error's message ends with Apple's error code. Code `1000` also comes from a phone signed in to no Apple Account, and from an app signed without the entitlement.

## Testing

The testing library has a fake of the native module. Until a spec says how the person signs in, they close the sheet, and `signIn()` rejects with `canceled`:

```typescript
import {
  render,
  setAppleSignInResult,
  fireEvent,
  waitForUpdate,
} from '@blotch/angular-lynx-testing-library';

setAppleSignInResult({ user: 'u1', email: 'ada@example.com' });
await render(SignIn);
// iOS's button, as the fake module stands for an iOS app with signInWithApple.
fireEvent.tap(document.querySelector('apple-sign-in-button')!);
await waitForUpdate();
```

See [`setAppleSignInResult()`](/api/testing-library/fakes/accounts.md#setapplesigninresult) for the rest: failing a sign-in, taking the sheet away as on Android, and a user's credential state.

## Related

- [`<apple-sign-in-button>`](/api/elements/apple-sign-in-button.md): the button that starts it
- [Sign in with a web page](/guide/web-sign-in.md): Apple's web flow, for Android
- [Turn on device features](/guide/native-apps/device-features.md): `signInWithApple` and the other opt-in settings
