diff --git a/ts/client.ts b/ts/client.ts new file mode 100644 index 00000000..e03f1751 --- /dev/null +++ b/ts/client.ts @@ -0,0 +1,24 @@ +// src/api/axios.js or similar +import axios from 'axios'; +import router from '@/router'; + +const apiClient = axios.create({ + baseURL: '/api', + withCredentials: true +}); + +// Response interceptor to catch auth failures +apiClient.interceptors.response.use( + (response) => response, + (error) => { + if (error.response && error.response.status === 401) { + // Session expired or not authenticated + router.push('/login'); + } + return Promise.reject(error); + } +); +apiClient.isAuthenticated = () => { + return true; +} +export default apiClient; diff --git a/ts/router.ts b/ts/router.ts index 10e73c72..0c722762 100644 --- a/ts/router.ts +++ b/ts/router.ts @@ -19,6 +19,7 @@ import Planning from "./view/Planning.vue"; import Review from "./view/Review.vue"; import Signin from "./view/Signin.vue"; import Sudo from "./view/Sudo.vue"; +import apiClient from "@/client"; const routes: RouteRecordRaw[] = [ { @@ -135,6 +136,7 @@ const router = createRouter({ history: createWebHistory("/"), routes, }); + // Global navigation guard router.beforeEach(async (to, from, next) => { const requiresAuth = to.matched.some(record => record.meta.requiresAuth); @@ -142,7 +144,7 @@ router.beforeEach(async (to, from, next) => { if (requiresAuth) { try { // Check if user is authenticated (could be an API call) - const isAuthenticated = await checkAuth(); + const isAuthenticated = await apiClient.isAuthenticated(); if (!isAuthenticated) { next('/signin'); } else { diff --git a/ts/store/user.ts b/ts/store/user.ts index b6014837..75c14b1f 100644 --- a/ts/store/user.ts +++ b/ts/store/user.ts @@ -81,6 +81,10 @@ export const useUserStore = defineStore("user", () => { } } + async function isAuthenticated(): boolean { + console.log("pretend check user auth"); + return true; + } return { // State display_name, @@ -95,5 +99,6 @@ export const useUserStore = defineStore("user", () => { username, // Actions fetchUser, + isAuthenticated, }; });