Skip to content

Commit

Permalink
Merge branch 'feat/update-session-manager-changes' into feat/login-us…
Browse files Browse the repository at this point in the history
…ing-webview

# Conflicts:
#	core/src/main/java/com/web3auth/core/Web3Auth.kt
#	core/src/main/java/com/web3auth/core/types/WebViewResultCallback.kt
  • Loading branch information
Gaurav Goel committed Nov 22, 2024
2 parents 707eba5 + 225eed9 commit 1d56aba
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
2 changes: 1 addition & 1 deletion core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ dependencies {
implementation 'org.web3j:core:4.8.8-android'

//session-manager-sdk
implementation 'com.github.grvgoel81:session-manager-android:7.2.0'
implementation 'com.github.Web3Auth:session-manager-android:3.0.0'

implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.2.0-alpha01"

Expand Down
26 changes: 19 additions & 7 deletions core/src/main/java/com/web3auth/core/Web3Auth.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResu
private lateinit var loginCompletableFuture: CompletableFuture<Web3AuthResponse>
private lateinit var enableMfaCompletableFuture: CompletableFuture<Boolean>
private lateinit var signMsgCF: CompletableFuture<SignResponse>
private var context = context

private var web3AuthResponse: Web3AuthResponse? = null
private var web3AuthOption = web3AuthOptions
Expand Down Expand Up @@ -168,15 +167,16 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResu
fetchProjectConfig(context).whenComplete { _, err ->
if (err == null) {
//authorize session
sessionManager.setSessionId(SessionManager.getSessionIdFromStorage())
this.authorizeSession(web3AuthOption.redirectUrl.toString(), context)
.whenComplete { resp, error ->
runOnUIThread {
if (error == null) {
web3AuthResponse = resp
initializeCf.complete(null)
} else {
initializeCf.completeExceptionally(error)
}
initializeCf.complete(null)
}
}
} else {
Expand Down Expand Up @@ -220,7 +220,8 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResu
val sessionId = sessionResponse.sessionId

if (sessionId.isNotBlank() && sessionId.isNotEmpty()) {
sessionManager.saveSessionId(sessionId)
SessionManager.saveSessionIdToStorage(sessionId)
sessionManager.setSessionId(sessionId)

//Rehydrate Session
this.authorizeSession(web3AuthOption.redirectUrl.toString(), context)
Expand All @@ -235,7 +236,10 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResu
throwLoginError(ErrorCode.SOMETHING_WENT_WRONG)
throwEnableMFAError(ErrorCode.SOMETHING_WENT_WRONG)
} else {
web3AuthResponse?.sessionId?.let { sessionManager.saveSessionId(it) }
web3AuthResponse?.sessionId?.let {
SessionManager.saveSessionIdToStorage(it)
sessionManager.setSessionId(it)
}

if (web3AuthResponse?.userInfo?.dappShare?.isNotEmpty() == true) {
KeyStoreManagerUtils.encryptData(
Expand Down Expand Up @@ -275,7 +279,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResu
loginParams.dappShare = share
}
}
CustomChromeTabsActivity.webViewResultCallback = this

//login
processRequest("login", loginParams, context)

Expand All @@ -293,6 +297,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResu
val sessionResponse: CompletableFuture<Boolean> =
sessionManager.invalidateSession(context)
sessionResponse.whenComplete { _, error ->
SessionManager.deleteSessionIdFromStorage()
runOnUIThread {
if (error == null) {
logoutCompletableFuture.complete(null)
Expand Down Expand Up @@ -438,9 +443,11 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResu
* @return A CompletableFuture<String> representing the asynchronous operation, containing the login ID.
*/
private fun getLoginId(jsonObject: JSONObject, context: Context): CompletableFuture<String> {
val sessionId = SessionManager.generateRandomSessionKey()
sessionManager.setSessionId(sessionId)
return sessionManager.createSession(
jsonObject.toString(),
context
context,
)
}

Expand All @@ -457,7 +464,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResu
context: Context
): CompletableFuture<Void> {
val launchWalletServiceCF: CompletableFuture<Void> = CompletableFuture()
val sessionId = sessionManager.getSessionId()
val sessionId = SessionManager.getSessionIdFromStorage()
if (sessionId.isNotBlank()) {
val sdkUrl = Uri.parse(web3AuthOption.walletSdkUrl)

Expand Down Expand Up @@ -523,6 +530,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResu
): CompletableFuture<SignResponse> {
signMsgCF = CompletableFuture()
WebViewActivity.webViewResultCallback = this

val sessionId = sessionManager.getSessionId()
if (sessionId.isNotBlank()) {
val sdkUrl = Uri.parse(web3AuthOption.walletSdkUrl)
Expand Down Expand Up @@ -725,5 +733,9 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions, context: Context) : WebViewResu
throwEnableMFAError(ErrorCode.SOMETHING_WENT_WRONG)
}
}

override fun onWebViewCancelled() {
signMsgCF.completeExceptionally(Exception("User cancelled the operation."))
}
}

8 changes: 5 additions & 3 deletions core/src/main/java/com/web3auth/core/WebViewActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,11 @@ class WebViewActivity : AppCompatActivity() {
}

override fun onBackPressed() {
when {
webView.canGoBack() -> webView.goBack()
else -> super.onBackPressed()
if (webView.canGoBack()) {
webView.goBack()
} else {
webViewResultCallback?.onWebViewCancelled()
super.onBackPressed()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package com.web3auth.core.types
interface WebViewResultCallback {
fun onSignResponseReceived(signResponse: SignResponse?)
fun onSessionResponseReceived(sessionResponse: SessionResponse?)

fun onWebViewCancelled()
}

0 comments on commit 1d56aba

Please sign in to comment.