From 7e0675ba4d8c41d9e4eb285c1b2f82ddf8037b7c Mon Sep 17 00:00:00 2001 From: Jan Date: Mon, 4 Mar 2024 12:37:04 +0100 Subject: [PATCH 1/3] Initial commit --- .../kotlin/io/github/jan/supabase/gotrue/Auth.kt | 6 ++++++ .../io/github/jan/supabase/gotrue/AuthExtensions.kt | 9 ++++++++- .../io/github/jan/supabase/gotrue/AuthImpl.kt | 13 +++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/GoTrue/src/commonMain/kotlin/io/github/jan/supabase/gotrue/Auth.kt b/GoTrue/src/commonMain/kotlin/io/github/jan/supabase/gotrue/Auth.kt index 8230ff8b2..5936dd7e1 100644 --- a/GoTrue/src/commonMain/kotlin/io/github/jan/supabase/gotrue/Auth.kt +++ b/GoTrue/src/commonMain/kotlin/io/github/jan/supabase/gotrue/Auth.kt @@ -23,6 +23,7 @@ import io.github.jan.supabase.plugins.MainPlugin import io.github.jan.supabase.plugins.SupabasePluginProvider import io.ktor.client.plugins.HttpRequestTimeoutException import kotlinx.coroutines.flow.StateFlow +import kotlinx.serialization.json.JsonObject /** * Plugin to interact with the Supabase Auth API @@ -127,6 +128,11 @@ sealed interface Auth : MainPlugin, CustomSerializationPlugin { config: (C.() -> Unit)? = null ) + /** + * TODO: Docs + */ + suspend fun signInAnonymously(data: JsonObject? = null, captchaToken: String? = null) + /** * Links an OAuth Identity to an existing user. * diff --git a/GoTrue/src/commonMain/kotlin/io/github/jan/supabase/gotrue/AuthExtensions.kt b/GoTrue/src/commonMain/kotlin/io/github/jan/supabase/gotrue/AuthExtensions.kt index 63812881e..36a5e3725 100644 --- a/GoTrue/src/commonMain/kotlin/io/github/jan/supabase/gotrue/AuthExtensions.kt +++ b/GoTrue/src/commonMain/kotlin/io/github/jan/supabase/gotrue/AuthExtensions.kt @@ -1,7 +1,9 @@ package io.github.jan.supabase.gotrue +import io.github.jan.supabase.encodeToJsonElement import io.github.jan.supabase.gotrue.user.UserSession import io.github.jan.supabase.logging.d +import kotlinx.serialization.json.jsonObject internal fun noDeeplinkError(arg: String): Nothing = error(""" Trying to use a deeplink as a redirect url, but no deeplink $arg is set in the GoTrueConfig. @@ -51,4 +53,9 @@ fun Auth.parseSessionFromFragment(fragment: String): UserSession { * @param url The url to parse the session from * @return The parsed session. Note that the user will be null, but you can retrieve it using [Auth.retrieveUser] */ -fun Auth.parseSessionFromUrl(url: String): UserSession = parseSessionFromFragment(url.substringAfter("#")) \ No newline at end of file +fun Auth.parseSessionFromUrl(url: String): UserSession = parseSessionFromFragment(url.substringAfter("#")) + +/** + * TODO: docs + */ +suspend inline fun Auth.signInAnonymously(data: T, captchaToken: String? = null) = signInAnonymously(serializer.encodeToJsonElement(data).jsonObject, captchaToken) \ No newline at end of file diff --git a/GoTrue/src/commonMain/kotlin/io/github/jan/supabase/gotrue/AuthImpl.kt b/GoTrue/src/commonMain/kotlin/io/github/jan/supabase/gotrue/AuthImpl.kt index 40028d85b..bc9912c87 100644 --- a/GoTrue/src/commonMain/kotlin/io/github/jan/supabase/gotrue/AuthImpl.kt +++ b/GoTrue/src/commonMain/kotlin/io/github/jan/supabase/gotrue/AuthImpl.kt @@ -43,6 +43,7 @@ import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.first import kotlinx.coroutines.launch import kotlinx.datetime.Clock +import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.JsonObjectBuilder import kotlinx.serialization.json.buildJsonObject import kotlinx.serialization.json.encodeToJsonElement @@ -111,6 +112,18 @@ internal class AuthImpl( importSession(it, source = SessionSource.SignIn(provider)) }, redirectUrl, config) + override suspend fun signInAnonymously(data: JsonObject?, captchaToken: String?) { + val response = api.postJson("signup", buildJsonObject { + data?.let { put("data", it) } + captchaToken?.let { + putJsonObject("gotrue_meta_security") { + put("captcha_token", captchaToken) + } + } + }) + //TODO: handle response + } + override suspend fun > signUpWith( provider: Provider, redirectUrl: String?, From 10b2b427a6b7c5f1467ab702b92a73b39a9debd8 Mon Sep 17 00:00:00 2001 From: Jan Date: Sun, 10 Mar 2024 11:57:18 +0100 Subject: [PATCH 2/3] Add implementation --- .../commonMain/kotlin/io/github/jan/supabase/gotrue/Auth.kt | 5 ++++- .../kotlin/io/github/jan/supabase/gotrue/AuthImpl.kt | 3 ++- .../kotlin/io/github/jan/supabase/gotrue/SessionStatus.kt | 5 +++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/GoTrue/src/commonMain/kotlin/io/github/jan/supabase/gotrue/Auth.kt b/GoTrue/src/commonMain/kotlin/io/github/jan/supabase/gotrue/Auth.kt index 957aae4ef..f264fae25 100644 --- a/GoTrue/src/commonMain/kotlin/io/github/jan/supabase/gotrue/Auth.kt +++ b/GoTrue/src/commonMain/kotlin/io/github/jan/supabase/gotrue/Auth.kt @@ -129,8 +129,11 @@ sealed interface Auth : MainPlugin, CustomSerializationPlugin { ) /** - * TODO: Docs + * Signs in the user without any credentials. This will create a new user session with a new access token. + * + * If you want to upgrade this anonymous user to a real user, use [linkIdentity] to link an OAuth identity or [modifyUser] to add an email or phone. */ + @SupabaseExperimental suspend fun signInAnonymously(data: JsonObject? = null, captchaToken: String? = null) /** diff --git a/GoTrue/src/commonMain/kotlin/io/github/jan/supabase/gotrue/AuthImpl.kt b/GoTrue/src/commonMain/kotlin/io/github/jan/supabase/gotrue/AuthImpl.kt index 6a1df86b3..8ed060488 100644 --- a/GoTrue/src/commonMain/kotlin/io/github/jan/supabase/gotrue/AuthImpl.kt +++ b/GoTrue/src/commonMain/kotlin/io/github/jan/supabase/gotrue/AuthImpl.kt @@ -121,7 +121,8 @@ internal class AuthImpl( } } }) - //TODO: handle response + val session = response.safeBody() + importSession(session, source = SessionSource.AnonymousSignIn) } override suspend fun > signUpWith( diff --git a/GoTrue/src/commonMain/kotlin/io/github/jan/supabase/gotrue/SessionStatus.kt b/GoTrue/src/commonMain/kotlin/io/github/jan/supabase/gotrue/SessionStatus.kt index bb966ca2d..d7bbf1bd5 100644 --- a/GoTrue/src/commonMain/kotlin/io/github/jan/supabase/gotrue/SessionStatus.kt +++ b/GoTrue/src/commonMain/kotlin/io/github/jan/supabase/gotrue/SessionStatus.kt @@ -51,6 +51,11 @@ sealed interface SessionSource { */ data object Storage : SessionSource + /** + * The session was loaded from an anonymous sign in + */ + data object AnonymousSignIn : SessionSource + /** * The session was loaded from a sign in * @param provider The provider that was used to sign in From 08fff3381657055e7609c9c262bde769f0d4b4a4 Mon Sep 17 00:00:00 2001 From: Jan Date: Sun, 10 Mar 2024 12:01:37 +0100 Subject: [PATCH 3/3] add missing docs --- .../kotlin/io/github/jan/supabase/gotrue/Auth.kt | 3 +++ .../io/github/jan/supabase/gotrue/AuthExtensions.kt | 8 +++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/GoTrue/src/commonMain/kotlin/io/github/jan/supabase/gotrue/Auth.kt b/GoTrue/src/commonMain/kotlin/io/github/jan/supabase/gotrue/Auth.kt index f264fae25..41c3a4b12 100644 --- a/GoTrue/src/commonMain/kotlin/io/github/jan/supabase/gotrue/Auth.kt +++ b/GoTrue/src/commonMain/kotlin/io/github/jan/supabase/gotrue/Auth.kt @@ -132,6 +132,9 @@ sealed interface Auth : MainPlugin, CustomSerializationPlugin { * Signs in the user without any credentials. This will create a new user session with a new access token. * * If you want to upgrade this anonymous user to a real user, use [linkIdentity] to link an OAuth identity or [modifyUser] to add an email or phone. + * + * @param data Extra data for the user + * @param captchaToken The captcha token to use */ @SupabaseExperimental suspend fun signInAnonymously(data: JsonObject? = null, captchaToken: String? = null) diff --git a/GoTrue/src/commonMain/kotlin/io/github/jan/supabase/gotrue/AuthExtensions.kt b/GoTrue/src/commonMain/kotlin/io/github/jan/supabase/gotrue/AuthExtensions.kt index 36a5e3725..cbfb6411c 100644 --- a/GoTrue/src/commonMain/kotlin/io/github/jan/supabase/gotrue/AuthExtensions.kt +++ b/GoTrue/src/commonMain/kotlin/io/github/jan/supabase/gotrue/AuthExtensions.kt @@ -36,6 +36,7 @@ fun Auth.parseSessionFromFragment(fragment: String): UserSession { val type = sessionParts["type"] ?: "" val providerToken = sessionParts["provider_token"] val providerRefreshToken = sessionParts["provider_refresh_token"] + return UserSession( accessToken = accessToken, refreshToken = refreshToken, @@ -56,6 +57,11 @@ fun Auth.parseSessionFromFragment(fragment: String): UserSession { fun Auth.parseSessionFromUrl(url: String): UserSession = parseSessionFromFragment(url.substringAfter("#")) /** - * TODO: docs + * Signs in the user without any credentials. This will create a new user session with a new access token. + * + * If you want to upgrade this anonymous user to a real user, use [Auth.linkIdentity] to link an OAuth identity or [Auth.modifyUser] to add an email or phone. + * + * @param data Extra data for the user + * @param captchaToken The captcha token to use */ suspend inline fun Auth.signInAnonymously(data: T, captchaToken: String? = null) = signInAnonymously(serializer.encodeToJsonElement(data).jsonObject, captchaToken) \ No newline at end of file