Skip to content

Commit

Permalink
feat: update example and add for sessionId check
Browse files Browse the repository at this point in the history
Signed-off-by: Gaurav Goel <[email protected]>
  • Loading branch information
grvgoel81 authored and Gaurav Goel committed Sep 2, 2024
1 parent b7699fd commit 5ebe8cf
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
18 changes: 7 additions & 11 deletions app/src/main/java/com/web3auth/sfaexample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,20 @@ class MainActivity : AppCompatActivity() {
singleFactorAuth = SingleFactorAuth(sfaParams, this)
loginParams = LoginParams(TEST_VERIFIER, TORUS_TEST_EMAIL, idToken)

// Consider exposing getSessionId() to avoid this try..catch in all implementations of this SDK
try {
// Already has done login and has sessionId stored.
singleFactorAuth.initialize(this.applicationContext)
} catch (ex: java.lang.Exception) {
// Try login and create new session which will then be stored
singleFactorAuth.getKey(loginParams, this)
singleFactorAuth.initialize(this.applicationContext)
if (singleFactorAuth.isSessionIdExists()) {
val torusSFAKey = singleFactorAuth.initialize(this.applicationContext)
val text = "Private Key: ${torusSFAKey.getPrivateKey()}"
tv.text = text
}
}

private fun getTorusKey() {
val idToken = JwtUtils.generateIdToken(TORUS_TEST_EMAIL)
loginParams = LoginParams(TEST_VERIFIER, TORUS_TEST_EMAIL, idToken)
val TorusSFAKey =
val torusSFAKey =
singleFactorAuth.getKey(loginParams, this.applicationContext)
if (TorusSFAKey != null) {
val text = "Private Key: ${TorusSFAKey.getPrivateKey()}"
if (torusSFAKey != null) {
val text = "Private Key: ${torusSFAKey.getPrivateKey()}"
tv.text = text
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ package com.web3auth.singlefactorauth
import android.content.Context
import com.google.gson.GsonBuilder
import com.web3auth.session_manager_android.SessionManager
import com.web3auth.singlefactorauth.types.*
import com.web3auth.singlefactorauth.types.ErrorCode
import com.web3auth.singlefactorauth.types.LoginParams
import com.web3auth.singlefactorauth.types.SFAError
import com.web3auth.singlefactorauth.types.SingleFactorAuthArgs
import com.web3auth.singlefactorauth.types.TorusSFAKey
import com.web3auth.singlefactorauth.types.TorusSubVerifierInfo
import org.json.JSONObject
import org.torusresearch.fetchnodedetails.FetchNodeDetails
import org.torusresearch.fetchnodedetails.types.NodeDetails
Expand Down Expand Up @@ -49,6 +54,10 @@ class SingleFactorAuth(sfaParams: SingleFactorAuthArgs, ctx: Context) {
}
}

fun isSessionIdExists(): Boolean {
return sessionManager.getSessionId().isNotEmpty()
}

fun getTorusKey(
loginParams: LoginParams
): TorusKey? {
Expand Down

0 comments on commit 5ebe8cf

Please sign in to comment.