refactor: outsource auth function in all routes

This commit is contained in:
2026-07-10 11:17:31 +02:00
parent ef5fb3e1da
commit 3153076d72
8 changed files with 105 additions and 107 deletions
@@ -1,14 +1,10 @@
import { createFileRoute, redirect } from "@tanstack/react-router";
import { isAuthenticated } from "../../../utils/api/auth";
import { createFileRoute } from "@tanstack/react-router";
import { verifyLogin } from "../../../utils/api/auth";
import { AddProduct } from "../../../pages/AddProduct";
export const Route = createFileRoute("/app/_hiddenLayout/add-product")({
beforeLoad: async () => {
if (!(await isAuthenticated())) {
throw redirect({
to: "/login",
});
}
await verifyLogin();
},
component: RouteComponent,
});
@@ -1,14 +1,10 @@
import { createFileRoute, redirect } from "@tanstack/react-router";
import { isAuthenticated } from "../../../utils/api/auth";
import { createFileRoute } from "@tanstack/react-router";
import { verifyLogin } from "../../../utils/api/auth";
import { Settings } from "../../../pages/Settings";
export const Route = createFileRoute("/app/_hiddenLayout/app-settings")({
beforeLoad: async () => {
if (!(await isAuthenticated())) {
throw redirect({
to: "/login",
});
}
await verifyLogin();
},
component: RouteComponent,
});
@@ -1,14 +1,10 @@
import { createFileRoute, redirect } from "@tanstack/react-router";
import { isAuthenticated } from "../../../utils/api/auth";
import { createFileRoute } from "@tanstack/react-router";
import { verifyLogin } from "../../../utils/api/auth";
import { InventoryPage } from "../../../pages/Inventory";
export const Route = createFileRoute("/app/_hiddenLayout/inventory")({
beforeLoad: async () => {
if (!(await isAuthenticated())) {
throw redirect({
to: "/login",
});
}
await verifyLogin();
},
component: RouteComponent,
});
@@ -1,14 +1,10 @@
import { createFileRoute, redirect } from "@tanstack/react-router";
import { isAuthenticated } from "../../../utils/api/auth";
import { createFileRoute } from "@tanstack/react-router";
import { verifyLogin } from "../../../utils/api/auth";
import { Storages } from "../../../pages/Storages";
export const Route = createFileRoute("/app/_hiddenLayout/storages")({
beforeLoad: async () => {
if (!(await isAuthenticated())) {
throw redirect({
to: "/login",
});
}
await verifyLogin();
},
component: RouteComponent,
});
@@ -1,15 +1,11 @@
import { createFileRoute, redirect } from "@tanstack/react-router";
import { createFileRoute } from "@tanstack/react-router";
import { z } from "zod";
import { isAuthenticated } from "../../../utils/api/auth";
import { verifyLogin } from "../../../utils/api/auth";
import { ViewProduct } from "../../../pages/ViewProduct";
export const Route = createFileRoute("/app/_hiddenLayout/view-product")({
beforeLoad: async () => {
if (!(await isAuthenticated())) {
throw redirect({
to: "/login",
});
}
await verifyLogin();
},
validateSearch: z.object({
product: z.string(),
+1
View File
@@ -6,6 +6,7 @@ export const Route = createFileRoute("/")({
if (!(await isAuthenticated())) {
throw redirect({
to: "/login",
search: { loggedOut: true },
});
} else {
throw redirect({
+4
View File
@@ -1,7 +1,11 @@
import { createFileRoute } from "@tanstack/react-router";
import { LoginCard } from "../components/LoginCard";
import { checkLogin } from "../utils/api/auth.ts";
export const Route = createFileRoute("/login")({
beforeLoad: async () => {
await checkLogin();
},
component: RouteComponent,
});
+31 -18
View File
@@ -1,11 +1,12 @@
import {API_BASE} from "../../config/api.config";
import { API_BASE } from "../../config/api.config";
import Cookies from "js-cookie";
import type {TFunction} from "i18next";
import {fetchSettings} from "./settings";
import type {ChangePasswordIntf} from "../../misc/interfaces";
import {createApiError} from "./apiError";
import type { TFunction } from "i18next";
import { fetchSettings } from "./settings";
import type { ChangePasswordIntf } from "../../misc/interfaces";
import { createApiError } from "./apiError";
import { redirect } from "@tanstack/react-router";
export async function isAuthenticated() {
export const isAuthenticated = async () => {
if (Cookies.get("token")) {
const result = await fetch(`${API_BASE}/users/verify-token`, {
method: "POST",
@@ -23,13 +24,13 @@ export async function isAuthenticated() {
Cookies.remove("token");
return false;
}
};
export async function signInUser(
export const signInUser = async (
username: string,
password: string,
t: TFunction,
) {
) => {
const result = await fetch(`${API_BASE}/users/login`, {
method: "POST",
headers: {
@@ -37,7 +38,7 @@ export async function signInUser(
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify({username, password}),
body: JSON.stringify({ username, password }),
});
const response = await result.json();
@@ -49,17 +50,12 @@ export async function signInUser(
Cookies.set("app-name", settings?.data[0].value);
Cookies.set("currency", settings?.data[1].value);
return {ok: true as const};
return { ok: true as const };
}
Cookies.remove("token");
throw createApiError(response.code, t(response.code || "unknown-error"));
}
export function signOutUser() {
Cookies.remove("token");
return {ok: true as const};
}
};
export const mutatePassword = async (payload: ChangePasswordIntf) => {
const result = await fetch(`${API_BASE}/users/change-password`, {
@@ -78,8 +74,25 @@ export const mutatePassword = async (payload: ChangePasswordIntf) => {
const response = await result.json();
if (response.code === "SU005") {
return {code: response.code};
return { code: response.code };
}
throw createApiError(response.code, "Change password failed");
};
export const verifyLogin = async () => {
if (!(await isAuthenticated())) {
throw redirect({
to: "/login",
search: { loggedOut: true },
});
}
};
export const checkLogin = async () => {
if (await isAuthenticated()) {
throw redirect({
to: "/app/inventory",
});
}
};