Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/sdk/core-kit/sfa-flutter/install.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ displayed_sidebar: sdk
description: "Web3Auth Core Kit Single Factor Auth Flutter SDK - Install | Documentation - Web3Auth"
---

## `single_factor_auth_flutter`
## [single_factor_auth_flutter](https://pub.dev/packages/single_factor_auth_flutter)

To install the `single_factor_auth_flutter` package, you have two options. You can either manually add the package in the `pubspec.yaml` file, or you
can use the `flutter pub add` command.
To install the `single_factor_auth_flutter` package, you have two options. You can either manually
add the package in the `pubspec.yaml` file, or you can use the `flutter pub add` command.

Add `single_factor_auth_flutter` as a dependency to your `pubspec.yaml`.

Expand All @@ -17,7 +17,7 @@ dependencies:
single_factor_auth_flutter: ^2.0.1
```

Add `single_factor_auth_flutter` using `flutter pub add command`.
Add `single_factor_auth_flutter` using `flutter pub add` command.

```sh
flutter pub add single_factor_auth_flutter
Expand Down
2 changes: 1 addition & 1 deletion docs/sdk/core-kit/sfa-ios/install.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ description: "Installing Web3Auth SFA iOS SDK | Documentation - Web3Auth"

1. In Xcode, with your app project open, navigate to **File > Add Packages**.

1. When prompted, add the tKey iOS SDK repository:
1. When prompted, add the single-factor-auth-swift iOS SDK repository:

```sh
https://github.com/Web3Auth/single-factor-auth-swift
Expand Down
261 changes: 261 additions & 0 deletions src/pages/guides/sfa-android-firebase.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,261 @@
---
title: Using Firebase with Web3Auth Android SFA SDK
image: "guides/banners/sfa-android-firebase.png"
description:
Web3Auth Single Factor Authentication (SFA) Android SDK with Firebase enables secure blockchain
authentication using Email and Password login. The process involves setting up Firebase and
Web3Auth, initializing the SDK, and interacting with the Ethereum blockchain.
type: guide
tags: [mobile, android, ethereum, SFA]
date: May 19, 2024
author: Web3Auth Team
---

import SEO from "@site/src/components/SEO";
import TabItem from "@theme/TabItem";
import Tabs from "@theme/Tabs";

<SEO
title="Using Firebase with Web3Auth Android SFA SDK"
description="Learn how to use Web3Auth Single Factor Auth with Firebase in your Android Application."
image="https://web3auth.io/docs/guides/banners/sfa-android-firebase.png"
slug="/guides/sfa-android-firebase"
/>

In this guide, we'll talk about how we can use Web3Auth Single Factor Auth with Firebase in your
Android application.

As an overview, the guide is quite simple, with functionality to log in, display user details, and
perform blockchain interactions. The signing of the blockchain transactions is done through the
Web3Auth embedded wallet. You can check out the infrastructure docs,
["Web3Auth Wallet Management Infrastructure"](/docs/infrastructure/infrastructure) for a high-level
overview of the Web3Auth architecture and implementation. For those who want to skip straight to the
code, you can find it on
[GitHub](https://github.com/Web3Auth/web3auth-core-kit-examples/tree/main/single-factor-auth-android/sfa-android-quick-start).

## How to set up Web3Auth Dashboard

If you haven't already, sign up on the Web3Auth platform. It is free and gives you access to the
Web3Auth's base plan. After the basic setup, explore other features and functionalities offered by
the Web3Auth Dashboard. It includes custom verifiers, whitelabeling, analytics, and more. Head to
[Web3Auth's documentation](/docs/dashboard-setup) page for detailed instructions on setting up the
Web3Auth Dashboard.

## How to set up Firebase for Android

If you haven't already setup the Firebase for your Android app, please setup the Firebase, as it's
the prerequisites for the guide. Head to the
[Firebase's documentation](https://firebase.google.com/docs/android/setup) for the details
instructions.

## How to set up Custom verifier

Once, you have set up the Web3Auth Dashboard, created a new project, and set up Firebase, it's time
to create a Custom Verifier for your Firebase application. We already have detail instructions on
how to create a Custom Verifier for Firebase, please head to our
[documentation](https://web3auth.io/docs/auth-provider-setup/authentication-service-providers#firebase).

## Integrating Web3Auth SFA in Android

Once, you have set up the Custom Verifier, it's time to integrate Web3Auth in your Android
application. For the implementation, we'll use the "single-factor-auth-android". This SDK
facilitates integration with Web3Auth. This way you can easily manage embedded wallet in your
Android application.

### Installation

In your module-level `build.gradle` or `settings.gradle` file, add JitPack repository:

```groovy
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
// highlight-next-line
maven { url "https://jitpack.io" } // <-- Add this line
}
}
```

Once, you have added the JitPack repository, then in your app-level `build.gradle` dependencies
section, add the `single-factor-auth-android`.

```groovy
dependencies {
// ...
// highlight-next-line
implementation 'com.github.web3auth:single-factor-auth-android:0.0.6'
}
```

For the prerequisites, and other mandatory configuration of the SDK, please head to our
[installation documentation](/docs/sdk/core-kit/sfa-android/install).

### Initialization

After successfully installing the package, the next step is to initialize `SingleFactorAuth` in your
Android app. This sets up the necessary configurations using Web3Auth network and prepares the SDK.
[Learn more about SingleFactorAuth Initialization](/docs/sdk/core-kit/sfa-android/initialize).

```kotlin
class MainActivity : AppCompatActivity() {
private lateinit var singleFactorAuth: SingleFactorAuth
private lateinit var singleFactorAuthArgs: SingleFactorAuthArgs

// Additional code

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

// highlight-start
singleFactorAuthArgs = SingleFactorAuthArgs(TorusNetwork.TESTNET)
singleFactorAuth = SingleFactorAuth(singleFactorAuthArgs)
// highlight-end
}

// Additional code
}
```

### Session Management

To check whether the user is authenticated, you can use the `initialize` method. For a user already
authenticated, the result would be a non-nullable `TorusKey`. You can navigate to different views
based on the result.

```kotlin
val sessionResponse: CompletableFuture<TorusKey> = singleFactorAuth.initialize(
this.applicationContext
)

sessionResponse.whenComplete {
torusKey, error ->
if (torusKey != null) {
publicAddress = torusKey?.publicAddress.toString()
println("""Private Key: ${torusKey.privateKey?.toString(16)}""".trimIndent())
} else {
Log.d("MainActivity_SFA", error.message ?: "Something went wrong")
}
}
```

### Authentication

If the user is not authenticated, you should utilize the `getKey` method. For the guide, we will add
Email Password login using Firebase. The `getKey` method is pretty straightforward in
SingleFactorAuth and takes `LoginParams` as input. After successfully logging in, the method will
return the `TorusKey`.

Learn more about [SingleFactorAuth LoginParams](/docs/sdk/core-kit/sfa-android/usage#loginparams).
To more about Firebase login methods, please
[checkout the Firebase documentation](https://firebase.google.com/docs/auth/android/start).

```kotlin
private var auth: FirebaseAuthauth = Firebase.auth

// Take the input for email and password from user
auth.signInWithEmailAndPassword("android@firebase.com", "Android@Web3Auth")
.addOnCompleteListener(this) {
task ->
if (task.isSuccessful) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithEmail:success")
val user = auth.currentUser

// Try to get a fresh idToken
user!!.getIdToken(true).addOnSuccessListener {
result ->
val idToken = result.token

Log.d(TAG, "GetTokenResult result = $idToken")

if (idToken != null) {
val sub = user!!.id

val loginParams = LoginParams(
// Replace with your custom verifier name
"web3auth-firebase-examples",
sub,
idToken
)

try {
// Save the TorusKey for future use to interact with Blockchain.
// highlight-start
torusKey = singleFactorAuth.getKey(
loginParams,
this.applicationContext,
86400
).get()
// highlight-end

} catch (e: ExecutionException) {
e.printStackTrace()
} catch (e: InterruptedException) {
e.printStackTrace()
}

publicAddress = torusKey?.publicAddress.toString()

println("""Private Key: ${torusKey?.privateKey?.toString(16)}""".trimIndent())
println("""Public Address: $publicAddress""".trimIndent())

// Additional code
};
}
} else {
// Upon failur, display a message to the user.
Log.w(TAG, "signInWithEmail:failure", task.exception)
Toast.makeText(
baseContext, "Authentication failed.",
Toast.LENGTH_SHORT
).show()
}
}
```

## Set up Blockchain Providers

Once we have successfully authenticated the user, the next step would be to fetch the user details,
retrieve wallet address and prepare blockchain providers for interactions. For this guide, we are
supporting only Ethereum ecosystem, but the general idea can be used for any blockchain ecosystem.

For interacting with ethereum chains, we'll use the [web3j](https://github.com/hyperledger/web3j)
SDK. For installation, in your app-level build.gradle's dependencies section, add the web3j
dependency.

```groovy
dependencies {
// ...
implementation 'org.web3j:core:4.8.7-android'
}
```

After successfully installing SDK, it's time to create `Credentials` instance to retrive user's EOA
address, and interact with blockchain. To retrive the user's private key, we'll use the `TorusKey`
instance.

```kotlin
// highlight-next-line
val credentials: Credentials = Credentials.create(torusKey!.privateKey!.toString(16))

// User's EOA address
Log.d(TAG, credentials.address)
```

To retrive user's balance, and interacting with Blockcahin, you can follow our detailed guide on how
to [interact with EVM chain guides](/docs/connect-blockchain/evm/ethereum/android#get-balance).
Since, you already have created `Credentials` instance, and retrived the user's EOA address, you can
skip that part. To interact with Solana blockchain, you can checkout our
[Solana blockchain guide](/docs/connect-blockchain/solana/android).

## Conclusion

Voila, you have learned how to use Web3Auth SFA SDK with Android application.

If you are interested in learning more about SFA SDK, please checkout our
[documentation for Android SFA SDK](/docs/sdk/core-kit/sfa-android/). You can find the code used for
the guide on our
[examples repo](https://github.com/Web3Auth/web3auth-core-kit-examples/tree/main/single-factor-auth-android/sfa-android-quick-start).
Loading