mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-23 23:19:40 +01:00
core: do a healthcheck before logging out user on session revoke
This commit is contained in:
committed by
Abdullah Atta
parent
1ee24cde07
commit
3674594832
31
packages/core/api/healthcheck.js
Normal file
31
packages/core/api/healthcheck.js
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,7 @@ import http from "../utils/http";
|
||||
import constants from "../utils/constants";
|
||||
import TokenManager from "./token-manager";
|
||||
import { EV, EVENTS } from "../common";
|
||||
import { HealthCheck } from "./healthcheck";
|
||||
|
||||
const ENDPOINTS = {
|
||||
signup: "/users",
|
||||
@@ -48,14 +49,20 @@ class UserManager {
|
||||
this.tokenManager = new TokenManager(storage);
|
||||
|
||||
EV.subscribe(EVENTS.userUnauthorized, async (url) => {
|
||||
if (url.includes("/connect/token")) return;
|
||||
if (
|
||||
url.includes("/connect/token") ||
|
||||
!(await HealthCheck.isAuthServerHealthy())
|
||||
)
|
||||
return;
|
||||
try {
|
||||
await this.tokenManager._refreshToken(true);
|
||||
} catch (e) {
|
||||
await this.logout(
|
||||
false,
|
||||
`Your token has been revoked. Error: ${e.message}.`
|
||||
);
|
||||
if (e.message === "invalid_grant" || e.message === "invalid_client") {
|
||||
await this.logout(
|
||||
false,
|
||||
`Your token has been revoked. Error: ${e.message}.`
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user