diff --git a/backend/src/api/auth.ts b/backend/src/api/auth.ts index 01568742..2a5d3445 100644 --- a/backend/src/api/auth.ts +++ b/backend/src/api/auth.ts @@ -167,7 +167,7 @@ export function apiTokenAuthHandlers() { return [ passport.authenticate("api-token", { failWithError: true }), (err, req: Request, res: Response, next) => { - return res.json({ error: err.message }); + return res.status(401).json({ error: err.message }); }, ]; } diff --git a/dashboard/src/api.ts b/dashboard/src/api.ts index bc37d3b8..5782aa47 100644 --- a/dashboard/src/api.ts +++ b/dashboard/src/api.ts @@ -30,6 +30,11 @@ function buildQueryString(params: QueryParamObject) { export function request(resource, fetchOpts: RequestInit = {}) { return fetch(`${apiUrl}/${resource}`, fetchOpts).then(async res => { if (!res.ok) { + if (res.status === 401) { + RootStore.dispatch("auth/expiredLogin"); + return; + } + const body = await res.json(); throw new ApiError(res.statusText, body, res.status, res); } diff --git a/dashboard/src/main.ts b/dashboard/src/main.ts index dcd78f64..46bb6f5e 100644 --- a/dashboard/src/main.ts +++ b/dashboard/src/main.ts @@ -22,6 +22,7 @@ if (window.location.pathname !== "/") { const errorMessages = { noAccess: "No dashboard access. If you think this is a mistake, please contact your server owner.", + expiredLogin: "Dashboard login expired. Please log in again.", }; const errorMessageElem = document.createElement("div"); diff --git a/dashboard/src/store/auth.ts b/dashboard/src/store/auth.ts index 84897b32..4e24658c 100644 --- a/dashboard/src/store/auth.ts +++ b/dashboard/src/store/auth.ts @@ -71,6 +71,11 @@ export const AuthStore: Module = { await post("auth/logout"); await dispatch("clearApiKey"); }, + + async expiredLogin({ dispatch }) { + await dispatch("clearApiKey"); + window.location.assign("/?error=expiredLogin"); + }, }, mutations: {