Skip to content

Commit 898c25b

Browse files
authored
Merge pull request #922 from hieuwu/master
Fix null result when sign up with Email provider
2 parents cd2cf5b + 3e5dd89 commit 898c25b

File tree

2 files changed

+74
-12
lines changed

2 files changed

+74
-12
lines changed

Auth/src/commonMain/kotlin/io/github/jan/supabase/auth/providers/builtin/DefaultAuthProvider.kt

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package io.github.jan.supabase.auth.providers.builtin
22

33
import io.github.jan.supabase.SupabaseClient
4-
import io.github.jan.supabase.annotations.SupabaseExperimental
54
import io.github.jan.supabase.annotations.SupabaseInternal
5+
import io.github.jan.supabase.auth.Auth
66
import io.github.jan.supabase.auth.AuthImpl
77
import io.github.jan.supabase.auth.FlowType
88
import io.github.jan.supabase.auth.auth
@@ -12,6 +12,7 @@ import io.github.jan.supabase.auth.providers.AuthProvider
1212
import io.github.jan.supabase.auth.putCodeChallenge
1313
import io.github.jan.supabase.auth.redirectTo
1414
import io.github.jan.supabase.auth.user.UserSession
15+
import io.github.jan.supabase.logging.w
1516
import io.github.jan.supabase.putJsonObject
1617
import io.github.jan.supabase.supabaseJson
1718
import io.ktor.client.call.body
@@ -20,6 +21,7 @@ import kotlinx.serialization.Serializable
2021
import kotlinx.serialization.json.JsonObject
2122
import kotlinx.serialization.json.buildJsonObject
2223
import kotlinx.serialization.json.decodeFromJsonElement
24+
import kotlinx.serialization.json.jsonObject
2325

2426
/**
2527
* A default authentication provider
@@ -65,7 +67,6 @@ sealed interface DefaultAuthProvider<C, R> : AuthProvider<C, R> {
6567
}
6668
}
6769

68-
@OptIn(SupabaseExperimental::class)
6970
override suspend fun signUp(
7071
supabaseClient: SupabaseClient,
7172
onSuccess: suspend (UserSession) -> Unit,
@@ -95,10 +96,16 @@ sealed interface DefaultAuthProvider<C, R> : AuthProvider<C, R> {
9596
redirectUrl?.let { redirectTo(it) }
9697
}
9798
val json = response.body<JsonObject>()
98-
if(json.containsKey("access_token")) {
99-
val userSession = supabaseJson.decodeFromJsonElement<UserSession>(json)
100-
onSuccess(userSession)
101-
return null
99+
if (json.containsKey("access_token")) {
100+
runCatching {
101+
val userSession = supabaseJson.decodeFromJsonElement<UserSession>(json)
102+
onSuccess(userSession)
103+
val userJson = json["user"]?.jsonObject ?: buildJsonObject { }
104+
return decodeResult(userJson)
105+
}.onFailure { exception ->
106+
Auth.logger.w(exception) { "Failed to decode user info" }
107+
return null
108+
}
102109
}
103110
return decodeResult(json)
104111
}

Auth/src/commonTest/kotlin/AuthApiTest.kt

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class AuthRequestTest {
4343
}
4444

4545
@Test
46-
fun testSignUpWithEmailNoAutoconfirm() {
46+
fun testSignUpWithEmailNoAutoConfirm() {
4747
runTest {
4848
val expectedEmail = "[email protected]"
4949
val expectedPassword = "password"
@@ -79,7 +79,43 @@ class AuthRequestTest {
7979
}
8080

8181
@Test
82-
fun testSignUpWithEmailAutoconfirm() {
82+
fun testSignUpWithEmailAutoConfirm() {
83+
runTest {
84+
val expectedEmail = "[email protected]"
85+
val expectedPassword = "password"
86+
val captchaToken = "captchaToken"
87+
val userData = buildJsonObject {
88+
put("key", "value")
89+
}
90+
val client = createMockedSupabaseClient(configuration = configuration) {
91+
val body = it.body.toJsonElement().jsonObject
92+
val metaSecurity = body["gotrue_meta_security"]!!.jsonObject
93+
assertMethodIs(HttpMethod.Post, it.method)
94+
assertPathIs("/signup", it.url.pathAfterVersion())
95+
assertEquals(expectedEmail, body["email"]?.jsonPrimitive?.content)
96+
assertEquals(expectedPassword, body["password"]?.jsonPrimitive?.content)
97+
assertEquals(captchaToken, metaSecurity["captcha_token"]?.jsonPrimitive?.content)
98+
assertEquals(userData, body["data"]!!.jsonObject)
99+
containsCodeChallenge(body)
100+
respondJson(
101+
sampleSessionWithUserData(email = "[email protected]", phone = "+1234567890")
102+
)
103+
}
104+
val user = client.auth.signUpWith(Email) {
105+
email = expectedEmail
106+
password = expectedPassword
107+
this.captchaToken = captchaToken
108+
data = userData
109+
}
110+
assertNotNull(user)
111+
assertEquals(expectedEmail, user?.email, "Email should be equal")
112+
assertNotNull(client.auth.currentSessionOrNull(), "Session should not be null")
113+
assertEquals(client.auth.sessionSource(), SessionSource.SignUp(Email))
114+
}
115+
}
116+
117+
@Test
118+
fun testSignUpWithEmailAutoConfirmWithoutUserData() {
83119
runTest {
84120
val expectedEmail = "[email protected]"
85121
val expectedPassword = "password"
@@ -114,7 +150,7 @@ class AuthRequestTest {
114150
}
115151

116152
@Test
117-
fun testSignUpWithPhoneAutoconfirm() {
153+
fun testSignUpWithPhoneAutoConfirm() {
118154
runTest {
119155
val expectedPhone = "+1234567890"
120156
val expectedPassword = "password"
@@ -133,7 +169,7 @@ class AuthRequestTest {
133169
assertEquals(userData, body["data"]!!.jsonObject)
134170
containsCodeChallenge(body)
135171
respondJson(
136-
sampleUserSession()
172+
sampleSessionWithUserData()
137173
)
138174
}
139175
val user = client.auth.signUpWith(Phone) {
@@ -142,14 +178,14 @@ class AuthRequestTest {
142178
this.captchaToken = captchaToken
143179
data = userData
144180
}
145-
assertNull(user)
181+
assertNotNull(user)
146182
assertNotNull(client.auth.currentSessionOrNull(), "Session should not be null")
147183
assertEquals(client.auth.sessionSource(), SessionSource.SignUp(Phone))
148184
}
149185
}
150186

151187
@Test
152-
fun testSignUpWithPhoneNoAutoconfirm() {
188+
fun testSignUpWithPhoneNoAutoConfirm() {
153189
runTest {
154190
val expectedPhone = "+1234567890"
155191
val expectedPassword = "password"
@@ -690,6 +726,25 @@ class AuthRequestTest {
690726
}
691727
""".trimIndent()
692728

729+
private fun sampleSessionWithUserData(email: String? = null, phone: String? = null) = """
730+
{
731+
"id": "id",
732+
"aud": "aud",
733+
"email": "$email",
734+
"phone": "$phone",
735+
"access_token": "token",
736+
"refresh_token": "refresh",
737+
"token_type": "bearer",
738+
"expires_in": 3600,
739+
"user": {
740+
"id": "id",
741+
"aud": "aud",
742+
"email": "$email",
743+
"phone": "$phone"
744+
}
745+
}
746+
""".trimIndent()
747+
693748
private fun containsCodeChallenge(body: JsonObject) {
694749
assertNotNull(body["code_challenge"])
695750
assertEquals(PKCEConstants.CHALLENGE_METHOD, body["code_challenge_method"]?.jsonPrimitive?.content)

0 commit comments

Comments
 (0)