Implement admin functionality for merging duplicate records with error handling and logging
This commit is contained in:
@@ -27,19 +27,28 @@ export const useAuthorization = () => {
|
||||
groups: []
|
||||
});
|
||||
|
||||
// Create a loading state
|
||||
const isLoading = ref(true);
|
||||
|
||||
// Function to sync auth state from nuxtApp payload
|
||||
const syncAuthState = () => {
|
||||
const payloadAuthState = nuxtApp.payload.data?.authState as AuthState;
|
||||
if (payloadAuthState) {
|
||||
authState.value = payloadAuthState;
|
||||
console.log('[useAuthorization] Auth state synced:', {
|
||||
authenticated: payloadAuthState.authenticated,
|
||||
groups: payloadAuthState.groups,
|
||||
user: payloadAuthState.user?.email
|
||||
});
|
||||
} else {
|
||||
console.log('[useAuthorization] No auth state found in payload');
|
||||
try {
|
||||
// Safely check if payload data exists
|
||||
if (nuxtApp.payload && nuxtApp.payload.data && nuxtApp.payload.data.authState) {
|
||||
const payloadAuthState = nuxtApp.payload.data.authState as AuthState;
|
||||
authState.value = payloadAuthState;
|
||||
isLoading.value = false;
|
||||
console.log('[useAuthorization] Auth state synced from payload:', {
|
||||
authenticated: payloadAuthState.authenticated,
|
||||
groups: payloadAuthState.groups,
|
||||
user: payloadAuthState.user?.email
|
||||
});
|
||||
return true;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[useAuthorization] Error syncing auth state:', error);
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
// Try to get auth state from API if not in payload
|
||||
@@ -47,6 +56,7 @@ export const useAuthorization = () => {
|
||||
try {
|
||||
const sessionData = await $fetch('/api/auth/session') as AuthState;
|
||||
authState.value = sessionData;
|
||||
isLoading.value = false;
|
||||
console.log('[useAuthorization] Auth state loaded from API:', {
|
||||
authenticated: sessionData.authenticated,
|
||||
groups: sessionData.groups,
|
||||
@@ -57,31 +67,46 @@ export const useAuthorization = () => {
|
||||
updateAuthState(sessionData);
|
||||
} catch (error) {
|
||||
console.error('[useAuthorization] Failed to load auth state:', error);
|
||||
isLoading.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
// Initialize auth state
|
||||
onMounted(() => {
|
||||
syncAuthState();
|
||||
// Initialize auth state immediately (not just onMounted)
|
||||
if (process.client) {
|
||||
// Try to sync from payload first
|
||||
const synced = syncAuthState();
|
||||
|
||||
// If no auth state in payload, try to load from API
|
||||
if (!authState.value.authenticated) {
|
||||
// If not synced from payload, load from API
|
||||
if (!synced) {
|
||||
loadAuthState();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// On server, try to get from payload
|
||||
syncAuthState();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current user groups from session
|
||||
*/
|
||||
const getUserGroups = (): string[] => {
|
||||
return authState.value.groups || [];
|
||||
try {
|
||||
return authState.value?.groups || [];
|
||||
} catch (error) {
|
||||
console.error('[useAuthorization] Error getting user groups:', error);
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Get current authenticated user
|
||||
*/
|
||||
const getCurrentUser = (): UserWithGroups | null => {
|
||||
return authState.value.user || null;
|
||||
try {
|
||||
return authState.value?.user || null;
|
||||
} catch (error) {
|
||||
console.error('[useAuthorization] Error getting current user:', error);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user