diff --git a/packages/docs/docs/tutorial/integrate-sdk/android.mdx b/packages/docs/docs/tutorial/integrate-sdk/android.mdx index 877507a56..a0c556933 100644 --- a/packages/docs/docs/tutorial/integrate-sdk/android.mdx +++ b/packages/docs/docs/tutorial/integrate-sdk/android.mdx @@ -7,27 +7,26 @@ import TabItem from '@theme/TabItem'; # Integrate `io.logto.sdk:android` - props.onNext(1)} -> - ## Step1: Install Logto Android SDK with Gradle ### Prerequisite -* Minimal Android SDK: Level 24 (TBD) +* Minimal Android SDK: Level 24 +Add the `mavenCentral()` repository to your Gradle project build file: +```kotlin +repositories { + mavenCentral() +} +``` + +Add the Logto Android SDK to your dependencies: ```kotlin dependencies { - implementation("io.logto.sdk:android:1.0.0") + implementation("io.logto.sdk:android:1.0.0") } ``` @@ -37,7 +36,7 @@ dependencies { ```groovy dependencies { - implementation 'io.logto.sdk:android:1.0.0' + implementation 'io.logto.sdk:android:1.0.0' } ``` @@ -45,16 +44,6 @@ dependencies { - - - props.onNext(2)} -> ## Step2: Configure @@ -87,15 +76,15 @@ import io.logto.sdk.android.LogtoClient import io.logto.sdk.android.type.LogtoConfig class MainActivity : AppCompatActivity() { - val logtoConfig = LogtoConfig( - endpoint = "", - appId = "", - scopes = null, - resources = null, - usingPersistStorage = true, - ) + val logtoConfig = LogtoConfig( + endpoint = "", + appId = "", + scopes = null, + resources = null, + usingPersistStorage = true, + ) - val logtoClient = LogtoClient(logtoConfig, application) + val logtoClient = LogtoClient(logtoConfig, application) } ``` @@ -108,22 +97,22 @@ import io.logto.sdk.android.LogtoClient; import io.logto.sdk.android.type.LogtoConfig; public class MainActivity extends AppCompatActivity { - private LogtoClient logtoClient; + private LogtoClient logtoClient; - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_main); + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); - LogtoConfig logtoConfig = new LogtoConfig( - "", - "", - null, - null, - true - ); - - logtoClient = new LogtoClient(logtoConfig, getApplication()); + LogtoConfig logtoConfig = new LogtoConfig( + "", + "", + null, + null, + true + ); + + logtoClient = new LogtoClient(logtoConfig, getApplication()); } } ``` @@ -137,16 +126,6 @@ Notes: - `` is the logto service running in your machine. If you start logto service in `http://localhost:300` then `` is the Application ID of your application, which is created in the Admin Console. - - - props.onNext(3)} -> ## Step3: Sign In @@ -192,15 +171,15 @@ logtoClient.signInWithBrowser( ```kotlin logtoClient.getAccessToken { logtoException: LogtoException?, result: AccessToken? -> - // custom logic + // custom logic } -logtoClient.getIdTokenClaims { logtoException: LogtoException?, result: IdTokenCliams? -> - // custom logic +logtoClient.getIdTokenClaims { logtoException: LogtoException?, result: IdTokenClaims? -> + // custom logic } logtoClient.fetchUserInfo { logtoException: LogtoException?, userInfoResponse: UserInfoResponse? -> - // custom logic + // custom logic } ``` @@ -210,7 +189,7 @@ logtoClient.fetchUserInfo { logtoException: LogtoException?, userInfoResponse: U ```java logtoClient.getAccessToken((logtoException, accessToken) -> { - // custom logic + // custom logic }); logtoClient.getIdTokenClaims((logtoException, idTokenClaims) -> { @@ -226,15 +205,6 @@ logtoClient.fetchUserInfo((logtoException, userInfoResponse) -> { - - - props.onNext(4)} -> ## Step4: Sign Out @@ -246,7 +216,7 @@ logtoClient.fetchUserInfo((logtoException, userInfoResponse) -> { ```kotlin logtoClient.signOut { logtoException: LogtoException? -> - // custom logic + // custom logic } ``` @@ -268,22 +238,9 @@ Notes: - `signOut` will always clear local credentials even if errors occurred. - - - ## Step5: Further Readings - Fetch User Info - Configure Social Sign-In - Access Protected API Resources - - diff --git a/packages/docs/i18n/zh-cn/docusaurus-plugin-content-docs/current/tutorial/integrate-sdk/android.mdx b/packages/docs/i18n/zh-cn/docusaurus-plugin-content-docs/current/tutorial/integrate-sdk/android.mdx new file mode 100644 index 000000000..46af395dc --- /dev/null +++ b/packages/docs/i18n/zh-cn/docusaurus-plugin-content-docs/current/tutorial/integrate-sdk/android.mdx @@ -0,0 +1,246 @@ +--- +sidebar_label: Android +--- + +import Tabs from '@theme/Tabs'; +import TabItem from '@theme/TabItem'; + +# 集成 `io.logto.sdk:android` + +## 步骤 1: 从 Gradle 安装 Logto Android SDK + +### 前提条件 +* 最小 Android SDK 版本: Level 24 + +将 `mavenCentral()` 添加到构建脚本中: +```kotlin +repositories { + mavenCentral() +} +``` + +添加 Logto Android SDK 依赖: + + + + +```kotlin +dependencies { + implementation("io.logto.sdk:android:1.0.0") +} +``` + + + + + +```groovy +dependencies { + implementation 'io.logto.sdk:android:1.0.0' +} +``` + + + + + + +## 步骤 2: 配置 + +### 配置 Redirect URI + +Redirect URI 指定了应用用来接受授权结果的端口,Logto Android SDK 内部实现了该重定向的功能 + +### 在管理员控制台中配置 Redirect URI + +将以下值添加到应用设置界面中的 Redirect URI 配置项: + +```bash +$(LOGTO_REDIRECT_SCHEME)://$(YOUR_APP_PACKAGE)/callback +``` + +注意: +- `LOGTO_REDIRECT_SCHEME` 应为自定义的反向域名格式的一串字符。 +- 将上述的 `$(LOGTO_REDIRECT_SCHEME)` 替换成你定义的值。 + +例: `io.logto.android://io.logto.sample/callback` + +### 配置 Logto Android SDK + + + + + +```kotlin +import io.logto.sdk.android.LogtoClient +import io.logto.sdk.android.type.LogtoConfig + +class MainActivity : AppCompatActivity() { + val logtoConfig = LogtoConfig( + endpoint = "", + appId = "", + scopes = null, + resources = null, + usingPersistStorage = true, + ) + + val logtoClient = LogtoClient(logtoConfig, application) +} +``` + + + + + +```java +import io.logto.sdk.android.LogtoClient; +import io.logto.sdk.android.type.LogtoConfig; + +public class MainActivity extends AppCompatActivity { + private LogtoClient logtoClient; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + LogtoConfig logtoConfig = new LogtoConfig( + "", + "", + null, + null, + true + ); + + logtoClient = new LogtoClient(logtoConfig, getApplication()); + } +} +``` + + + + + +Notes: + +- `` 是你运行 Logto 服务所在的地址. 若你的 Logto 服务运行在 `http://localhost:300`,则 `` 是你为自己的应用在管理员控制台中所创建的客户端ID. + + +## 步骤 3: 登录 + + +### 执行登录 + + + + + +```kotlin +logtoClient.signInWithBrowser( + this, + "io.logto.android://io.logto.sample/callback", +) { logtoException: LogtoException? -> + // 后续处理逻辑 +} +``` + + + + + +```java +logtoClient.signInWithBrowser( + this, + "io.logto.android://io.logto.sample/callback", + logtoException -> { + // 后续处理逻辑 + } +); +``` + + + + + +### 登录成功后,你可以使用 SDK 提供的一些 API 来实现自己的业务逻辑 + + + + + +```kotlin +logtoClient.getAccessToken { logtoException: LogtoException?, result: AccessToken? -> + // 后续处理逻辑 +} + +logtoClient.getIdTokenClaims { logtoException: LogtoException?, result: IdTokenClaims? -> + // 后续处理逻辑 +} + +logtoClient.fetchUserInfo { logtoException: LogtoException?, userInfoResponse: UserInfoResponse? -> + // 后续处理逻辑 +} +``` + + + + + +```java +logtoClient.getAccessToken((logtoException, accessToken) -> { + // 后续处理逻辑 +}); + +logtoClient.getIdTokenClaims((logtoException, idTokenClaims) -> { + // 后续处理逻辑 +}); + +logtoClient.fetchUserInfo((logtoException, userInfoResponse) -> { + // 后续处理逻辑 +}); +``` + + + + + + +## 步骤 4: 登出 + +### 执行登出 + + + + + +```kotlin +logtoClient.signOut { logtoException: LogtoException? -> + // 后续处理逻辑 +} +``` + + + + + +```java +logtoClient.signOut(logtoException -> { + // 后续处理逻辑 +}); +``` + + + + + +注意: + +- 登出操作会清除本地存储的用户相关凭据,即使在登出过程中发生了异常。 + + +## 步骤 5: 延伸阅读 + +- 获取用户信息 +- 配置社会化登录 +- 访问受保护的 API 资源