Check auth off of our API client
This commit is contained in:
parent
2856587aca
commit
b081dcf6d5
3 changed files with 32 additions and 1 deletions
24
ts/client.ts
Normal file
24
ts/client.ts
Normal file
|
|
@ -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;
|
||||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
};
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue