Skip to content

Commit

Permalink
Merge pull request #388 from supabase-community/fix-linking
Browse files Browse the repository at this point in the history
Fix OAuthProvider linking
  • Loading branch information
jan-tennert authored Dec 14, 2023
2 parents dd66f12 + add276b commit d809e43
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,12 @@ sealed interface Auth : MainPlugin<AuthConfig>, CustomSerializationPlugin {
/**
* Unlinks an OAuth Identity from an existing user.
* @param identityId The id of the OAuth identity
* @param updateLocalUser Whether to delete the identity from the local user or not
*/
@SupabaseExperimental
suspend fun unlinkIdentity(
identityId: String
identityId: String,
updateLocalUser: Boolean = true
)

/**
Expand Down Expand Up @@ -349,6 +351,13 @@ sealed interface Auth : MainPlugin<AuthConfig>, CustomSerializationPlugin {
*/
fun currentIdentitiesOrNull() = currentUserOrNull()?.identities

/**
* Blocks the current coroutine until the plugin is initialized.
*
* This will make sure that the [SessionStatus] is set to [SessionStatus.Authenticated], [SessionStatus.NotAuthenticated] or [SessionStatus.NetworkError].
*/
suspend fun awaitInitialization()

companion object : SupabasePluginProvider<AuthConfig, Auth> {

override val key = "auth"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import io.ktor.client.call.body
import io.ktor.client.request.parameter
import io.ktor.client.statement.HttpResponse
import io.ktor.client.statement.bodyAsText
import io.ktor.client.statement.request
import io.ktor.http.HttpMethod
import io.ktor.http.HttpStatusCode
import kotlinx.coroutines.CoroutineScope
Expand All @@ -36,14 +37,13 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
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
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
import kotlinx.serialization.json.put
import kotlinx.serialization.json.putJsonObject
import kotlin.math.floor
Expand Down Expand Up @@ -123,10 +123,10 @@ internal class AuthImpl(
redirectUrl = redirectUrl,
getUrl = {
val url = oAuthUrl(provider, it, "user/identities/authorize", config)
val data = api.rawRequest(url) {
val response = api.rawRequest(url) {
method = HttpMethod.Get
}.body<JsonObject>()
data["url"]?.jsonPrimitive?.content ?: error("No url found in response")
}
response.request.url.toString()
},
onSessionSuccess = {
importSession(it)
Expand All @@ -135,8 +135,14 @@ internal class AuthImpl(
}

@SupabaseExperimental
override suspend fun unlinkIdentity(identityId: String) {
override suspend fun unlinkIdentity(identityId: String, updateLocalUser: Boolean) {
api.delete("user/identities/$identityId")
if (updateLocalUser) {
val session = currentSessionOrNull() ?: return
val newUser = session.user?.copy(identities = session.user.identities?.filter { it.identityId != identityId })
val newSession = session.copy(user = newUser)
_sessionStatus.value = SessionStatus.Authenticated(newSession)
}
}

override suspend fun retrieveSSOUrl(
Expand Down Expand Up @@ -531,6 +537,10 @@ internal class AuthImpl(
sessionJob = null
}

override suspend fun awaitInitialization() {
sessionStatus.first { it !is SessionStatus.LoadingFromStorage }
}

}

@SupabaseInternal
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ kotlin.experimental.tryK2=false
org.jetbrains.compose.experimental.uikit.enabled=true
org.jetbrains.compose.experimental.jscanvas.enabled=true

supabase-version = 2.0.0
supabase-version = 2.0.1

0 comments on commit d809e43

Please sign in to comment.