From: Adam Dullage Date: Fri, 17 Oct 2025 11:38:49 +0000 (+0100) Subject: Alternative fix for issue highlighted in PR #337 by @kontaxis X-Git-Url: http://git.99rst.org/?a=commitdiff_plain;h=76ea8f08a76f4c6d4fb01fb2b500bf52585b11ba;p=flatnotes.git Alternative fix for issue highlighted in PR #337 by @kontaxis --- diff --git a/client/api.js b/client/api.js index d27d3b5..b1c629e 100644 --- a/client/api.js +++ b/client/api.js @@ -48,8 +48,8 @@ export async function getConfig() { try { const response = await api.get("api/config"); return response.data; - } catch (error) { - return Promise.reject(error); + } catch (response) { + return Promise.reject(response); } } @@ -65,6 +65,15 @@ export async function getToken(username, password, totp) { } } +export async function authCheck() { + try { + const response = await api.get("api/auth-check"); + return response.data; + } catch (response) { + return Promise.reject(response); + } +} + export async function getNotes(term, sort, order, limit) { try { const response = await api.get("api/search", { diff --git a/client/router.js b/client/router.js index 81b2314..e86dbc9 100644 --- a/client/router.js +++ b/client/router.js @@ -2,6 +2,8 @@ import * as constants from "./constants.js"; import { createRouter, createWebHistory } from "vue-router"; +import { authCheck } from "./api.js"; + const router = createRouter({ history: createWebHistory(""), routes: [ @@ -39,6 +41,27 @@ const router = createRouter({ ], }); +// Check the user is authenticated on first navigation (unless going to login) +let authChecked = false; +router.beforeEach(async (to) => { + if (authChecked || to.name === "login") { + return; + } + try { + await authCheck(); + return; + } catch (error) { + if (error.response && error.response.status === 401) { + return { + name: "login", + query: { [constants.params.redirect]: to.fullPath }, + }; + } + } finally { + authChecked = true; + } +}); + router.afterEach((to) => { let title = "flatnotes"; if (to.name === "note") { diff --git a/server/main.py b/server/main.py index 1cc0701..2bca5ee 100644 --- a/server/main.py +++ b/server/main.py @@ -42,7 +42,7 @@ def root(title: str = ""): # endregion -# region Login +# region Auth if global_config.auth_type not in [AuthType.NONE, AuthType.READ_ONLY]: @router.post("/api/token", response_model=Token) @@ -55,6 +55,13 @@ if global_config.auth_type not in [AuthType.NONE, AuthType.READ_ONLY]: ) +@router.get("/api/auth-check", dependencies=auth_deps) +def auth_check() -> str: + """A lightweight endpoint that simply returns 'OK' if the user is + authenticated.""" + return "OK" + + # endregion