
# ログイン画面

ログイン画面の既製の部品です。`LoginForm` (`AuthenticationManager` を使うユーザー名とパスワード)、プロパティの任意の登録のための `OAuth2LoginButton`、そしてその所有者が定める見た目の Google、Apple、Microsoft、GitHub のボタンです。これらはログインを始めるだけです。その後に起きることは、`AuthenticationManager` や `OAuth2LoginService` の仕事です。

## 使う理由

どのアプリも同じフォーム (自動入力のヒント、表示の切り替え付きの伏せられたパスワード、間違った名前と間違ったパスワードに対する同じエラー) と、同じブランドのボタン (その色、ロゴ、サイズは所有者が定め、App Review がチェックします) を書きます。これらの部品はそれを一度で行い、アプリのデザイン (Material、Cupertino、素の `WidgetsApp`) に従います。

## 例

```dart
// フォームと二つのプロバイダーを持つログイン画面。
Scaffold(
  appBar: AppBar(title: const Text('Log in')),
  body: Padding(
    padding: const EdgeInsets.all(16),
    child: Column(
      children: [
        LoginForm(onSuccess: (_) => Navigator.of(context).pop()),
        const SizedBox(height: 24),
        GoogleLoginButton(onSuccess: (_) => Navigator.of(context).pop()),
        OAuth2LoginButton(
          'keycloak',
          icon: const Icon(Icons.key),
          onSuccess: (_) => Navigator.of(context).pop(),
        ),
      ],
    ),
  ),
)
```

## そのデザインはアプリに従う

`LoginForm` と `OAuth2LoginButton` は Material も Cupertino もインポートしません。フィールドとボタンは、アプリのデザインライブラリーのコントロール (Flutter のスターターの `DesignControls`) で描かれ、それはデザインのスターターが渡します。

| アプリ | フォームと `OAuth2LoginButton` |
|---|---|
| `spring.flutter.app.type: material`。`spring_boot_starter_flutter_material` とともに | アプリの `ThemeData` (したがって `spring.flutter.theme.components.input.*` と `button.*`) でスタイルが付けられた Material のテキストフィールドと `FilledButton` |
| `spring.flutter.app.type: cupertino`。`spring_boot_starter_flutter_cupertino` とともに | `CupertinoTheme` でスタイルが付けられた `CupertinoTextField` と `CupertinoButton.filled` |
| デザインライブラリーなし (`widgets`) | `SpringTheme` の色の素のフィールドとボタン |

アプリが自分で組み立てる `MaterialApp` や `CupertinoApp` の下でも、アプリケーションにそのデザインのスターターがあれば、そのデザインのコントロールを見つけます。`design:` で選べます: `LoginDesign.app` (アプリのデザインライブラリーのコントロール。ないところでは素のウィジェット) または `LoginDesign.widgets` (常に素のウィジェット)。`design:` がなければ、ウィジェットは自分の上からデザインを見つけます。ブランドのボタンはどこでも同じ見た目です。その見た目はブランドが決めます。

上の例は `material` の種類のアプリのものです。`Scaffold`、`AppBar`、`Icons` は `material_ui` から来ます。

## LoginForm

送信時 (ボタン、またはパスワードのフィールドでの Enter) に、両方のフィールドが埋まっていることをチェックし、ユーザー名の前後の空白を取り除き、`UsernamePasswordAuthenticationToken` でアプリケーションの `AuthenticationManager` Bean に問い合わせます。その間、ボタンは進捗を表示し、フィールドはロックされます。

- 成功すると、ユーザーを `SecurityContextHolder` に置き、`InteractiveAuthenticationSuccessEvent` を発行し、パスワードマネージャーにログインを保存するよう伝え、`onSuccess` を呼びます。
- 失敗すると、パスワードをクリアし、エラーを表示し、`onError` を呼びます。間違った名前やパスワード、未知のユーザー、拒否されたアカウントは同じテキストを表示するので、どの部分が間違っていたかは誰にもわかりません。それ以外は、ログインをチェックできなかったと伝えます。
- フィールドは `username` と `password` の自動入力のヒントを持ちます。パスワードは表示・非表示の切り替え付きで伏せられます。何もログに出ません。
- `authenticate:` は、独自のサービスでログインするアプリのためにマネージャーを置き換えます。`authenticationManager:` は別のマネージャーを指定します。
- ビルダーで、テーマの後にカスタマイズできます。`fieldBuilder` (`LoginFormField`)、`submitButtonBuilder` (`LoginFormSubmitButton`)、`errorBuilder`、そして失敗のテキストのための `errorMessage` です。

テキストは、これらの英語のデフォルトとともにアプリのメッセージファイルから、または `usernameLabel`、`passwordLabel`、`submitLabel` から来ます。

```properties
# lib/resources/messages.properties
login-form.username=User name
login-form.password=Password
login-form.submit=Log in
login-form.show-password=Show password
login-form.hide-password=Hide password
login-form.username-required=Enter your user name
login-form.password-required=Enter your password
login-form.bad-credentials=Wrong user name or password
login-form.service-error=The login could not be checked. Try again later.
```

## OAuth2LoginButton

```dart
OAuth2LoginButton(
  'keycloak',
  icon: const Icon(Icons.key),
  additionalParameters: const {'prompt': 'login'},
  onSuccess: (_) => Navigator.of(context).pop(),
  onError: (error) => setState(() => _error = '$error'),
)
```

`OAuth2LoginService.login(registrationId)` を呼び、ログインの実行中は進捗を表示し、ユーザーとともに `onSuccess` を呼びます (ページがログインへ去るブラウザーでは `null`)。ユーザーがログインウィンドウを閉じるのはエラーではありません。ボタンは再び使えるようになります。その他の失敗は `onError` に、それがなければ `FlutterError.reportError` に行きます。そのテキストは「Sign in with _client name_」(メッセージ `login-button.oauth2`、`Sign in with {0}`) です。登録の `client-name`、なければ既知のプロバイダーの名前、なければ登録 ID です。`label:` はテキストを、`login:` は呼び出しを、`builder:` は見た目を置き換えます。

## プロバイダーのボタン

| ボタン | 登録 ID (デフォルト) | アプリが選べるもの |
|---|---|---|
| `GoogleLoginButton` | `google` | `theme` (light、dark、neutral)、`shape` (rectangular、pill)、`text` (sign in、sign up、continue)、`iconOnly` |
| `AppleLoginButton` | `apple` | `style` (black、white、white with outline)、`text`、`circular` 付きの `iconOnly`、`height` (30 以上、デフォルトは 44)、`cornerRadius`。アプリのアセットからの `logo` |
| `MicrosoftLoginButton` | `microsoft` | `theme` (light、dark)、`text` ("Sign in with Microsoft"、"Sign in") |
| `GitHubLoginButton` | `github` | `theme` (dark、light)、`shape`、`text`、`iconOnly` |

それぞれは中身が `OAuth2LoginButton` です。同じ `onSuccess`、`onError`、`additionalParameters`、`loginService`、`login` を持ち、`registrationId` はプロパティの任意の ID にできます。色、ロゴ、フォント、サイズ、テキストはパラメーターではありません。それらは所有者の商標であり、所有者は何を変えてよいかを公開しています (Google のブランドガイドライン、App Review がチェックする Apple の Human Interface Guidelines、Microsoft と GitHub のロゴのルール)。ルールが対象にしないデザインは、ブランドのロゴなしで `OAuth2LoginButton(builder: ...)` で作ります。

**Apple のロゴはアプリから来ます。** Apple の Developer Artwork License は、メンバーが自分のアプリでだけロゴを使うことを許し、再配布を決して許さないので、ライブラリはそれを同梱できません。Apple Design Resources から「Sign in with Apple Logo」をダウンロードし、ファイルをアプリのアセットに追加し、`logo` として渡してください。

```dart
AppleLoginButton(
  logo: (context, artwork) => Image.asset(
    'assets/apple/${artwork.logoOnly ? 'logo-only' : 'left-aligned'}-'
    '${artwork.color.name}.png',
    height: artwork.height,
  ),
  onSuccess: (_) => Navigator.of(context).pop(),
)
```

iOS と macOS では、`login('apple')` は Apple のネイティブのシートを開きます。ボタンはログインのサービスを呼ぶだけで、選ぶのはサービスです。

**フォント。** Google Sans と Segoe UI は Google と Microsoft のもので、同梱されていません。使用が許されているなら独自のフォントとして追加してください。そうでなければ、ボタンは Roboto、アプリのフォント、またはシステムのフォントにフォールバックします。

テキストはアプリのメッセージで翻訳できます (意味を保ち、「Google」だけにはしないでください)。

```properties
login-button.google.sign-in=Sign in with Google
login-button.google.sign-up=Sign up with Google
login-button.google.continue=Continue with Google
login-button.apple.sign-in=Sign in with Apple
login-button.apple.sign-up=Sign up with Apple
login-button.apple.continue=Continue with Apple
login-button.microsoft.sign-in-with=Sign in with Microsoft
login-button.microsoft.sign-in=Sign in
login-button.github.sign-in=Sign in with GitHub
login-button.github.sign-up=Sign up with GitHub
login-button.github.continue=Continue with GitHub
```

## Spring (Java) との対応

最も近いのは Spring Security のデフォルトのログインページ (`formLogin()` と `oauth2Login()`) で、フォームと、クライアント名のラベルが付いた登録ごとに一つのリンクを持つサーバーのページです。

## 関連項目

- [ユーザーに従うウィジェット](principal-widgets.md)
- [プロバイダー: Google と Apple](providers.md)
- [テーマ](../spring-boot-starter-flutter/themes/index.md)
- [デザインライブラリー](../spring-boot-starter-flutter/design-libraries.md)
