|
8 | 8 |
|
9 | 9 | # Logto Flutter SDKs
|
10 | 10 |
|
11 |
| -The monorepo for SDKs and demo apps wrote in Flutter. |
| 11 | +[](https://github.com/logto-io/dart/actions/workflows/main.yml) |
| 12 | + |
| 13 | +Logto's flutter SDK for native apps. |
| 14 | + |
| 15 | +## Installation |
| 16 | + |
| 17 | +```sh |
| 18 | +flutter pub get logto_dart_sdk |
| 19 | +``` |
| 20 | + |
| 21 | +## Products |
| 22 | + |
| 23 | +| Name | Description | |
| 24 | +| ------------ | ----------------------------------------------------------------------------------------------------------- | |
| 25 | +| logto_core | Core SDK is used for generation dart project with basic API and util method provided. | |
| 26 | +| logto_client | Client SDK for flutter native apps. Built based on logto_core with user sign-in interaction flow integrated | |
| 27 | + |
| 28 | +## Logto Client |
| 29 | + |
| 30 | +### Configurations |
| 31 | + |
| 32 | +#### flutter_secure_storage |
| 33 | + |
| 34 | +We use [flutter_secure_storage](https://pub.dev/packages/flutter_secure_storage) to implement the cross-platform persistent auth_token secure storage. |
| 35 | + |
| 36 | +- Keychain is used for iOS |
| 37 | +- AES encryption is used for Android. |
| 38 | + |
| 39 | +Configure Android version: |
| 40 | + |
| 41 | +In [project]/android/app/build.gradle set minSdkVersion to >= 18. |
| 42 | + |
| 43 | +```gradle |
| 44 | +android { |
| 45 | + ... |
| 46 | +
|
| 47 | + defaultConfig { |
| 48 | + ... |
| 49 | + minSdkVersion 18 |
| 50 | + ... |
| 51 | + } |
| 52 | +} |
| 53 | +
|
| 54 | +``` |
| 55 | + |
| 56 | +> Note By default Android backups data on Google Drive. It can cause exception java.security.InvalidKeyException:Failed to unwrap key. You need to: |
| 57 | +> |
| 58 | +> - disable autobackup, |
| 59 | +> - exclude sharedprefs FlutterSecureStorage used by the plugin |
| 60 | +
|
| 61 | +1. To disable autobackup, go to your app manifest file and set the boolean value android:allowBackup. |
| 62 | + |
| 63 | +```xml |
| 64 | +<manifest ... > |
| 65 | + ... |
| 66 | + <application |
| 67 | + android:allowBackup="false" |
| 68 | + android:fullBackupContent="false"> |
| 69 | + ... |
| 70 | + > |
| 71 | + ... |
| 72 | + </application> |
| 73 | +</manifest> |
| 74 | +``` |
| 75 | + |
| 76 | +2. Exclude sharedprefs FlutterSecureStorage. |
| 77 | + |
| 78 | +If you need to enable the android:fullBackupContent for your app. Set up a backup rule to [exclude](https://developer.android.com/guide/topics/data/autobackup#IncludingFiles) the prefs used by the plugin. |
| 79 | + |
| 80 | +```xml |
| 81 | +<application ... |
| 82 | + android:fullBackupContent="@xml/backup_rules"> |
| 83 | +</application> |
| 84 | +``` |
| 85 | + |
| 86 | +```xml |
| 87 | +<?xml version="1.0" encoding="utf-8"?> |
| 88 | +<full-backup-content> |
| 89 | + <exclude domain="sharedpref" path="FlutterSecureStorage"/> |
| 90 | +</full-backup-content> |
| 91 | +``` |
| 92 | + |
| 93 | +Please check [flutter_secure_storage](https://pub.dev/packages/flutter_secure_storage#configure-android-version) for more details. |
| 94 | + |
| 95 | +#### flutter_web_auth |
| 96 | + |
| 97 | +[flutter_web_auth](https://pub.dev/packages/flutter_appauth) is used behind Logto's flutter SDK. We rely on its webview-based interaction interface to open Logto's authorization pages. |
| 98 | + |
| 99 | +> In the background, this plugin uses ASWebAuthenticationSession on iOS 12+ and macOS 10.15+, SFAuthenticationSession on iOS 11, Chrome Custom Tabs on Android and opens a new window on Web. You can build it with iOS 8+, but it is currently only supported by iOS 11 or higher. |
| 100 | +
|
| 101 | +Android |
| 102 | + |
| 103 | +In order to capture the callback url from Logto's sign-in web page, you will need to register your sign-in redirectUri to the `AndroidManifest.xml`. |
| 104 | + |
| 105 | +```xml |
| 106 | +<activity android:name="com.linusu.flutter_web_auth.CallbackActivity" android:exported="false"> |
| 107 | + <intent-filter android:label="flutter_web_auth"> |
| 108 | + <action android:name="android.intent.action.VIEW"/> |
| 109 | + <category android:name="android.intent.category.DEFAULT"/> |
| 110 | + <category android:name="android.intent.category.BROWSABLE"/> |
| 111 | + <data android:scheme="io.logto"/> |
| 112 | + </intent-filter> |
| 113 | +</activity> |
| 114 | +``` |
| 115 | + |
| 116 | +By doing so, your app will automatically capture the callbaclUri after a successful sign-in and redirect the user back to the app. |
| 117 | + |
| 118 | +### Basic Usage |
| 119 | + |
| 120 | +```dart |
| 121 | +import 'package:logto_dart_sdk/logto_dart_sdk.dart'; |
| 122 | +
|
| 123 | +// ... |
| 124 | +late LogtoClient logtoClient; |
| 125 | +
|
| 126 | +void _init() async { |
| 127 | + logtoClient = LogtoClient( |
| 128 | + config: config, // LogtoConfig |
| 129 | + httpClient: http.Client(), // Optional http client |
| 130 | + ); |
| 131 | +} |
| 132 | +
|
| 133 | +void signIn() async { |
| 134 | + await logtoClient.signIn(redirectUri); |
| 135 | +} |
| 136 | +
|
| 137 | +void signOut() async { |
| 138 | + await logtoClient.signOut(); |
| 139 | +} |
| 140 | +
|
| 141 | +``` |
| 142 | + |
| 143 | +### Class LogtoConfig |
| 144 | + |
| 145 | +#### Properties |
| 146 | + |
| 147 | +| name | type | description | |
| 148 | +| --------- | --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | |
| 149 | +| appId | String | Your appId generate from Logto's admin console | |
| 150 | +| appSecret | String? | App Secret generated along with the appId. Optional for native apps. | |
| 151 | +| endpoint | String | Your logto server endpoint. e.g. https://logto.dev | |
| 152 | +| scopes | List<String>? | List all the permission scopes your app will request for. You may define and find it through Logto's admin console. | |
| 153 | +| resources | List<String>? | List all the [resource indicators](https://docs.logto.io/docs/references/resources/) you app may request for access. You may define and find it through Logto's admin console. | |
| 154 | + |
| 155 | +### Class LogtoClient |
| 156 | + |
| 157 | +#### Properties |
| 158 | + |
| 159 | +| name | type | description | |
| 160 | +| --------------- | --------------------------------------- | ----------------------------------------------- | |
| 161 | +| config | final LogtoConfig | Logto Config used to init Logto Client | |
| 162 | +| idToken | read-only Future<String?> | idToken returned after success authentication | |
| 163 | +| isAuthenticated | read-only Future<bool> | Is Authenticated status | |
| 164 | +| idTokenClaims | read-only Future<OpenIdClaims?> | Decoded idToken claims including basic userinfo | |
| 165 | +| loading | read-only bool | Global API loading status | |
| 166 | + |
| 167 | +#### Methods |
| 168 | + |
| 169 | +| name | type | description | |
| 170 | +| -------------- | --------------------------------------------------- | ------------------------------------------------------------------- | |
| 171 | +| getAccessToken | ({String? resource}) -> Future<AccessToken> | Request for an api resource specific access token for authorization | |
| 172 | +| signIn | (String? redirectUri) -> Future<void> | Init user sign-in flow | |
| 173 | +| signOut | () -> Future<void> | Sign-out | |
0 commit comments