Skip to content

Commit 6c1828b

Browse files
committed
fix: rely on new auth endpoint to determine auth state
1 parent be50416 commit 6c1828b

3 files changed

Lines changed: 26 additions & 31 deletions

File tree

resources/js/App.vue

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,18 @@ import { storeToRefs } from 'pinia';
1313
import { useAppStore } from '@/stores/AppStore';
1414
import { RouterView } from 'vue-router';
1515
16-
const toastPosition = ref<ToastPostion>();
17-
1816
const { contextMenuItems, contextMenuStyle, contextMenuItemStyle } = storeToRefs(useAppStore());
1917
const { isFullscreen } = useFullscreen();
2018
19+
const authStore = useAuthStore();
20+
21+
const toastPosition = ref<ToastPostion>();
22+
2123
async function loadUser() {
22-
const authStore = useAuthStore();
2324
await authStore.fetchUser();
2425
}
2526
26-
onMounted(async () => {
27+
onMounted(() => {
2728
useAppStore().initBrowserState();
2829
2930
toastPosition.value = getScreenSize() === 'default' ? 'top-center' : 'bottom-left';

resources/js/service/records/useRecords.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { useAuthStore } from '@/stores/AuthStore';
77
import { queryClient } from '@/service/vue-query';
88
import { storeToRefs } from 'pinia';
99
import { computed } from 'vue';
10+
1011
import api from './api';
1112

1213
export function useRecordsLimited(limit = 10) {

resources/js/stores/AuthStore.ts

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type { UserResource } from '@/types/resources';
22

33
import { authenticate } from '@/service/authAPI';
44
import { defineStore } from 'pinia';
5-
import { AxiosError } from 'axios';
65
import { toast } from '@aminnausin/cedar-ui';
76
import { ref } from 'vue';
87

@@ -15,43 +14,37 @@ export const useAuthStore = defineStore('Auth', () => {
1514

1615
const fetchUser = async (force: boolean = false): Promise<boolean> => {
1716
/*
18-
Auth States:
17+
Auth States (not very good):
1918
20-
1: Never logged in -> No token -> state is null
21-
2: Logged in previously -> Token -> Token is invalid (ajax) -> State is false
22-
3: Logged in previously -> Token -> Token is valid (ajax) -> State is true
23-
4: State exists -> State is State (Logged in or out has already been checked)
19+
1: Initial -> No checks have run -> State is null
20+
2: Never logged in -> No State -> State is null
21+
3: State exists -> State is State (Logged in or out has already been checked)
2422
2523
*/
2624

27-
if (!localStorage.getItem('auth-token')) return false; // no auth token
28-
29-
if (userData.value && !force) return true;
30-
25+
if (userData.value?.id && !force) return true; // Bad practice? Should I always check?
26+
if (isLoadingUserData.value) return false;
3127
if (userFetchPromise) return userFetchPromise;
3228

3329
userFetchPromise = (async () => {
3430
isLoadingUserData.value = true;
3531

3632
try {
37-
const localToken = localStorage.getItem('auth-token');
38-
if (!localToken) {
39-
clearAuthState();
40-
return false;
41-
}
42-
43-
const { data, status } = await authenticate(localToken);
33+
const { data } = await authenticate();
4434

45-
if (status !== 200) {
46-
throw new AxiosError('Not Authenticated', status.toString());
35+
if (data.isAuthenticated) {
36+
userData.value = data.user;
37+
return true;
4738
}
4839

49-
userData.value = data.data?.user;
50-
return true;
51-
} catch (error) {
52-
clearAuthState();
40+
console.log('Clear auth state because not authenticated');
5341

54-
console.error('Authentication failed:', error);
42+
clearAuthState(false);
43+
return false;
44+
} catch (error) {
45+
// Only when network or server error
46+
console.error(error);
47+
clearAuthState(true);
5548
return false;
5649
} finally {
5750
isLoadingUserData.value = false;
@@ -64,12 +57,12 @@ export const useAuthStore = defineStore('Auth', () => {
6457

6558
const clearAuthState = (showMessage: boolean = false, status = 401): void => {
6659
userData.value = null;
67-
isLoadingUserData.value = false;
68-
localStorage.removeItem('auth-token');
60+
localStorage.removeItem('auth-token'); // Legacy: Clears existing auth-tokens. No auth-tokens are created ever again.
6961

7062
if (!showMessage) return;
71-
const message = status === 419 ? 'Session Expired' : `Authentication Failed (${status})`;
63+
const message = `Login Expired${status ? ` (${status})` : ''}`;
7264

65+
// Can remove?
7366
toast.warning(message, {
7467
description: 'Please log in again.',
7568
});

0 commit comments

Comments
 (0)