Skip to content

Commit bbff689

Browse files
Merge pull request #52 from Web3Auth/feat/add-userinfo-method
Exposed getUserInfo() method
2 parents fb2a4fa + adc36d3 commit bbff689

File tree

5 files changed

+67
-12
lines changed

5 files changed

+67
-12
lines changed

app/src/main/java/com/web3auth/app/MainActivity.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
5757
reRender(loginResponse)
5858
println("PrivKey: " + web3Auth.getPrivkey())
5959
println("ed25519PrivKey: " + web3Auth.getEd25519PrivKey())
60+
println("Web3Auth UserInfo" + web3Auth.getUserInfo())
6061
} else {
6162
Log.d("MainActivity_Web3Auth", error.message ?: "Something went wrong")
6263
}
@@ -137,6 +138,7 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
137138
reRender(loginResponse)
138139
println("PrivKey: " + web3Auth.getPrivkey())
139140
println("ed25519PrivKey: " + web3Auth.getEd25519PrivKey())
141+
println("Web3Auth UserInfo" + web3Auth.getUserInfo())
140142
} else {
141143
Log.d("MainActivity_Web3Auth", error.message ?: "Something went wrong")
142144
}

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<resources>
22
<string name="app_name">Torus Web3Auth</string>
3-
<string name="web3auth_project_id">BASdZxp5tWqPGkzbOU4VMJsnz8QOpJ2pyxf6kZcuxI8AJ1gsOw5MgbW96RpAAx_Kk-5p4rhOopPhCa9kR_9mKpE</string>
3+
<string name="web3auth_project_id">BEaGnq-mY0ZOXk2UT1ivWUe0PZ_iJX4Vyb6MtpOp7RMBu_6ErTrATlfuK3IaFcvHJr27h6L1T4owkBH6srLphIw</string>
44
<string name="sign_in">Sign in</string>
55
<string name="not_logged_in">Not logged in</string>
66
<string name="sign_out">Sign out</string>

core/src/main/java/com/web3auth/core/Web3Auth.kt

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) {
154154
if (web3AuthResponse.error?.isNotBlank() == true) {
155155
loginCompletableFuture.completeExceptionally(
156156
UnKnownException(
157-
web3AuthResponse.error ?: "Something went wrong"
157+
web3AuthResponse.error ?: Web3AuthError.getError(ErrorCode.SOMETHING_WENT_WRONG)
158158
)
159159
)
160160
}
@@ -225,7 +225,6 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) {
225225
messageObj,
226226
ShareMetadata::class.java
227227
)
228-
println("shareMetadata$shareMetadata")
229228

230229
KeyStoreManagerUtils.savePreferenceData(
231230
KeyStoreManagerUtils.EPHEM_PUBLIC_Key,
@@ -260,7 +259,9 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) {
260259
Handler(Looper.getMainLooper()).postDelayed(10) {
261260
sessionCompletableFuture.completeExceptionally(
262261
UnKnownException(
263-
web3AuthResponse.error ?: "Something went wrong"
262+
web3AuthResponse.error ?: Web3AuthError.getError(
263+
ErrorCode.SOMETHING_WENT_WRONG
264+
)
264265
)
265266
)
266267
}
@@ -334,23 +335,39 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) {
334335
}
335336

336337
fun getPrivkey(): String {
337-
val privKey: String = if (web3AuthOption.useCoreKitKey == true) {
338-
web3AuthResponse.coreKitKey.toString()
338+
val privKey: String = if (web3AuthResponse == null) {
339+
throw Error(Web3AuthError.getError(ErrorCode.NOUSERFOUND))
339340
} else {
340-
web3AuthResponse.privKey.toString()
341+
if (web3AuthOption.useCoreKitKey == true) {
342+
web3AuthResponse.coreKitEd25519PrivKey.toString()
343+
} else {
344+
web3AuthResponse.ed25519PrivKey.toString()
345+
}
341346
}
342347
return privKey
343348
}
344349

345350
fun getEd25519PrivKey(): String {
346-
val ed25519Key: String = if (web3AuthOption.useCoreKitKey == true) {
347-
web3AuthResponse.coreKitEd25519PrivKey.toString()
351+
val ed25519Key: String = if (web3AuthResponse == null) {
352+
throw Error(Web3AuthError.getError(ErrorCode.NOUSERFOUND))
348353
} else {
349-
web3AuthResponse.ed25519PrivKey.toString()
354+
if (web3AuthOption.useCoreKitKey == true) {
355+
web3AuthResponse.coreKitEd25519PrivKey.toString()
356+
} else {
357+
web3AuthResponse.ed25519PrivKey.toString()
358+
}
350359
}
351360
return ed25519Key
352361
}
353362

363+
fun getUserInfo(): UserInfo? {
364+
if (web3AuthResponse == null || web3AuthResponse.userInfo == null) {
365+
throw Error(Web3AuthError.getError(ErrorCode.NOUSERFOUND))
366+
} else {
367+
return web3AuthResponse.userInfo
368+
}
369+
}
370+
354371
fun logout(
355372
redirectUrl: Uri? = null,
356373
appState: String? = null
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.web3auth.core.types
2+
3+
object Web3AuthError {
4+
5+
fun getError(errorCode: ErrorCode): String {
6+
return when (errorCode) {
7+
ErrorCode.NOUSERFOUND -> {
8+
"No user found, please login again!"
9+
}
10+
ErrorCode.ENCODING_ERROR -> {
11+
"Encoding Error"
12+
}
13+
ErrorCode.DECODING_ERROR -> {
14+
"Decoding Error"
15+
}
16+
ErrorCode.SOMETHING_WENT_WRONG -> {
17+
"Something went wrong!"
18+
}
19+
ErrorCode.RUNTIME_ERROR -> {
20+
"Runtime Error"
21+
}
22+
ErrorCode.APP_CANCELLED -> {
23+
"App Cancelled"
24+
}
25+
}
26+
}
27+
}
28+
29+
enum class ErrorCode {
30+
NOUSERFOUND,
31+
ENCODING_ERROR,
32+
DECODING_ERROR,
33+
RUNTIME_ERROR,
34+
APP_CANCELLED,
35+
SOMETHING_WENT_WRONG,
36+
}

core/src/main/java/com/web3auth/core/types/Web3AuthResponse.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import androidx.annotation.Keep
44

55
@Keep
66
data class Web3AuthResponse(
7-
var privKey: String? = null,
8-
var ed25519PrivKey: String? = null,
7+
val privKey: String? = null,
8+
val ed25519PrivKey: String? = null,
99
val userInfo: UserInfo? = null,
1010
val error: String? = null,
1111
val sessionId: String? = null,

0 commit comments

Comments
 (0)