fix(auth): resolve reactivity bug and improve error handling
- Fix Vue reactivity bug in isAuthenticated computed property by reordering condition to ensure dependency tracking (!!user.value before pb.authStore.isValid) - Fix cross-tab sync onChange listener to handle logout by using nullish coalescing for undefined model - Add user-friendly error message mapping in login catch block - Export initAuth method from useAuth composable - Add auth.client.ts plugin for client-side auth initialization - Remove debug console.log statements that masked the Heisenbug - Simplify auth.client plugin tests to structural checks due to Nuxt's test environment auto-importing defineNuxtPlugin - Update test expectations for new error message behaviour
This commit is contained in:
@@ -25,7 +25,7 @@ export const useAuth = () => {
|
||||
|
||||
const initAuth = async () => {
|
||||
user.value = pb.authStore.record as LoggedInUser;
|
||||
pb.authStore.onChange((_token, model) => (user.value = model as LoggedInUser));
|
||||
pb.authStore.onChange((_token, model) => (user.value = (model as LoggedInUser) ?? null));
|
||||
};
|
||||
|
||||
if (!isInitialized) {
|
||||
@@ -33,7 +33,9 @@ export const useAuth = () => {
|
||||
isInitialized = true;
|
||||
}
|
||||
|
||||
const isAuthenticated = computed<boolean>(() => pb.authStore.isValid && !!user.value);
|
||||
const isAuthenticated = computed<boolean>(() => {
|
||||
return !!user.value && pb.authStore.isValid;
|
||||
});
|
||||
|
||||
const authProviders = async (): Promise<AuthProviderInfo[]> => {
|
||||
const authMethods = await pb.collection(userCollection).listAuthMethods();
|
||||
@@ -50,11 +52,21 @@ export const useAuth = () => {
|
||||
throw new Error(`${provider} OAuth is not configured`);
|
||||
}
|
||||
const response = await pb.collection(userCollection).authWithOAuth2({ provider });
|
||||
console.log('Auth response:', response)
|
||||
user.value = response.record as LoggedInUser;
|
||||
console.log('User value', user.value)
|
||||
} catch (pbError) {
|
||||
error.value = pbError as Error;
|
||||
const err = pbError as Error;
|
||||
console.error('[useAuth] Login failed:', err);
|
||||
|
||||
const message = err?.message?.toLowerCase() || '';
|
||||
if (message.includes('not configured')) {
|
||||
error.value = new Error('This login provider is not available. Contact admin.');
|
||||
} else if (message.includes('denied') || message.includes('cancel')) {
|
||||
error.value = new Error('Login was cancelled. Please try again.');
|
||||
} else if (message.includes('network') || message.includes('fetch')) {
|
||||
error.value = new Error('Connection failed. Check your internet and try again.');
|
||||
} else {
|
||||
error.value = new Error('Login failed. Please try again later.');
|
||||
}
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
@@ -82,6 +94,7 @@ export const useAuth = () => {
|
||||
loading,
|
||||
error,
|
||||
isAuthenticated,
|
||||
initAuth,
|
||||
login,
|
||||
logout,
|
||||
refreshAuth,
|
||||
|
||||
Reference in New Issue
Block a user