Skip to content

Commit

Permalink
Merge pull request #954 from Web3Auth/session-management
Browse files Browse the repository at this point in the history
[Improve Initialisation] PnP Mobile SDKs
  • Loading branch information
shahbaz17 authored Oct 18, 2024
2 parents 965c523 + b8daada commit 25fa68b
Show file tree
Hide file tree
Showing 19 changed files with 161 additions and 122 deletions.
1 change: 0 additions & 1 deletion docs/sdk/core-kit/tkey/initialize.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ modules.
- [Configuration of Storage Layer](#configuring-storage-layer)
- [Configuration of Modules](#configuring-modules)
- [Instantiation of tKey](#instantiating-tkey)
- [Initialisation of Service Provider](#initializing-service-provider)

:::note

Expand Down
2 changes: 1 addition & 1 deletion docs/sdk/core-kit/tkey/usage.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ which is compatible with blockchains like Solana, Aptos, and etc. However, you c
`secp256k1` key into other curves and utilize it.

In addition to that, we have dedicated provider packages for EVM, Solana and XRPL libraries. You can
check out their respective documentation in the [Providers Section](/sdk/providers/).
check out their respective documentation in the [Providers Section](/sdk/pnp/web/providers/).

:::tip

Expand Down
70 changes: 64 additions & 6 deletions docs/sdk/pnp/android/initialize.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@ import SetResultUrl from "@site/src/common/sdk/pnp/android/_set-result-url.mdx";
import TabItem from "@theme/TabItem";
import Tabs from "@theme/Tabs";

After installation, the next step to use Web3Auth is to initialize the SDK.
After installation, the next step to use Web3Auth is to configure & initialize the SDK.

However, Initialization is a two-step process:
However, configuring and initializing is a four-step process:

1. [Creating a Web3Auth Instance](#create-web3auth-instance)
2. [Setting a Result URL](#set-result-url)
3. [Triggering Login exceptions](#triggering-login-exceptions)
4. [Initialize Web3Auth](#initialize-web3auth)

## Create Web3Auth Instance

Please note that these are the most critical steps where you must pass on different parameters
according to the preference of your project. Additionally, You must configure Whitelabeling and
Custom Authentication within this step if you wish to customize your Web3Auth Instance.
The first step is to create the Web3Auth instance, and configure the Whitelabeling, Session time,
and Custom Authentication. Please note that these are the most critical steps where you need to pass
on different parameters according to the preference of your project.

### Parameters

Expand Down Expand Up @@ -74,10 +76,66 @@ data class Web3AuthOptions(
</TabItem>
</Tabs>

### Initialize Web3Auth
### Instantiate Web3Auth

<Initialization />

## Set Result URL

<SetResultUrl />

## Triggering Login exceptions

The `setCustomTabsClosed` method can be used to trigger login exceptions for Android. The Android
SDK uses the custom tabs and from current implementation of chrome custom tab, it's not possible to
add a listener directly to chrome custom tab close button and trigger login exceptions.

Hence, it's necessary to use `setCustomTabsClosed` method in your login screen to trigger
exceptions.

```kotlin
class MainActivity : AppCompatActivity() {
// Additional code

override fun onResume() {
super.onResume()
// focus-start
if (Web3Auth.getCustomTabsClosed()) {
Toast.makeText(this, "User closed the browser.", Toast.LENGTH_SHORT).show()
web3Auth.setResultUrl(null)
Web3Auth.setCustomTabsClosed(false)
}
// focus-end
}

// Additional code
}
```

## Initialize Web3Auth

After instantiating Web3Auth, the next step is to initialize it using the `initialize` method. This
method is essential for setting up the SDK, checking for any active sessions, and fetching the
whitelabel configuration from your dashboard.

Once the `initialize` method executes successfully, you can use the `getPrivKey` or
`getEd25519PrivKey` methods to verify if an active session exists. If there is no active session,
these methods will return an empty string; otherwise, they will return the respective private key.

Note that if the API call to fetch the project configuration fails, the method will throw an error.

```kotlin
val sessionResponse: CompletableFuture<Void> = web3Auth.initialize()
sessionResponse.whenComplete { _, error ->
if (error == null) {
// Check for the active session
if(web3Auth.getPrivKey()isNotEmpty()) {
// Active session found
}
// No active session is not present

} else {
// Handle the error
}
}
```
30 changes: 0 additions & 30 deletions docs/sdk/pnp/android/usage.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -366,36 +366,6 @@ val userInfo = web3Auth.getUserInfo()

<EnableMFAMethod />

## Triggering Login exceptions

The `setCustomTabsClosed` method can be used to trigger login exceptions for Android. The Android
SDK uses the custom tabs and from current implementation of chrome custom tab, it's not possible to
add a listener directly to chrome custom tab close button and trigger login exceptions.

Hence, it's necessary to use `setCustomTabsClosed` method in your login screen to trigger
exceptions.

### Usage

```kt
class MainActivity : AppCompatActivity() {
// Additional code

override fun onResume() {
super.onResume()
// focus-start
if (Web3Auth.getCustomTabsClosed()) {
Toast.makeText(this, "User closed the browser.", Toast.LENGTH_SHORT).show()
web3Auth.setResultUrl(null)
Web3Auth.setCustomTabsClosed(false)
}
// focus-end
}

// Additional code
}
```

## Launch Wallet Services

The `launchWalletServices` method launches a WebView which allows you to use the templated wallet UI
Expand Down
84 changes: 81 additions & 3 deletions docs/sdk/pnp/flutter/initialize.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@ import Initialization from "@site/src/common/sdk/pnp/flutter/_initialization.mdx
import TabItem from "@theme/TabItem";
import Tabs from "@theme/Tabs";

After installation, the next step to use Web3Auth is to initialize the SDK. We can initialize the
SDK using `init` method.
After installation, the next step to use Web3Auth is to configure & initialize the SDK.

However, configuring & initializing is a three-step process:

1. [Configure the Web3Auth Instance](#configure-web3auth-instance)
2. [Triggering Login exceptions](#triggering-login-exceptions)
3. [Initialize Web3Auth](#initialize-web3auth)

## Configure Web3Auth instance

Please note that these are the most critical steps where you need to pass on different parameters
according to the preference of your project. Additionally, Whitelabeling and Custom Authentication
Expand Down Expand Up @@ -96,6 +103,77 @@ class Web3AuthOptions {
</TabItem>
</Tabs>

## Initialize Web3AuthFlutter
### Configure Web3AuthFlutter

<Initialization />

## Triggering Login exceptions

The `setCustomTabsClosed` method can be used to trigger login exceptions for Android. For iOS, you
don't need this method to trigger the login exceptions. The Android SDK uses the custom tabs and
from current implementation of chrome custom tab, it's not possible to add a listener directly to
chrome custom tab close button and trigger login exceptions.

Hence, it's necessary to use `setCustomTabsClosed` method in your login screen to trigger
exceptions.

```dart
class LoginScreen extends State<T> with WidgetsBindingObserver {
@override
void initState() {
super.initState();
// focus-next-line
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
super.dispose();
// focus-next-line
WidgetsBinding.instance.removeObserver(this);
}
// focus-start
@override
void didChangeAppLifecycleState(final AppLifecycleState state) {
// This is important to trigger the user cancellation on Android.
if (state == AppLifecycleState.resumed) {
Web3AuthFlutter.setCustomTabsClosed();
}
}
// focus-end
@override
Widget build(BuildContext context) {
// Your UI code
}
}
```

## Initialize Web3Auth

After configuring Web3Auth, the next step is to initialize it using the `initialize` method. This
method is essential for setting up the SDK, checking for any active sessions, and fetching the
whitelabel configuration from your dashboard.

Once the `initialize` method executes successfully, you can use the `getPrivKey` or
`getEd25519PrivKey` methods to verify if an active session exists. If there is no active session,
these methods will return an empty string; otherwise, they will return the respective private key.

Note that if the API call to fetch the project configuration fails, the method will throw an error.

```dart
try {
await Web3AuthFlutter.initialize();
} catch (e) {
// Handle/Swallow Error
}
final privateKey = await Web3AuthFlutter.getPrivKey();
// Check if the session is present or not
if(privateKey.isNotEmpty) {
// Active session present
}
// No active session present
```
57 changes: 0 additions & 57 deletions docs/sdk/pnp/flutter/usage.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -447,63 +447,6 @@ await Web3AuthFlutter.logout();

<EnableMFAMethod />

## Triggering Login exceptions

The `setCustomTabsClosed` method can be used to trigger login exceptions for Android. For iOS, you
don't need this method to trigger the login exceptions. The Android SDK uses the custom tabs and
from current implementation of chrome custom tab, it's not possible to add a listener directly to
chrome custom tab close button and trigger login exceptions.

Hence, it's necessary to use `setCustomTabsClosed` method in your login screen to trigger
exceptions.

### Usage

```dart
class LoginScreen extends State<T> with WidgetsBindingObserver {
@override
void initState() {
super.initState();
// focus-next-line
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
super.dispose();
// focus-next-line
WidgetsBinding.instance.removeObserver(this);
}
// focus-start
@override
void didChangeAppLifecycleState(final AppLifecycleState state) {
// This is important to trigger the user cancellation on Android.
if (state == AppLifecycleState.resumed) {
Web3AuthFlutter.setCustomTabsClosed();
}
}
// focus-end
@override
Widget build(BuildContext context) {
// Your UI code
}
Future<void> _login() async {
try {
await Web3AuthFlutter.login(LoginParams(loginProvider: Provider.google));
} on UserCancelledException {
log("User cancelled.");
} on UnKnownException {
log("Unknown exception occurred");
} catch (e) {
log(e.toString());
}
}
}
```

## Launch Wallet Services

The `launchWalletServices` method launches a WebView which allows you to use the templated wallet UI
Expand Down
2 changes: 1 addition & 1 deletion docs/sdk/pnp/ios/initialize.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ fields of the `W3AInitParams` object.
| `mfaSettings?` | Allows developers to configure the Mfa settings for authentication. It takes `MfaSettings` as a value. |
| `sessionTime?` | It allows developers to configure the session management time. Session Time is in seconds, default is 86400 seconds which is 1 day. `sessionTime` can be max 7 days |

### Initialize Web3Auth Flutter
## Initialize Web3Auth

<Initialization />
2 changes: 1 addition & 1 deletion docs/sdk/pnp/web/modal/account-abstraction.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Please note this is the important step for setting the Web3Auth account abstract
- [Configure Smart Account provider](#configure-smart-account-provider)
- [Configure Bundler](#configure-bundler)
- [Configure Sponsored Paymaster](#sponsored-paymaster)
- [Configure ERC-20 Paymaster](#erc20-paymaster)
- [Configure ERC-20 Paymaster](#erc-20-paymaster)

## Configure Smart Account Provider

Expand Down
2 changes: 1 addition & 1 deletion docs/sdk/pnp/web/no-modal/account-abstraction.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Please note this is the important step for setting the Web3Auth account abstract
- [Configure Smart Account provider](#configure-smart-account-provider)
- [Configure Bundler](#configure-bundler)
- [Configure Sponsored Paymaster](#sponsored-paymaster)
- [Configure ERC-20 Paymaster](#erc20-paymaster)
- [Configure ERC-20 Paymaster](#erc-20-paymaster)

## Configure Smart Account Provider

Expand Down
4 changes: 2 additions & 2 deletions docs/sdk/pnp/web/no-modal/whitelabel.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ Within the Web3Auth Plug and Play No Modal SDK, whitelabeling can happen in 2 di
- [**Auth Adapter**](#auth-adapter): The redirect screens while logging in and constructing the key,
can be customized according to your common as well. For this, you need to pass on the `whiteLabel`
configuration parameter to the [`auth-adapter`](/sdk/pnp/web/adapters/auth).
- [**uiConfig**](/sdk/pnp/web/no-modal/initialize#uiconfig): The UI components can be customized
using the `uiConfig` parameter while initializing the Web3AuthNoModal SDK.
- [**uiConfig**](/sdk/pnp/web/no-modal/initialize#whitelabeling): The UI components can be
customized using the `uiConfig` parameter while initializing the Web3AuthNoModal SDK.

<AuthWhiteLabelConfig />

Expand Down
4 changes: 2 additions & 2 deletions docs/sdk/pnp/web/providers/aa-provider.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Please note this is the important step for setting the Web3Auth account abstract
- [Configure Smart Account provider](#configure-smart-account-provider)
- [Configure Bundler](#configure-bundler)
- [Configure Sponsored Paymaster](#sponsored-paymaster)
- [Configure ERC-20 Paymaster](#erc20-paymaster)
- [Configure ERC-20 Paymaster](#erc-20-paymaster)

## Configure Smart Account Provider

Expand Down Expand Up @@ -313,7 +313,7 @@ provide approve allowance to be used by the paymaster.
To modify the token allowance, you'll need to perform a write operation on the ERC-20 contract. In
the example below, we're using Pimlico, but be sure to update the paymaster and ERC-20 token
addresses according to your specific case. For using ERC-20 Paymaster, refer to the configuration
for [Pimlico's ERC-20 paymaster in the AccountAbstractionProvider](/#erc-20-paymaster).
for [Pimlico's ERC-20 paymaster in the AccountAbstractionProvider](#erc-20-paymaster).

```ts
// Use the same accountAbstractionProvider we created earlier.
Expand Down
9 changes: 0 additions & 9 deletions src/common/sdk/pnp/flutter/_initialization.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,5 @@ Future<void> initWeb3Auth() async {
));
// focus-end
// Just after init method, invoke the initialize function
// to use any active session. If no active session are
// present, the method will throw an error.
try {
await Web3AuthFlutter.initialize();
} catch (e) {
// Handle/Swallow Error
}
}
```
2 changes: 1 addition & 1 deletion src/pages/guides/android-auth0.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private fun signIn() {
```

When connecting, the `login` function takes the LoginParams arguments for the login. See the
[LoginParams](/sdk/pnp/android/usage#arguments) for more details.
[LoginParams](/sdk/pnp/android/usage#parameters) for more details.

#### Sample loginResponse

Expand Down
2 changes: 1 addition & 1 deletion src/pages/guides/android-firebase.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ private fun signIn() {
```

When connecting, the `login` function takes the LoginParams arguments for the login. See the
[LoginParams](/sdk/pnp/android/usage#arguments) for more details.
[LoginParams](/sdk/pnp/android/usage#parameters) for more details.

#### Sample loginResponse

Expand Down
Loading

0 comments on commit 25fa68b

Please sign in to comment.