Skip to content

Commit

Permalink
fix linking and add utility method
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-tennert committed Dec 14, 2023
1 parent 37b43f9 commit 3b515e8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 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() ?: error("No url found in response")
},
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

0 comments on commit 3b515e8

Please sign in to comment.