Skip to content

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

fix: force logout user on 401 unauthorized error

thecodrr2021fe56d5f9
diff --git a/packages/core/api/token-manager.js b/packages/core/api/token-manager.js
index b35a41a3b..f5d13c977 100644
--- a/packages/core/api/token-manager.js
+++ b/packages/core/api/token-manager.js
@@ -18,11 +18,6 @@ class TokenManager {
constructor(storage) {
this._storage = storage;
this._refreshTokenMutex = withTimeout(new Mutex(), 10 * 1000);
- EV.subscribe(EVENTS.refreshToken, async (url) => {
- if (url && url.includes(ENDPOINTS.token)) return;
-
- await this._refreshToken(true);
- });
}
async getToken(renew = true, forceRenew = false) {
diff --git a/packages/core/api/user-manager.js b/packages/core/api/user-manager.js
index a7d772e81..af2c96c32 100644
--- a/packages/core/api/user-manager.js
+++ b/packages/core/api/user-manager.js
@@ -29,6 +29,10 @@ class UserManager {
this._storage = storage;
this._db = db;
this.tokenManager = new TokenManager(storage);
+
+ EV.subscribe(EVENTS.logoutUser, async (reason) => {
+ await this.logout(true, reason);
+ });
}
async init() {
@@ -258,7 +262,7 @@ class UserManager {
if (!new_password) throw new Error("New password is required.");
const attachmentsKey = await this.getAttachmentsKey();
- data.encryptionKey = data.encryptionKey || await this.getEncryptionKey();
+ data.encryptionKey = data.encryptionKey || (await this.getEncryptionKey());
await this._db.outbox.add(type, data, async () => {
await this._db.sync(true, true);
diff --git a/packages/core/common.js b/packages/core/common.js
index 014bc6ad1..612e036f8 100644
--- a/packages/core/common.js
+++ b/packages/core/common.js
@@ -47,7 +47,7 @@ export const EVENTS = {
appRefreshRequested: "app:refreshRequested",
noteRemoved: "note:removed",
tokenRefreshed: "token:refreshed",
- refreshToken: "token:refresh",
+ logoutUser: "user:logout",
attachmentsLoading: "attachments:loading",
attachmentDeleted: "attachment:deleted",
mediaAttachmentDownloaded: "attachments:mediaDownloaded",
diff --git a/packages/core/utils/http.js b/packages/core/utils/http.js
index 86bf346f9..20e51c230 100644
--- a/packages/core/utils/http.js
+++ b/packages/core/utils/http.js
@@ -62,7 +62,7 @@ async function handleResponse(response) {
if (response.ok) return await response.text();
else if (response.status === 401) {
- EV.publish(EVENTS.refreshToken, response.url);
+ EV.publish(EVENTS.logoutUser, `401 unauthorized.`);
throw new Error("Unauthorized.");
} else
throw new Error(