Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

feat: handle "not authorized" error by logging the user out

thecodrrea51fa7aa046
diff --git a/packages/core/api/index.js b/packages/core/api/index.js
index 9b87016dd..af8b15a70 100644
--- a/packages/core/api/index.js
+++ b/packages/core/api/index.js
@@ -40,8 +40,6 @@ class Database {
this._validate();
this.user = new User(this);
- await this.user.sync();
-
this.syncer = new Sync(this);
this.vault = new Vault(this);
this.conflicts = new Conflicts(this);
@@ -62,6 +60,8 @@ class Database {
this.delta = await Content.new(this, "delta", false);
/** @type {Content} */
this.text = await Content.new(this, "text", false);
+
+ await this.user.sync();
}
async _onUserStateChanged(user) {
diff --git a/packages/core/collections/collection.js b/packages/core/collections/collection.js
index 63ea03412..bf74ee8d0 100644
--- a/packages/core/collections/collection.js
+++ b/packages/core/collections/collection.js
@@ -8,7 +8,7 @@ class Collection {
if (collection.init) await collection.init();
if (collection._collection.clear)
- db.ev.subscribe("clear", () => collection._collection.clear());
+ db.ev.subscribe("user:loggedOut", () => collection._collection.clear());
return collection;
}
diff --git a/packages/core/models/user.js b/packages/core/models/user.js
index ab6790983..cdc220a55 100644
--- a/packages/core/models/user.js
+++ b/packages/core/models/user.js
@@ -13,13 +13,18 @@ export default class User {
async sync() {
var user = await this.get();
if (!user) return;
- var serverUser = await authRequest.call(
- this,
- "users",
- undefined,
- true,
- true
- );
+ try {
+ var serverUser = await authRequest.call(
+ this,
+ "users",
+ undefined,
+ true,
+ true
+ );
+ } catch (e) {
+ if (e.message.includes("not authorized")) await this.logout();
+ else throw e;
+ }
await this.set({
...user,
@@ -90,10 +95,8 @@ export default class User {
}
logout() {
- this._db.ev.publish("clear");
-
// propogate event
- this._db.ev.publish("user:loggedOut", null);
+ this._db.ev.publish("user:loggedOut");
return this._context.clear();
}