Add duplicate management interface with scanning and merging functionality
This commit is contained in:
@@ -17,72 +17,59 @@ export interface AuthState {
|
||||
* Authorization composable for role-based access control
|
||||
*/
|
||||
export const useAuthorization = () => {
|
||||
// Get the current user state from Nuxt
|
||||
const nuxtApp = useNuxtApp();
|
||||
|
||||
// Create reactive auth state
|
||||
const authState = ref<AuthState>({
|
||||
// Simple reactive state that starts with safe defaults
|
||||
const authState = reactive<AuthState>({
|
||||
user: null,
|
||||
authenticated: false,
|
||||
groups: []
|
||||
});
|
||||
|
||||
// Create a loading state
|
||||
// Loading state
|
||||
const isLoading = ref(true);
|
||||
const hasInitialized = ref(false);
|
||||
|
||||
// Initialize auth state with comprehensive error handling
|
||||
const initializeAuth = async () => {
|
||||
if (hasInitialized.value) return;
|
||||
|
||||
// Function to sync auth state from nuxtApp payload
|
||||
const syncAuthState = () => {
|
||||
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
|
||||
console.log('[useAuthorization] Initializing auth state...');
|
||||
|
||||
// Try to get auth from session API directly
|
||||
const sessionData = await $fetch('/api/auth/session') as AuthState;
|
||||
|
||||
if (sessionData && typeof sessionData === 'object') {
|
||||
// Update reactive state
|
||||
authState.user = sessionData.user || null;
|
||||
authState.authenticated = sessionData.authenticated || false;
|
||||
authState.groups = Array.isArray(sessionData.groups) ? sessionData.groups : [];
|
||||
|
||||
console.log('[useAuthorization] Auth state initialized:', {
|
||||
authenticated: authState.authenticated,
|
||||
groups: authState.groups,
|
||||
user: authState.user?.email
|
||||
});
|
||||
|
||||
hasInitialized.value = true;
|
||||
isLoading.value = false;
|
||||
return true;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[useAuthorization] Error syncing auth state:', error);
|
||||
console.error('[useAuthorization] Failed to initialize auth:', error);
|
||||
}
|
||||
|
||||
// Set safe defaults on error
|
||||
authState.user = null;
|
||||
authState.authenticated = false;
|
||||
authState.groups = [];
|
||||
hasInitialized.value = true;
|
||||
isLoading.value = false;
|
||||
return false;
|
||||
};
|
||||
|
||||
// Try to get auth state from API if not in payload
|
||||
const loadAuthState = async () => {
|
||||
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,
|
||||
user: sessionData.user?.email
|
||||
});
|
||||
|
||||
// Update nuxtApp payload for future use
|
||||
updateAuthState(sessionData);
|
||||
} catch (error) {
|
||||
console.error('[useAuthorization] Failed to load auth state:', error);
|
||||
isLoading.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
// Initialize auth state immediately (not just onMounted)
|
||||
// Initialize immediately on client
|
||||
if (process.client) {
|
||||
// Try to sync from payload first
|
||||
const synced = syncAuthState();
|
||||
|
||||
// If not synced from payload, load from API
|
||||
if (!synced) {
|
||||
loadAuthState();
|
||||
}
|
||||
} else {
|
||||
// On server, try to get from payload
|
||||
syncAuthState();
|
||||
initializeAuth();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -90,7 +77,7 @@ export const useAuthorization = () => {
|
||||
*/
|
||||
const getUserGroups = (): string[] => {
|
||||
try {
|
||||
return authState.value?.groups || [];
|
||||
return Array.isArray(authState.groups) ? authState.groups : [];
|
||||
} catch (error) {
|
||||
console.error('[useAuthorization] Error getting user groups:', error);
|
||||
return [];
|
||||
@@ -102,7 +89,7 @@ export const useAuthorization = () => {
|
||||
*/
|
||||
const getCurrentUser = (): UserWithGroups | null => {
|
||||
try {
|
||||
return authState.value?.user || null;
|
||||
return authState.user || null;
|
||||
} catch (error) {
|
||||
console.error('[useAuthorization] Error getting current user:', error);
|
||||
return null;
|
||||
@@ -233,11 +220,16 @@ export const useAuthorization = () => {
|
||||
/**
|
||||
* Update auth state (called by middleware)
|
||||
*/
|
||||
const updateAuthState = (authState: AuthState) => {
|
||||
if (!nuxtApp.payload.data) {
|
||||
nuxtApp.payload.data = {};
|
||||
const updateAuthState = (newAuthState: AuthState) => {
|
||||
try {
|
||||
const nuxtApp = useNuxtApp();
|
||||
if (!nuxtApp.payload.data) {
|
||||
nuxtApp.payload.data = {};
|
||||
}
|
||||
nuxtApp.payload.data.authState = newAuthState;
|
||||
} catch (error) {
|
||||
console.error('[useAuthorization] Error updating auth state:', error);
|
||||
}
|
||||
nuxtApp.payload.data.authState = authState;
|
||||
};
|
||||
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user