# Provide sign-in

The auth role splits sign-in between its provider and its template. A provider keeps the accounts of the users and knows who is signed in on the device, such as a backend with authentication. The template of the role owns what is the same with every provider: the session of the app, the mode, the list of the ways to sign in, and what happens to an anonymous user who gets an account. [Sign-in](https://doc.saymyframe.com/guides/sign-in.md) describes what the app has.

## The service

The template generates `lib/core/auth/auth_service.dart` with what a provider implements and what its calls fail with:

| In the file | What it is |
| --- | --- |
| `AuthService` | The interface that a provider implements. |
| `AuthUser` | A user as the provider knows them: a `uid`, which stays the same for as long as the user exists, `isAnonymous`, an `email` or `null`, and the `issuers` of the identities of the user at identity providers. |
| `SignInCredential` | The credential of a way to sign in, a sealed class. `IdTokenCredential` is the one so far: the `issuer`, the `idToken`, and the `rawNonce`, the `authorizationCode` and the name of the user, when the identity provider gives them. |
| `AuthFailure` | The failure of a call, the only thing that such a call throws: a `reason` and, for the developer of the app, a `developerHint` or `null`. |
| `AuthFailureReason` | Why a call failed. |

This is the interface, without its comments:

```dart title="lib/core/auth/auth_service.dart"
abstract interface class AuthService {
  AuthUser? get currentUser;

  Stream<AuthUser?> get userChanges;

  Future<void> signIn({required String email, required String password});

  Future<void> signUp({required String email, required String password});

  Future<void> linkPassword({required String email, required String password});

  Future<void> signInWith(SignInCredential credential);

  Future<void> linkWith(SignInCredential credential);

  Future<void> signInAnonymously();

  Future<void> sendPasswordReset(String email);

  Future<void> signOut();

  Future<void> deleteAccount({SignInCredential? fresh});
}
```

The code of the app does not call it. It signs in through `appSession`, which calls this service.

## The module

A provider contributes its implementation as data of the role, an `AuthImplementation`, much as the providers of the other [service roles](https://doc.saymyframe.com/extending/role-provider.md#a-service-events-analytics-crash-reporting) contribute a `RoleImplementation`. The data of the auth role is an `AuthData`: the `AuthImplementation` of the provider, or a `SignInMethod` of a module with a [way to sign in](#ways-to-sign-in). This is the built-in [`firebase_auth`](https://doc.saymyframe.com/modules/firebase-auth.md):

```dart
  @override
  ModuleDescriptor get descriptor => const ModuleDescriptor(
        id: id,
        description: 'Firebase Authentication with firebase_auth',
        kind: ModuleKinds.infrastructure,
        dependsOn: {FirebaseCoreModule.id},
        uses: {localizationRole},
        providers: [_FirebaseAuthProvider()],
      );

  @override
  List<Contribution> contribute(ModuleContext context) => [
        BrickContribution(
          firebaseAuthBundle,
          vars: {
            'email_language': const RoleVar(
              localizationRole,
              present: _emailLanguage,
              absent: '',
            ),
            // For the script that enables the ways to sign in: the file
            // that it reads the mode of the app from, how the auth role
            // says to read it, the mode with anonymous users, and the
            // Firebase CLI that it asks for.
            'session_file': AuthRole.sessionFile,
            'mode_declaration': modeDeclarationCode,
            'anonymous_mode': AuthMode.anonymous.name,
            'minimum_firebase_cli': firstFirebaseCliWithSignIn,
          },
        ),
        const PubspecContribution.hosted('firebase_auth', '^6.7.0'),
        authRole.data(
          const AuthImplementation(
            type: TypeRef('FirebaseAuthService', import: _file),
            create: FactoryRef('createFirebaseAuthService', import: _file),
          ),
        ),
        const Preflight([firebaseCliWithSignIn]),
        enableSignIn,
        AppEntryRole.agentSections.entry(
          authRole.description,
          AgentNote(agentNote),
        ),
      ];
```

- A provider contributes one implementation. Its function is in the file of the implementation and takes no arguments. An implementation that is created asynchronously uses `AuthImplementation.async`, and `initAuth()` of the role awaits it.
- What the service keeps to is the same in every mode of the app. A provider that renders something by the mode reads the mode in its render hook, with `authRole.modeIn(input)`. `firebase_auth` has a provider of its own, `_FirebaseAuthProvider`, for the ways to sign in of the app: its `validate` hook refuses a way to sign in that it cannot take, and its render hook writes the README section of the module and gives the script of the app the ways to sign in that it sets up. A tool of the app that follows the mode reads it from the app; see [a tool that reads the mode](#a-tool-that-reads-the-mode).
- The brick of `firebase_auth` reads `email_language`, a [variable that depends on a role](https://doc.saymyframe.com/extending/templates.md#variables-that-depend-on-a-role). In an app with the localization role it is the code that gives Firebase the language of the app, and elsewhere it is empty.
- The `Preflight` and `enableSignIn` are the check and the step with which the module enables the sign-in methods in the Firebase project after generation; see [a step with a notice](https://doc.saymyframe.com/extending/infrastructure-module.md#a-step-with-a-notice).
- A provider tells in a README section of its own what its side needs for a mode, such as how anonymous users are enabled. The role writes the section "Sign-in", whose heading is `AuthRole.readmeHeading`, and the section of a provider can refer to it.
- The note of the provider goes under `authRole.description` in the guide for coding agents, after the note of the role; see [Notes for coding agents](https://doc.saymyframe.com/extending/infrastructure-module.md#notes-for-coding-agents).

The role uses the router role and the localization role, and a provider uses what its role uses. So the hooks of a provider may read the routes and the guards of the app, and the contract harness builds the apps of a provider with each router of its registry and without one, and with the localization role and without it. Put a router and a provider of the localization into the registry of the tests of a provider.

## Ways to sign in

A module that requires the role, or its provider, may give it a `SignInMethod`: a way to sign in with an identity provider, such as Google. It has the label and the icon of its button, the platforms that it works on, the function of the module that asks the identity provider for a credential, the issuer of the tokens of the identity provider, whether a deletion asks it again (`revokesOnDeletion`), what it is for the App Store, and what it `needs` of the backend, such as the SHA of the keys of the Android app. The template lists the ways to sign in of the app in `appSignInMethods`, and a provider reads them in its hooks with `authRole.methodsIn(input)`. [`google_sign_in`](https://doc.saymyframe.com/modules/google-sign-in.md) and [`apple_sign_in`](https://doc.saymyframe.com/modules/apple-sign-in.md) give such ways.

A provider takes the credentials of the ways to sign in that its backend takes:

- It meets what each way to sign in `needs` of its backend, such as with a script of the app, or tells in its section of the README how to.
- What no command can set up, it lists as [steps by hand](https://doc.saymyframe.com/extending/infrastructure-module.md#steps-that-depend-on-the-data-of-a-role) from its `manualSteps` hook, as `firebase_auth` does for Apple and for the SHA of the keys of a release.
- It reports in its `validate` hook a way to sign in whose identity provider its backend does not take, with the module of the way to sign in as the origin, so that `smf create` leaves that module out.
- Its service fails a credential that it does not take with `notConfigured`, with the issuer in the hint.

The module rule `auth.methods` checks the ways to sign in of a module: only a module that requires or provides the role gives one, with a label, platforms, an issuer of an https URL, an icon and a function in files that the bricks of the module generate, and each issuer once. The structural rule `auth.method_functions` checks that the function is a top-level function of the files of the module that takes no arguments, returns a `Future` of a credential that may be `null`, and whose file does not use `appSession`. `auth.method_icons` checks that the icon is a class of the files of the module with a `const` unnamed constructor that takes no arguments.

## A tool that reads the mode

A provider whose side has to be set up for the mode may give the app a tool for that, a script that `dart` runs. `firebase_auth` generates one, `tool/enable_firebase_sign_in.dart`, which enables anonymous users in the Firebase project only in the mode `anonymous`.

Such a tool cannot take the mode from the render hook of the provider: that is the mode that the app was generated in, and it is stale from the first time the developer of the app changes `authMode`. It cannot import the file of the session either, which imports Flutter. So it reads the declaration from the text of that file, and the role says how:

| | |
| --- | --- |
| `AuthRole.modeDeclaration` | The source of a regular expression for a search over the lines of the file, as `RegExp(AuthRole.modeDeclaration, multiLine: true)`. Each match is a comment of the file, or a declaration of `authMode` that starts its line and has a mode as its value. Only a declaration has the first group, the name of its mode. |
| `authRole.modeWrittenIn(source)` | The mode that the text of the file declares, or `null` when the text does not tell it: it has no declaration that the expression takes, or more than one. |

The file tells the mode when exactly one match has the first group. A tool stops otherwise, and does not guess.

- The expression takes the declaration as the template writes it, `const AuthMode authMode = AuthMode.required;`, and as a developer may leave it: without the type, with `final`, with its value on the next line, and with a comment after it.
- It takes no value that is not the name of an `AuthMode`, no computed value, no declaration within a comment, and none that does not start its line.
- The comment of `authMode` in the app tells the developer to keep the declaration in that form.

A script of the app has only the libraries of Dart, so the expression goes into it as code. `firebase_auth` gives its brick the expression as a variable, and the files of the module stay the same in every mode.

## What the role does itself

The template of the role does the rest, the same with every provider:

- It generates `lib/core/auth/app_session.dart` with `appSession`, the session of the app, the constant `authMode`, and `initAuth()`, which creates the service with the function of the provider and starts the session with it. `bootstrap()` awaits `initAuth()` in its platform phase, so who is signed in is known before the first frame.
- The session runs its calls one after another and turns every error that is no `AuthFailure` into one, with the reason `unknown` and the error as its `developerHint`.
- For an anonymous user, the session calls `linkPassword` in place of `signUp`, so that the user keeps the id. Before an anonymous user signs in to an account that exists already, it takes the data of the guest, and it gives that data to the account afterwards, as `lib/core/auth/guest_data.dart` of the app says.
- `appSession.signInWith` gives an anonymous user the identity of a credential with `linkWith`, so the user keeps the id. When that fails with `credentialInUse` or `accountExistsWithOtherMethod`, it signs in to the account that has the identity with `signInWith` and the identical credential object, moving the data of the guest. A user of an account, or someone signed out, signs in with `signInWith` at once.
- For an account with a way to sign in that `revokesOnDeletion` and works on the platform, `appSession.deleteAccount()` calls the function of that way to sign in itself and gives the credential to `deleteAccount(fresh:)`. When the function returns `null`, the call deletes nothing.
- It renders `appSignInMethods` and warns, as `auth.no_equivalent_sign_in`, of an iOS app with a login service of a third party and no way to sign in that the App Store takes as its equivalent.
- In the mode `anonymous`, the session calls `signInAnonymously` when the app starts without a user, waits for it for at most 3 seconds, and tries again each time the user comes back to the app and after each call that leaves nobody signed in.
- It asks for the mode, writes it into the app, and writes the section "Sign-in" of the README of the app and the note of the role in the guide for coding agents.
- It publishes `AuthRole.account`, the [condition](https://doc.saymyframe.com/concepts/conditions.md) of a route whose screen is only for the user of an account, and warns when routes ask for it and no guard of the app stands for it.

So a provider keeps no session of its own and declares no guard. A widget of a provider signs in through `appSession` too.

## What every provider keeps to

The contract of `AuthService` is in the comments of `lib/core/auth/auth_service.dart`. It is the same in every mode of the app, whatever the provider renders by the mode:

- The function of the implementation returns a service whose `currentUser` is the user of the device already, without a request to the server. It may be called again, and the service that it returns then has the user who is signed in now. A test runs `initAuth()` again to see what the next launch of the app finds.
- When a call completes, `currentUser` is its result. `userChanges`, a broadcast stream, tells of every change of `currentUser`, also of one that no call made, and when it tells of a user, `currentUser` is that user. An error that the stream sends changes nothing: the session reports it with `FlutterError.reportError` and goes on listening.
- Every failure is an `AuthFailure`, and every call ends. When the server does not answer, the call fails with the reason `network`.
- `signIn` is also called while a user is signed in, an anonymous one or the user of another account, and `signUp` while the user of another account is. The user of the call then replaces that user on the device.
- A wrong password and an address without an account are both `invalidCredentials`, and `sendPasswordReset` completes whether the address has an account or not. So nobody learns from the app which addresses have an account.
- `signUp` and `linkPassword` with an address that has an account fail with `emailInUse` and change nothing.
- A call that fails leaves the user who is signed in as that user was. An anonymous user stays signed in when `linkPassword` fails, and so does whoever is signed in when `signIn` or `signUp` fails.
- `signInAnonymously` creates a user with an id of its own, and `linkPassword` and `linkWith` keep the id of the user.
- `linkWith` of an identity that another user has fails with `credentialInUse`, and of one whose email address has an account of another user with `accountExistsWithOtherMethod`, and changes nothing. It keeps what its backend answered for that credential object, such as a credential to sign in with, without holding the token longer than needed. The session passes the identical object to `signInWith`, which uses that answer, or the credential itself when the backend gave nothing, since an identity provider may take a credential only once. `firebase_auth` keeps the answer of Firebase in an `Expando` keyed by the credential object.
- A credential whose identity provider the provider does not take fails with `notConfigured`, with the issuer in the hint.
- `deleteAccount(fresh:)` signs the user in again with the credential first, and revokes the tokens of the identity provider where it can. A credential of another user fails with `invalidCredentials`, and a revocation that fails with `notConfigured`, and neither deletes anything.
- `currentUser` lists the `issuers` of the identities of the user.
- `deleteAccount` deletes the user who is signed in, an anonymous one too, and signs out. It succeeds for a user who has just signed in, and for an anonymous user who was just created. For a user who signed in longer ago, such as one whom the service found on the device, it may fail with `recentSignInRequired`, and it succeeds once the user has signed in again. The role has no call that asks for the password again.
- `linkPassword` and `deleteAccount` are calls for the user who is signed in. Made when nobody is signed in, such a call fails with `recentSignInRequired`. Made for a user whose session has ended on the server, it signs that user out on the device first and then fails with the same reason. That is the one case in which a call that fails changes who is signed in.

| Reason | When a call fails with it |
| --- | --- |
| `invalidCredentials` | The email address and the password match no account. |
| `emailInUse` | The email address has an account already. |
| `credentialInUse` | The identity of a credential belongs to another user, in `linkWith`. |
| `accountExistsWithOtherMethod` | The email address of the identity of a credential has an account of another user. |
| `weakPassword` | The provider takes no password as weak as this one. |
| `invalidEmail` | The text is no email address. |
| `userDisabled` | The account is disabled. |
| `tooManyAttempts` | The provider takes no more attempts for a while. |
| `network` | The device did not reach the provider. |
| `recentSignInRequired` | The call needs a user who signed in a short while ago, or the session of the user has ended on the server. |
| `notConfigured` | Sign-in is not set up at the provider, such as a way to sign in that is not enabled there. The `developerHint` explains the mistake in the setup. |
| `unknown` | Anything else. The `developerHint` has the error. |

A package may promise less than the role, and the implementation then makes up the difference. The firebase_auth package keeps a deleted user as its current one until the platform tells it of the sign-out, so `FirebaseAuthService` signs out after it deleted a user. [`firebase_auth`](https://doc.saymyframe.com/modules/firebase-auth.md#what-the-service-adds-to-the-package) lists what else it does.

## Rules

The structural rules of the role read what each Dart file of an app imports and calls:

| Rule | What it requires |
| --- | --- |
| `auth.factory_calls` | No file of a module, and none of the template of another role, calls `createAuthService()`. Of such files, only those of the implementation of the provider, the files of its class and of its function, import or export the file of `AuthService`. |
| `auth.start_calls` | Only the file of `bootstrap()` calls `initAuth()`, and no file of a module or of the template of another role calls `appSession.start()`. |
| `auth.implementation_factories` | The function of the implementation is in its file and takes no arguments. |
| `auth.method_functions`, `auth.method_icons` | The function and the icon of every way to sign in; see [ways to sign in](#ways-to-sign-in). |

The module rule `services.implementations`, which the role shares with the other service roles, checks that a provider, and only a provider, contributes one implementation.

The rules do not see a `part` file of a library that imports the file of the session, a file that reaches that file through an export of another file of the app, or `appSession` under another name, such as a variable that it was assigned to.

## Tests

The contract harness checks the rules above, and builds the apps of a provider in each mode of the role; see [Test a module](https://doc.saymyframe.com/extending/testing.md#the-modes-of-a-role). Only a running app shows whether a provider keeps to the contract of the service.

SMF's CI checks that in running apps, in every mode, with every provider of the role that SMF has. The test of the role calls the service of the provider through `createAuthService()`, takes `appSession` through a sign-up, a sign-out, a sign-in and the deletion of the account, and runs `initAuth()` again to see what the next start finds. CI runs the same test on two providers with a known bug, and it must fail on each: one that returns its service before it has read who is signed in on the device, and one that gives an anonymous user a new id in `linkPassword()`.

In an app with ways to sign in, a second test, `auth_methods`, checks the credentials: what `signInWith`, `linkWith` and `deleteAccount(fresh:)` of the service do with a credential of each issuer of the app, that `userChanges` tells of each change that they make, and what the session does with the function of each way to sign in. It must fail on a third provider with a known bug, whose `linkWith` gives an anonymous user a new id. [Contributing](https://doc.saymyframe.com/contributing.md#the-roles-at-run-time) lists these tests.

The mocks of a provider take test tokens in the tests of an app, whatever their issuer among those of the app:

- A test token is a JSON Web Token without a signature, whose header has `alg` none and whose payload has `iss`, the issuer, `sub`, the identity at the identity provider, maybe `email`, and the `nonce` of its request, if the request had one. A token of a request without a nonce has none, as Google sign-in started without one gets it.
- The nonce of a token is the `rawNonce` of its credential, or the SHA-256 hash of that `rawNonce` in lowercase hexadecimal, as a way to sign in whose request has the hash gets it, such as Sign in with Apple. Where its backend checks the nonce, a provider refuses a token whose nonce is neither.
- A token with a nonce may be taken once: a provider refuses a token whose nonce it took already with `invalidCredentials`. The one exception is the credential object that `linkWith` refused just before, which `signInWith` takes with what the provider kept for it.
- A credential of the issuer of a way to sign in that `revokesOnDeletion` has an `authorizationCode` of its own.

The mocks of the module of a way to sign in issue such tokens, of one identity of their own and the same each time.

The tests that a provider keeps for what only it does use the implementation of its own module, as those of `firebase_auth` use `createFirebaseAuthService()`, and check what reaches its platform side. [Sign-in](https://doc.saymyframe.com/guides/sign-in.md#in-tests) describes what a test of the session can do.
