Skip to content

Commit f2f1c87

Browse files
authored
[RKOTLIN-1083] Remove deprecated user methods (#1750)
1 parent 5c7c2a0 commit f2f1c87

File tree

8 files changed

+34
-58
lines changed

8 files changed

+34
-58
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ This release will bump the Realm file format 24. Opening a file with an older fo
77
* Removed property `RealmLog.level`. Log levels can be set with `RealmLog.setLevel`. (Issue [#1691](https://github.com/realm/realm-kotlin/issues/1691) [JIRA](https://jira.mongodb.org/browse/RKOTLIN-1038))
88
* Removed `LogConfiguration`. Log levels and custom loggers can be set with `RealmLog`. (Issue [#1691](https://github.com/realm/realm-kotlin/issues/1691) [JIRA](https://jira.mongodb.org/browse/RKOTLIN-1038))
99
* Removed deprecated `io.realm.kotlin.types.ObjectId`. Use `org.mongodb.kbson.BsonObjectId` or its type alias `org.mongodb.kbson.ObjectId` instead. (Issue [#1749](https://github.com/realm/realm-kotlin/issues/1749) [JIRA](https://jira.mongodb.org/browse/RKOTLIN-1082))
10+
* [Sync] Removed deprecated methods `User.identity` and `User.provider`, user identities can be accessed with the already existing `User.identities`. (Issue [#1751](https://github.com/realm/realm-kotlin/issues/1751) [JIRA](https://jira.mongodb.org/browse/RKOTLIN-1083))
11+
* [Sync] `App.allUsers` does no longer return a map, but only a list of users known locally. (Issue [#1751](https://github.com/realm/realm-kotlin/issues/1751) [JIRA](https://jira.mongodb.org/browse/RKOTLIN-1083))
1012

1113
### Enhancements
1214
* Support for RealmLists and RealmDictionaries in `RealmAny`. (Issue [#1434](https://github.com/realm/realm-kotlin/issues/1434))

packages/library-sync/src/commonMain/kotlin/io/realm/kotlin/mongodb/App.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,9 @@ public interface App {
106106
* Returns all known users that are either [User.State.LOGGED_IN] or [User.State.LOGGED_OUT].
107107
* Only users that at some point logged into this device will be returned.
108108
*
109-
* @return a map of user identifiers and users known locally. User identifiers will match what
110-
* is returned by [User.identity].
109+
* @return a list of locally known users.
111110
*/
112-
public fun allUsers(): Map<String, User>
111+
public fun allUsers(): List<User>
113112

114113
/**
115114
* Log in as a user with the given credentials associated with an authentication provider.

packages/library-sync/src/commonMain/kotlin/io/realm/kotlin/mongodb/User.kt

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,6 @@ public interface User {
5252
*/
5353
public val state: State
5454

55-
/**
56-
* The server id of the user.
57-
*
58-
* This property has been deprecated in favor of [id] and will be replaced in a future release.
59-
*/
60-
@Deprecated("Use `User.id` instead", replaceWith = ReplaceWith("id"))
61-
public val identity: String
62-
6355
/**
6456
* The server id of the user.
6557
*/
@@ -79,13 +71,6 @@ public interface User {
7971
*/
8072
public val identities: List<UserIdentity>
8173

82-
/**
83-
* Returns the provider type used to log the user in.
84-
* If a user logs out, the authentication provider last used to log the user in will still be returned.
85-
*/
86-
@Deprecated("Users might have multiple providers. This will return the provider of the first identity of the user", ReplaceWith("identities"))
87-
public val provider: AuthenticationProvider
88-
8974
/**
9075
* Returns the current access token for the user.
9176
* If a user logs out, an empty access token is returned.

packages/library-sync/src/commonMain/kotlin/io/realm/kotlin/mongodb/internal/AppImpl.kt

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,11 @@ public class AppImpl(
134134
?.let { UserImpl(it, this) }
135135
override val sync: Sync by lazy { SyncImpl(nativePointer) }
136136

137-
override fun allUsers(): Map<String, User> {
138-
val nativeUsers: List<RealmUserPointer> =
139-
RealmInterop.realm_app_get_all_users(nativePointer)
140-
val map = mutableMapOf<String, User>()
141-
nativeUsers.map { ptr: RealmUserPointer ->
142-
val user = UserImpl(ptr, this)
143-
map[user.identity] = user
144-
}
145-
return map
146-
}
137+
override fun allUsers(): List<User> =
138+
RealmInterop.realm_app_get_all_users(nativePointer)
139+
.map { ptr: RealmUserPointer ->
140+
UserImpl(ptr, this)
141+
}
147142

148143
override suspend fun login(credentials: Credentials): User {
149144
// suspendCoroutine doesn't allow freezing callback capturing continuation

packages/library-sync/src/commonMain/kotlin/io/realm/kotlin/mongodb/internal/UserImpl.kt

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import io.realm.kotlin.internal.interop.RealmInterop
2020
import io.realm.kotlin.internal.interop.RealmUserPointer
2121
import io.realm.kotlin.internal.interop.sync.CoreUserState
2222
import io.realm.kotlin.internal.util.use
23-
import io.realm.kotlin.mongodb.AuthenticationProvider
2423
import io.realm.kotlin.mongodb.Credentials
2524
import io.realm.kotlin.mongodb.Functions
2625
import io.realm.kotlin.mongodb.User
@@ -41,16 +40,11 @@ public class UserImpl(
4140
override val state: User.State
4241
get() = fromCoreState(RealmInterop.realm_user_get_state(nativePointer))
4342

44-
// TODO Can maybe fail, but we could also cache the return value?
45-
override val identity: String
46-
get() = id
4743
override val id: String
4844
get() = RealmInterop.realm_user_get_identity(nativePointer)
4945
override val loggedIn: Boolean
5046
get() = RealmInterop.realm_user_is_logged_in(nativePointer)
51-
@Deprecated("Property not stable, users might have multiple providers.", ReplaceWith("User.identities"))
52-
override val provider: AuthenticationProvider
53-
get() = identities.first().provider
47+
5448
override val accessToken: String
5549
get() = RealmInterop.realm_user_get_access_token(nativePointer)
5650
override val refreshToken: String
@@ -187,13 +181,13 @@ public class UserImpl(
187181
if (other == null || this::class != other::class) return false
188182

189183
other as UserImpl
184+
if (id != (other.id)) return false
190185

191-
if (identity != (other.identity)) return false
192186
return app.configuration == other.app.configuration
193187
}
194188

195189
override fun hashCode(): Int {
196-
var result = identity.hashCode()
190+
var result = id.hashCode()
197191
result = 31 * result + app.configuration.appId.hashCode()
198192
return result
199193
}

packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/AppTests.kt

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ import kotlin.test.assertNotEquals
6161
import kotlin.test.assertNull
6262
import kotlin.test.assertSame
6363
import kotlin.test.assertTrue
64-
import kotlin.test.fail
6564

6665
class AppTests {
6766

@@ -121,7 +120,7 @@ class AppTests {
121120
@Suppress("LoopWithTooManyJumpStatements")
122121
@Test
123122
fun login_invalidCredentialsThrows() = runBlocking {
124-
for (provider in AuthenticationProvider.values()) {
123+
for (provider in AuthenticationProvider.entries) {
125124
when (provider) {
126125
AuthenticationProvider.ANONYMOUS -> {
127126
// No user input, so invalid credentials are not possible.
@@ -164,50 +163,52 @@ class AppTests {
164163
@Test
165164
fun allUsers() = runBlocking {
166165
assertEquals(0, app.allUsers().size)
166+
167167
val user1 = app.login(Credentials.anonymous())
168168
var allUsers = app.allUsers()
169169
assertEquals(1, allUsers.size)
170-
assertTrue(allUsers.containsKey(user1.identity))
171-
assertEquals(user1, allUsers[user1.identity])
170+
assertTrue(allUsers.contains(user1))
172171

173172
// Only 1 anonymous user exists, so logging in again just returns the old one
174173
val user2 = app.login(Credentials.anonymous())
175174
allUsers = app.allUsers()
176175
assertEquals(1, allUsers.size)
177-
assertTrue(allUsers.containsKey(user2.identity))
176+
assertTrue(allUsers.contains(user2))
178177

179178
val user3: User = app.asTestApp.createUserAndLogIn(TestHelper.randomEmail(), "123456")
180179
allUsers = app.allUsers()
181180
assertEquals(2, allUsers.size)
182-
assertTrue(allUsers.containsKey(user3.identity))
181+
assertTrue(allUsers.contains(user3))
183182

184183
// Logging out users that registered with email/password will just put them in LOGGED_OUT state
185184
user3.logOut()
186185
allUsers = app.allUsers()
187186
assertEquals(2, allUsers.size)
188-
assertTrue(allUsers.containsKey(user3.identity))
189-
assertEquals(User.State.LOGGED_OUT, allUsers[user3.identity]!!.state)
187+
assertTrue(allUsers.contains(user3))
188+
assertEquals(User.State.LOGGED_OUT, user3.state)
190189

191190
// Logging out anonymous users will remove them completely
192191
user1.logOut()
193192
allUsers = app.allUsers()
194193
assertEquals(1, allUsers.size)
195-
assertFalse(allUsers.containsKey(user1.identity))
194+
assertTrue(allUsers.contains(user3))
195+
assertFalse(allUsers.contains(user2))
196+
assertFalse(allUsers.contains(user1))
196197
}
197198

198199
@Test
199200
fun allUsers_retrieveRemovedUser() = runBlocking {
200201
val user1: User = app.login(Credentials.anonymous())
201-
val allUsers: Map<String, User> = app.allUsers()
202+
val allUsers = app.allUsers()
202203
assertEquals(1, allUsers.size)
203204
user1.logOut()
204205
assertEquals(1, allUsers.size)
205-
val userCopy: User = allUsers[user1.identity] ?: fail("Could not find user")
206+
val userCopy: User = allUsers.first()
206207
assertEquals(user1, userCopy)
207208
assertEquals(User.State.REMOVED, userCopy.state)
208209
assertTrue(app.allUsers().isEmpty())
209210
}
210-
//
211+
211212
// @Test
212213
// fun switchUser() {
213214
// val user1: User = app.login(Credentials.anonymous())

packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/CredentialsTests.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class CredentialsTests {
7272

7373
@Test
7474
fun allCredentials() {
75-
AuthenticationProvider.values().flatMap {
75+
AuthenticationProvider.entries.flatMap {
7676
when (it) {
7777
AuthenticationProvider.ANONYMOUS -> listOf(it to anonymous())
7878
AuthenticationProvider.EMAIL_PASSWORD -> listOf(it to emailPassword())
@@ -103,7 +103,7 @@ class CredentialsTests {
103103

104104
@Test
105105
fun allCredentials_emptyInputThrows() {
106-
for (value in AuthenticationProvider.values()) {
106+
for (value in AuthenticationProvider.entries) {
107107
assertFailsWith<IllegalArgumentException>("$value failed") { // No arguments should be allow
108108
when (value) {
109109
AuthenticationProvider.ANONYMOUS -> throw IllegalArgumentException("Do nothing, no arguments")
@@ -267,29 +267,29 @@ class CredentialsTests {
267267
assertNotNull(firstUser)
268268
val reusedUser = app.login(Credentials.anonymous())
269269
assertNotNull(reusedUser)
270-
assertEquals(firstUser.identity, reusedUser.identity)
270+
assertEquals(firstUser, reusedUser)
271271

272272
val newAnonymousUser1 = app.login(Credentials.anonymous(false))
273273
assertNotNull(newAnonymousUser1)
274-
assertNotEquals(firstUser.identity, newAnonymousUser1.identity)
274+
assertNotEquals(firstUser, newAnonymousUser1)
275275

276276
val newAnonymousUser2 = app.login(Credentials.anonymous(false))
277277
assertNotNull(newAnonymousUser2)
278-
assertNotEquals(newAnonymousUser1.identity, newAnonymousUser2.identity)
278+
assertNotEquals(newAnonymousUser1, newAnonymousUser2)
279279
}
280280
}
281281

282282
@Test
283283
fun loginUsingCredentials() {
284284
runBlocking {
285-
AuthenticationProvider.values().forEach { provider ->
285+
AuthenticationProvider.entries.forEach { provider ->
286286
when (provider) {
287287
AuthenticationProvider.ANONYMOUS -> {
288288
val reusableUser = app.login(Credentials.anonymous())
289289
assertNotNull(reusableUser)
290290
val nonReusableUser = app.login(Credentials.anonymous(false))
291291
assertNotNull(nonReusableUser)
292-
assertNotEquals(reusableUser.identity, nonReusableUser.identity)
292+
assertNotEquals(reusableUser, nonReusableUser)
293293
}
294294
AuthenticationProvider.API_KEY -> {
295295
// Log in, create an API key, log out, log in with the key, compare users

packages/test-sync/src/commonTest/kotlin/io/realm/kotlin/test/mongodb/common/UserTests.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,10 @@ class UserTests {
125125
fun getProviderType() = runBlocking {
126126
val email = randomEmail()
127127
val emailUser = createUserAndLogin(email, "123456")
128-
assertEquals(AuthenticationProvider.EMAIL_PASSWORD, emailUser.provider)
128+
assertEquals(AuthenticationProvider.EMAIL_PASSWORD, emailUser.identities.first().provider)
129129
emailUser.logOut()
130130
// AuthenticationProvider is not removed once user is logged out
131-
assertEquals(AuthenticationProvider.EMAIL_PASSWORD, emailUser.provider)
131+
assertEquals(AuthenticationProvider.EMAIL_PASSWORD, emailUser.identities.first().provider)
132132
}
133133

134134
@Test

0 commit comments

Comments
 (0)