Skip to content

A self-hosted media server and file management system

fix: multiple session expired errors

aminnausind7ba397b51de
diff --git a/resources/js/layouts/AuthGate.vue b/resources/js/layouts/AuthGate.vue
index 81360610..1d1e16fa 100644
--- a/resources/js/layouts/AuthGate.vue
+++ b/resources/js/layouts/AuthGate.vue
@@ -1,4 +1,5 @@
<script setup lang="ts">
+//UNUSED
import { useAuthStore } from '@/stores/AuthStore';
import { storeToRefs } from 'pinia';
import { useRoute } from 'vue-router';
diff --git a/resources/js/router/index.ts b/resources/js/router/index.ts
index 77439321..ff504b54 100644
--- a/resources/js/router/index.ts
+++ b/resources/js/router/index.ts
@@ -28,7 +28,7 @@ interface RouteMeta {
guestOnly?: boolean;
}
-const router = createRouter({
+export const router = createRouter({
history: createWebHistory(),
routes: [
{
diff --git a/resources/js/service/api.ts b/resources/js/service/api.ts
index 4a8fe0d0..412f06eb 100644
--- a/resources/js/service/api.ts
+++ b/resources/js/service/api.ts
@@ -1,7 +1,10 @@
+import { useAuthStore } from '@/stores/AuthStore';
+import { AxiosError } from 'axios';
import { toast } from '@/service/toaster/toastService';
import nProgress from 'nprogress';
-import axios, { AxiosError } from 'axios';
+import axios from 'axios';
+import { useRouter } from 'vue-router';
let progressTimeout: NodeJS.Timeout;
@@ -11,18 +14,48 @@ const handleResponse = (response: any) => {
return response;
};
-const handleError = (error: AxiosError<{ message?: string }>) => {
+const handleError = async (error: AxiosError<{ message?: string }>) => {
clearTimeout(progressTimeout);
nProgress.done(true);
- // if the server throws an error (404, 500 etc.)
- console.log(error);
+ const auth = useAuthStore();
const message = error.response?.data?.message ?? error.message;
+ const status = error.response?.status || 0;
+
+ // if the server throws an error (404, 500 etc.)
+ const knownError = [401, 422, 500, 404, 419].includes(status);
+ const showToast = !error.config?.headers?.['X-Skip-Toast'];
+ const isSessionExpired = status === 419;
+ const isAuthError = status === 401;
+
+ if (showToast) {
+ toast('Error', { type: 'danger', description: message });
+ }
+
+ // Handle session timeout (CSRF token expired)
+ if (isSessionExpired && auth.userData) {
+ import('@/router/index').then(({ router }) => {
+ if (router.currentRoute.value.name !== 'logout') {
+ router.push('/logout');
+ }
+ });
+ error.message = `Session Expired: ${message}`;
+ throw error;
+ }
+
+ // Handle expired auth token
+ if (isAuthError && auth.userData) {
+ auth.clearAuthState(true);
+ error.message = `Not logged in: ${message}`;
+ throw error;
+ }
- toast('Error', { type: 'danger', description: message });
- if (error.status === 401 || error.status == 422 || error.status == 500 || error.status == 404) throw error;
+ if (!knownError) {
+ console.error(error);
+ return error.response;
+ }
- return error.response;
+ throw error;
};
export const API = axios.create({
diff --git a/resources/js/service/authAPI.ts b/resources/js/service/authAPI.ts
index 2a635672..adce6be8 100644
--- a/resources/js/service/authAPI.ts
+++ b/resources/js/service/authAPI.ts
@@ -48,6 +48,7 @@ export const authenticate = async (token: string | null) => {
try {
return await API.get('/auth', {
headers: {
+ 'X-Skip-Toast': 'true',
// Authorization: `bearer ${token}`, //This is the only place i use bearer but the entire spa app uses cookies
},
});