Skip to content

Commit

Permalink
Implement User.state (#5712)
Browse files Browse the repository at this point in the history
* Implement 'User.state'.

* Add CHANGELOG entry.

* Re-add 'UserState.Active' as deprecated.

* Update CHANGELOG entry.

* Update CHANGELOG entry.
  • Loading branch information
elle-j authored Apr 17, 2023
1 parent bb74833 commit 301b6ce
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
* None

### Fixed
* <How to hit and notice issue? what was the impact?> ([#????](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
Expand Down
21 changes: 21 additions & 0 deletions integration-tests/tests/src/tests/sync/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
});
});
});
});

Expand Down
23 changes: 19 additions & 4 deletions packages/realm/src/app-services/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}

/**
Expand Down Expand Up @@ -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}`);
}
}

/**
Expand Down

0 comments on commit 301b6ce

Please sign in to comment.