diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d9f1d3d75..cb5729bdf8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,8 +7,7 @@ * None ### Fixed -* ([#????](https://github.com/realm/realm-js/issues/????), since v?.?.?) -* None +* Added missing implementation of `User.state` and changed the `UserState` enum values to use pascal case to conform to the v11 implementation (except for `UserState.Active` that we now deprecate in favor of `UserState.LoggedIn`). [#5686](https://github.com/realm/realm-js/issues/5686) ### Compatibility * React Native >= v0.71.0 diff --git a/integration-tests/tests/src/tests/sync/user.ts b/integration-tests/tests/src/tests/sync/user.ts index 12fa66cc6c..033a8ce341 100644 --- a/integration-tests/tests/src/tests/sync/user.ts +++ b/integration-tests/tests/src/tests/sync/user.ts @@ -23,6 +23,7 @@ import { randomVerifiableEmail, } from "../../utils/generators"; import { KJUR } from "jsrsasign"; +import { UserState } from "realm"; function expectIsUSer(user: Realm.User) { expect(user).to.not.be.undefined; @@ -320,6 +321,26 @@ describe.skipIf(environment.missingServer, "User", () => { expect(user2).to.be.undefined; expect(didFail).to.be.true; }); + + describe("state", () => { + it("can fetch state when logged in with email password", async function (this: AppContext & RealmContext) { + expect(this.app.currentUser).to.be.null; + const user = await registerAndLogInEmailUser(this.app); + expect(user.state).to.equal(UserState.LoggedIn); + }); + + it("can fetch state when logged out with email password", async function (this: AppContext & RealmContext) { + const user = await registerAndLogInEmailUser(this.app); + await user.logOut(); + expect(user.state).to.equal(UserState.LoggedOut); + }); + + it("can fetch state when removed with email password", async function (this: AppContext & RealmContext) { + const user = await registerAndLogInEmailUser(this.app); + await this.app.removeUser(user); + expect(user.state).to.equal(UserState.Removed); + }); + }); }); }); diff --git a/packages/realm/src/app-services/User.ts b/packages/realm/src/app-services/User.ts index 12f7243bbf..a61ddb404b 100644 --- a/packages/realm/src/app-services/User.ts +++ b/packages/realm/src/app-services/User.ts @@ -43,12 +43,17 @@ export type UserChangeCallback = () => void; * The state of a user. */ export enum UserState { - /** Authenticated and available to communicate with services. */ + /** + * Authenticated and available to communicate with services. + * @deprecated Will be removed in v13. Please use {@link LoggedIn} + */ Active = "active", + /** Authenticated and available to communicate with services. */ + LoggedIn = "LoggedIn", /** Logged out, but ready to be logged in. */ - LoggedOut = "logged-out", + LoggedOut = "LoggedOut", /** Removed from the app entirely. */ - Removed = "removed", + Removed = "Removed", } /** @@ -141,7 +146,17 @@ export class User< * The state of the user. */ get state(): UserState { - throw new Error("Not yet implemented"); + const state = this.internal.state; + switch (state) { + case binding.SyncUserState.LoggedIn: + return UserState.LoggedIn; + case binding.SyncUserState.LoggedOut: + return UserState.LoggedOut; + case binding.SyncUserState.Removed: + return UserState.Removed; + default: + throw new Error(`Unsupported SyncUserState value: ${state}`); + } } /**