## Flutter advanced setup

In this guide, you'll find an overview and best practices for enabling advanced features and using built in functions for biometric authentication (passkeys) and step-up.

### Custom loader iOS

To customize the loader for iOS when using Embedded mode, you can set up a custom loader by modifying your `AppDelegate.swift` file. The custom loader will be displayed during authentication processes.

First, ensure that Embedded mode is enabled in your configuration.

```xml
 <plist version="1.0">
<dict>
	<key>embeddedMode</key>
	<true/>
	...
</dict>
</plist>
```

Here's how to implement a custom loader:

```swift
import SwiftUI
import FronteggSwift

@main
@objc class AppDelegate: FlutterAppDelegate {
    override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
         ...
        // Setup Loader for Frontegg Embedded Loading
        // Can be any view
        DefaultLoader.customLoaderView = AnyView(Text("Loading..."))
        
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }
    ...
}
```

### Custom loader Android

To customize the loader for Android when using Embedded Activity mode, you can set up a custom loader by modifying your `MainActivity.kt` file. The custom loader will be displayed during authentication processes.

First, ensure that Embedded Activity mode is enabled in your configuration.

Here's how to implement a custom loader:

```groovy
import android.content.res.ColorStateList
import android.graphics.Color
import android.os.Bundle
import android.widget.ProgressBar
import com.frontegg.android.ui.DefaultLoader
import io.flutter.embedding.android.FlutterActivity

class MainActivity : FlutterActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    // Setup Loader for Frontegg Embedded Activity Loading
    DefaultLoader.setLoaderProvider {
      // Can be any view
      val progressBar = ProgressBar(it)
      val colorStateList = ColorStateList.valueOf(Color.GREEN)
      progressBar.indeterminateTintList = colorStateList

      progressBar
    }
  }
}
```

### Step-up authentication

Step-up authentication allows you to temporarily elevate a user's authentication level to perform sensitive actions. This is useful for operations like updating credentials, accessing confidential data, or performing secure transactions.

`maxAge` (optional): How long the elevated session is considered valid, in seconds.

`completion`: A closure called after authentication finishes. If step-up fails, it receives an error.

#### `stepUp` -

Starts the step-up authentication flow. This will usually trigger a stronger authentication method ( e.g. biometric, MFA, etc).

```
await stepUp(maxAge: Duration(minutes: 5));
```

#### `isSteppedUp` -

This method checks if the user has recently completed a step-up authentication and whether it is still valid.

```
final isElevated = await isSteppedUp(maxAge: Duration(minutes: 5));
if (isElevated) {
    // Proceed with sensitive action
} else {
    // Prompt user to step up
}
```

**Example:**

```
Future<void> performSensitiveAction() async {
    final steppedUp = await isSteppedUp(maxAge: Duration(minutes: 5));
      
    if (!steppedUp) {
        await stepUp(maxAge: Duration(minutes: 5));
    }
      
    // Continue with the sensitive action
}
```

### Frontegg Flutter API Reference

#### Properties

| Property | Description |
|  --- | --- |
| `currentState` | Gets the current state of the Frontegg instance. Returns a `FronteggState` which includes information such as authentication status, loading status, and user details. |
| `stateChanged` | A stream that emits changes to the FronteggState. Provides real-time updates on the state of authentication, user details, and loading statuses. |


#### Methods

##### Authentication methods

| Method | Parameters | Description |
|  --- | --- | --- |
| `login` | `loginHint` (optional): String | Initiates the authentication process. `loginHint` pre-fills the login field in the Frontegg Login Box. |
| `loginWithPasskeys` | None | Authenticates the user using passkeys. |
| `registerPasskeys` | None | Registers passkeys for the user. |
| `logout` | None | Logs out the user and clears user-related data. |
| `refreshToken` | None | Refreshes the authentication token if needed. Returns `true` if successful. |


##### Social & direct login methods

| Method | Parameters | Description |
|  --- | --- | --- |
| `directLogin` | - `url` (required): String- `ephemeralSession`: bool = true- `additionalQueryParams`: Map<String, String>? | Initiates a direct login using a provided URL. |
| `socialLogin` | - `provider` (required): FronteggSocialProvider- `ephemeralSession`: bool = true- `additionalQueryParams`: Map<String, String>? | Initiates a social login using the specified provider (e.g., Google, Facebook). |
| `customSocialLogin` | - `id` (required): String- `ephemeralSession`: bool = true- `additionalQueryParams`: Map<String, String>? | Initiates a custom social login using a unique identifier. |


##### Account (tenant) management

| Method | Parameters | Description |
|  --- | --- | --- |
| `switchTenant` | `tenantId` (required): String | Switches the user's active tenant. IDs available from `User.tenants.tenantId`. |


##### Authorization & security

| Method | Parameters | Description |
|  --- | --- | --- |
| `requestAuthorize` | - `refreshToken` (required): String- `deviceTokenCookie`: String? | Initiates authorization request. Returns `FronteggUser` if successful, `null` if fails. |
| `isSteppedUp` | `maxAge`: Duration? | Checks if user has completed step-up authentication. |
| `stepUp` | `maxAge`: Duration? | Initiates the step-up authentication process. |


##### Utility methods

| Method | Parameters | Description |
|  --- | --- | --- |
| `dispose` | None | Cancels the subscription to the state stream. Call when object is no longer needed. |
| `getConstants` | None | Fetches the Frontegg constants containing configuration values. Returns `FronteggConstants`. |


##### Error handling

All methods can throw a `FronteggException` for platform-specific errors. The error handling system automatically converts platform exceptions to `FronteggException` instances.

##### Common error scenarios:

- Authentication failures
- Network connectivity issues
- Invalid parameters
- Platform-specific errors