refactor: replace password card logic with useHasCredentialProvider hook

This commit is contained in:
javayhu 2025-08-21 23:54:28 +08:00
parent d644611afd
commit 19120ee7f1
2 changed files with 53 additions and 32 deletions

View File

@ -10,10 +10,10 @@ import {
CardTitle,
} from '@/components/ui/card';
import { Skeleton } from '@/components/ui/skeleton';
import { useHasCredentialProvider } from '@/hooks/use-auth';
import { authClient } from '@/lib/auth-client';
import { cn } from '@/lib/utils';
import { useTranslations } from 'next-intl';
import { useEffect, useState } from 'react';
/**
* PasswordCardWrapper renders either:
@ -24,38 +24,15 @@ import { useEffect, useState } from 'react';
*/
export function PasswordCardWrapper() {
const { data: session } = authClient.useSession();
const [hasCredentialProvider, setHasCredentialProvider] = useState(false);
const [isLoading, setIsLoading] = useState(true);
const { hasCredentialProvider, isLoading, error } = useHasCredentialProvider(
session?.user?.id
);
useEffect(() => {
const checkCredentialProvider = async () => {
if (!session?.user) {
setIsLoading(false);
return;
}
try {
// Get the user's linked accounts
const accounts = await authClient.listAccounts();
// console.log('accounts', accounts);
// Check if the response is successful and contains accounts data
if ('data' in accounts && Array.isArray(accounts.data)) {
// Check if any account has a credential provider (provider === 'credential')
const hasCredential = accounts.data.some(
(account) => account.provider === 'credential'
);
setHasCredentialProvider(hasCredential);
}
} catch (error) {
console.error('Error checking credential provider:', error);
} finally {
setIsLoading(false);
}
};
checkCredentialProvider();
}, [session]);
// Handle error state
if (error) {
console.error('check credential provider error:', error);
return null;
}
// Don't render anything while loading
if (isLoading) {

44
src/hooks/use-auth.ts Normal file
View File

@ -0,0 +1,44 @@
import { authClient } from '@/lib/auth-client';
import { useQuery } from '@tanstack/react-query';
// Query keys
export const userAccountsKeys = {
all: ['userAccounts'] as const,
list: (userId: string) => [...userAccountsKeys.all, 'list', userId] as const,
};
// Hook to fetch user accounts
export function useUserAccounts(userId: string | undefined) {
return useQuery({
queryKey: userAccountsKeys.list(userId || ''),
queryFn: async () => {
if (!userId) {
throw new Error('User ID is required');
}
const accounts = await authClient.listAccounts();
// Check if the response is successful and contains accounts data
if ('data' in accounts && Array.isArray(accounts.data)) {
return accounts.data;
}
throw new Error('Failed to fetch user accounts');
},
enabled: !!userId,
});
}
// Hook to check if user has credential provider
export function useHasCredentialProvider(userId: string | undefined) {
const { data: accounts, isLoading, error } = useUserAccounts(userId);
const hasCredentialProvider =
accounts?.some((account) => account.provider === 'credential') ?? false;
return {
hasCredentialProvider,
isLoading,
error,
};
}