codalware-auth
Version:
Complete authentication system with enterprise security, attack protection, team workspaces, waitlist, billing, UI components, 2FA, and account recovery - production-ready in 5 minutes. Enhanced CLI with verification, rollback, and App Router scaffolding.
43 lines (35 loc) • 1.25 kB
text/typescript
import { useEffect, useState } from 'react';
export interface UserDevice {
id: string;
deviceName: string;
deviceType: string;
browser: string;
os: string;
lastUsedAt: string;
isTrusted: boolean;
createdAt: string;
}
export const useUserDevices = () => {
const [devices,setDevices] = useState<UserDevice[]>([]);
const [loading,setLoading] = useState(true);
const [error,setError] = useState<string|null>(null);
const load = async () => {
setLoading(true); setError(null);
try {
// Avoid browser conditional caching; request fresh data.
const res = await fetch('/api/user/devices', { cache: 'no-store' as RequestCache });
// If server responds 304 Not Modified, keep existing devices (not an error)
if (res.status === 304) {
setLoading(false);
return;
}
if(!res.ok) throw new Error('Failed to load devices');
const data = await res.json();
setDevices(data.devices || []);
} catch(e: unknown){
setError(e instanceof Error ? e.message : 'Failed to load devices');
} finally { setLoading(false); }
};
useEffect(()=>{ load(); },[]);
return { devices, loading, error, reload: load };
};