Skip to content

Commit 76ea8f0

Browse files
committed
Alternative fix for issue highlighted in PR #337 by @kontaxis
1 parent e1e1c44 commit 76ea8f0

3 files changed

Lines changed: 42 additions & 3 deletions

File tree

client/api.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ export async function getConfig() {
4848
try {
4949
const response = await api.get("api/config");
5050
return response.data;
51-
} catch (error) {
52-
return Promise.reject(error);
51+
} catch (response) {
52+
return Promise.reject(response);
5353
}
5454
}
5555

@@ -65,6 +65,15 @@ export async function getToken(username, password, totp) {
6565
}
6666
}
6767

68+
export async function authCheck() {
69+
try {
70+
const response = await api.get("api/auth-check");
71+
return response.data;
72+
} catch (response) {
73+
return Promise.reject(response);
74+
}
75+
}
76+
6877
export async function getNotes(term, sort, order, limit) {
6978
try {
7079
const response = await api.get("api/search", {

client/router.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import * as constants from "./constants.js";
22

33
import { createRouter, createWebHistory } from "vue-router";
44

5+
import { authCheck } from "./api.js";
6+
57
const router = createRouter({
68
history: createWebHistory(""),
79
routes: [
@@ -39,6 +41,27 @@ const router = createRouter({
3941
],
4042
});
4143

44+
// Check the user is authenticated on first navigation (unless going to login)
45+
let authChecked = false;
46+
router.beforeEach(async (to) => {
47+
if (authChecked || to.name === "login") {
48+
return;
49+
}
50+
try {
51+
await authCheck();
52+
return;
53+
} catch (error) {
54+
if (error.response && error.response.status === 401) {
55+
return {
56+
name: "login",
57+
query: { [constants.params.redirect]: to.fullPath },
58+
};
59+
}
60+
} finally {
61+
authChecked = true;
62+
}
63+
});
64+
4265
router.afterEach((to) => {
4366
let title = "flatnotes";
4467
if (to.name === "note") {

server/main.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def root(title: str = ""):
4242
# endregion
4343

4444

45-
# region Login
45+
# region Auth
4646
if global_config.auth_type not in [AuthType.NONE, AuthType.READ_ONLY]:
4747

4848
@router.post("/api/token", response_model=Token)
@@ -55,6 +55,13 @@ def token(data: Login):
5555
)
5656

5757

58+
@router.get("/api/auth-check", dependencies=auth_deps)
59+
def auth_check() -> str:
60+
"""A lightweight endpoint that simply returns 'OK' if the user is
61+
authenticated."""
62+
return "OK"
63+
64+
5865
# endregion
5966

6067

0 commit comments

Comments
 (0)