core: do a healthcheck before logging out user on session revoke

This commit is contained in:
Abdullah Atta
2023-03-17 17:00:15 +05:00
committed by Abdullah Atta
parent 1ee24cde07
commit 3674594832
2 changed files with 43 additions and 5 deletions

View File

@@ -0,0 +1,31 @@
/*
This file is part of the Notesnook project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import hosts from "../utils/constants";
import http from "../utils/http";
export class HealthCheck {
static async isAuthServerHealthy() {
try {
const response = await http.get(`${hosts.AUTH_HOST}/health`);
return response.trim() === "Healthy";
} catch {
return false;
}
}
}

View File

@@ -22,6 +22,7 @@ import http from "../utils/http";
import constants from "../utils/constants"; import constants from "../utils/constants";
import TokenManager from "./token-manager"; import TokenManager from "./token-manager";
import { EV, EVENTS } from "../common"; import { EV, EVENTS } from "../common";
import { HealthCheck } from "./healthcheck";
const ENDPOINTS = { const ENDPOINTS = {
signup: "/users", signup: "/users",
@@ -48,14 +49,20 @@ class UserManager {
this.tokenManager = new TokenManager(storage); this.tokenManager = new TokenManager(storage);
EV.subscribe(EVENTS.userUnauthorized, async (url) => { EV.subscribe(EVENTS.userUnauthorized, async (url) => {
if (url.includes("/connect/token")) return; if (
url.includes("/connect/token") ||
!(await HealthCheck.isAuthServerHealthy())
)
return;
try { try {
await this.tokenManager._refreshToken(true); await this.tokenManager._refreshToken(true);
} catch (e) { } catch (e) {
await this.logout( if (e.message === "invalid_grant" || e.message === "invalid_client") {
false, await this.logout(
`Your token has been revoked. Error: ${e.message}.` false,
); `Your token has been revoked. Error: ${e.message}.`
);
}
} }
}); });
} }