> ## Documentation Index
> Fetch the complete documentation index at: https://docs-dev-update-anonymous-sessons-ea.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> Lock.Swift で利用できる動作設定オプション

# Lock.swift: 設定オプション

以下に、Lock の動作を設定するためのさまざまなオプションを示します。さらに、[Style Customization Options](/docs/ja-jp/libraries/lock-swift/lock-swift-customization) ページでは、Lock の外観やスタイルを変更するためのオプションも多数用意されています。

<h2 id="configuring-locks-behavior">
  Lock の動作を設定する
</h2>

設定オプションは、`withOptions` を使って Lock の初期化時に追加できます。

```swift lines theme={null}
Lock
  .classic()
  .withOptions {
    $0.closable = true
    $0.usernameStyle = [.Username]
    $0.allow = [.Login, .ResetPassword]
  }
  .present(from: self)
```

<h2 id="behavior-options">
  動作設定
</h2>

<h3 id="closable">
  closable
</h3>

ユーザーがLockを閉じられるようにします。デフォルトでは `false` です。

```swift lines theme={null}
.withOptions {
  $0.closable = true
}
```

<h3 id="scope">
  scope
</h3>

認証に使用するスコープです。デフォルトは `openid` です。返されるのは **<Tooltip tip="Access Token: API へのアクセスに使用される、不透明な文字列または JWT 形式の認可資格情報。" cta="用語集を見る" href="/docs/ja-jp/glossary?term=Access+Token">アクセストークン</Tooltip>** だけでなく、ユーザー情報を含む <Tooltip tip="ID Token: リソースへのアクセスではなく、クライアント自体のための資格情報。" cta="用語集を見る" href="/docs/ja-jp/glossary?term=JSON+Web+Token">JSON Web トークン</Tooltip> (JWT) である **<Tooltip tip="ID Token: リソースへのアクセスではなく、クライアント自体のための資格情報。" cta="用語集を見る" href="/docs/ja-jp/glossary?term=ID+Token">ID トークン</Tooltip>** も含まれます。認証スコープの詳細については、[Scopes](/docs/ja-jp/get-started/apis/scopes) のドキュメントを参照してください。

```swift lines theme={null}
.withOptions {
  $0.scope = "openid name email picture"
}
```

<h4 id="refresh-tokens">
  リフレッシュトークン
</h4>

Lock オプションで `offline_access` スコープを指定すると、access\_token と id\_token に加えて [リフレッシュトークン](/docs/ja-jp/secure/tokens/refresh-tokens) を受け取れるようになります。<Tooltip tip="リフレッシュトークン: ユーザーに再度ログインを求めることなく、新しいアクセストークンを取得するために使用されるトークン。" cta="用語集を見る" href="/docs/ja-jp/glossary?term=Refresh+Tokens">リフレッシュトークン</Tooltip>は保存しておき、古いアクセストークンの有効期限が切れたときに新しいアクセストークンを取得するために使用できます。Auth0 認証でリフレッシュトークンを使用する方法の詳細については、リフレッシュトークンの実装に使用する [Auth0.Swift SDK](/docs/ja-jp/libraries/auth0-swift) のリファレンスドキュメント、または Swift 開発での Auth0 の使用例 (リフレッシュトークンの管理を含む) を包括的に紹介している [Swift Quickstart ガイド](/docs/ja-jp/quickstart/native/ios-swift) を参照してください。

<h3 id="termsofservice">
  termsOfService
</h3>

デフォルトでは、Lock は Auth0 の利用規約とプライバシーポリシーを使用しますが、別の URL を指定して、他の利用規約やポリシーにリンクすることもできます。

```swift lines theme={null}
.withOptions {
  $0.termsOfService = "https://mycompany.com/terms"
  $0.privacyPolicy = "https://mycompany.com/privacy"
}
```

<h3 id="show-terms-of-service">
  利用規約を表示
</h3>

データベース接続では、利用規約ダイアログが表示されます。既定値は `true` です。なお、`mustAcceptTerms` フラグが有効な場合、利用規約は常に表示されます。

```swift lines theme={null}
.withOptions {
    $0.showTerms = true
}
```

<h3 id="require-users-to-accept-the-terms-of-service">
  ユーザーに利用規約への同意を必須にする
</h3>

データベース接続では、利用規約に明示的に同意する必要があります。

```swift lines theme={null}
.withOptions {
    $0.mustAcceptTerms = true
}
```

<h2 id="web-authentication-options">
  Web Authentication オプション
</h2>

<h3 id="leeway">
  leeway
</h3>

ID トークンのバリデーションで使用するクロックスキューです。サーバー時刻とクライアント時刻の差を考慮して、ID トークンが有効と見なされる時間の許容範囲を広げます。デフォルトは **60000 ミリ秒** (60 秒) です。

```swift lines theme={null}
.withOptions {
  $0.leeway = 30000 // 30秒
}
```

<h3 id="maxage">
  maxAge
</h3>

ユーザーが最後に認証されてからの経過時間として許容される最大値 (ミリ秒) です。ID トークンのバリデーションに使用されます。設定すると、ID トークンには認証時刻を示す `auth_time` クレームが含まれます。デフォルトは `nil` です。

```swift lines theme={null}
.withOptions {
  $0.maxAge = 86400000 // 1日
}
```

<h2 id="database-options">
  Database のオプション
</h2>

<h3 id="allow">
  allow
</h3>

アクセス可能な Database の画面を指定します。既定では、`.Login`、`.Signup`、`.ResetPassword` など、すべての画面が有効です。

```swift lines theme={null}
.withOptions {
  $0.allow = [.Login, .ResetPassword]
}
```

<h3 id="initialscreen">
  initialScreen
</h3>

ユーザーに最初に表示される画面です。既定値は `.Login` で、その他のオプションには `.Signup` と `ResetPassword` があります。

```swift lines theme={null}
.withOptions {
  $0.initialScreen = .Login
}
```

<h3 id="usernamestyle">
  usernameStyle
</h3>

ログイン時に必要な識別子の種類を指定します。デフォルトは `[.Username, .Email]` ですが、`[.Username]` または `[.Email]` も指定できます。ただし、このオプションが有効になるのは、[Auth0 Dashboard](https://manage.auth0.com/#/) で `requires_username` フラグを `true` に設定している場合のみです。

```swift lines theme={null}
.withOptions {
  $0.usernameStyle = [.Username]
}
```

<h4 id="custom-signup-fields">
  カスタム Signup フィールド
</h4>

Signup 時に既定で必要な情報は、ユーザーのメールアドレスとパスワードです。必要に応じて、収集する情報を増やすことができます。ここで追加の Signup フィールドを収集すると、それらは `user_metadata` に保存されます。詳しくは [メタデータ](/docs/ja-jp/manage-users/user-accounts/metadata) をご覧ください。なお、カスタムのテキストフィールドで使用するアイコンは指定する必要があります。

```swift lines theme={null}
.withOptions {
  $0.customSignupFields = [
    CustomTextField(name: "first_name", placeholder: "First Name", icon: LazyImage(name: "ic_person", bundle: Lock.bundle)),
    CustomTextField(name: "last_name", placeholder: "Last Name", icon: LazyImage(name: "ic_person", bundle: Lock.bundle))
  ]
}
```

以下の例のように、ほかのバンドルのアイコンを指定することもできます。
CustomTextField(name: "slack\_handle", placeholder: "Slack Handle", icon: LazyImage(name: "ic\_slack", bundle: Bundle(identifier: "CustomBundle")))

<h2 id="enterprise-options">
  エンタープライズのオプション
</h2>

エンタープライズ接続固有の設定オプションもあります：

<h3 id="enterpriseconnectionusingactiveauth">
  enterpriseConnectionUsingActiveAuth
</h3>

デフォルトでは、エンタープライズ接続は Web Authentication を使用します。ただし、代わりに資格情報を使った認証を使用し、ユーザー名とパスワードの入力を求める接続を指定することもできます。

```swift lines theme={null}
.withOptions {
  $0.enterpriseConnectionUsingActiveAuth = ["enterprisedomain.com"]
}
```

<h3 id="activedirectoryemailasusername">
  activeDirectoryEmailAsUsername
</h3>

クレデンシャル認証モードでは、ユーザーの識別子としてメールアドレスを必須にしますか？デフォルトは `false` で、その場合は代わりにユーザー名が必要です。

```swift lines theme={null}
.withOptions {
  $0.activeDirectoryEmailAsUsername = true
}
```

<h2 id="logging-options">
  ログオプション
</h2>

Lock では、ログ機能のオン/オフや、そのほかのログ関連設定を簡単に変更できます。

<h3 id="loglevel">
  logLevel
</h3>

既定では `.off` です。Syslog のログレベルをサポートしています。

```swift lines theme={null}
.withOptions {
  $0.logLevel = .all
}
```

<h3 id="loghttprequest">
  logHttpRequest
</h3>

Auth0.swift API リクエストをログに記録するかどうかを指定します。デフォルトでは `false` です。

```swift lines theme={null}
.withOptions {
  $0.logHttpRequest = true
}
```

<h3 id="loggeroutput">
  loggerOutput
</h3>

ロガーの出力ハンドラーを指定します。デフォルトでは `print` 文を使用します。

```swift lines theme={null}
.withOptions {
  $0.loggerOutput = CleanroomLockLogger()
}
```

上記のコードでは、loggerOutput が [CleanroomLogger](https://github.com/emaloney/CleanroomLogger) を使用するように設定されています。通常は、loggerOutput プロトコルを実装することでこれを実現できます。もちろん、お好みのロガーライブラリを使用することもできます。以下は、CleanroomLogger で logger 出力を処理する使用例です。

```swift lines theme={null}
class CleanroomLockLogger: LoggerOutput {
  func message(_ message: String, level: LoggerLevel, filename: String, line: Int) {
    let channel: LogChannel?
    switch level {
    case .debug:
        channel = Log.debug
    case .error:
        channel = Log.error
    case .info:
        channel = Log.info
    case .verbose:
        channel = Log.verbose
    case .warn:
        channel = Log.warning
    default:
        channel = nil
    }
    channel?.message(message, filePath: filename, fileLine: line)
  }
}
```
