Files
ember-market-frontend/lib/hooks/useUser.ts
g fe01f31538
Some checks failed
Build Frontend / build (push) Failing after 7s
Refactor UI imports and update component paths
Replaces imports from 'components/ui' with 'components/common' across the app and dashboard pages, and updates model and API imports to use new paths under 'lib'. Removes redundant authentication checks from several dashboard pages. Adds new dashboard components and utility files, and reorganizes hooks and services into the 'lib' directory for improved structure.
2026-01-13 05:02:13 +00:00

51 lines
993 B
TypeScript

"use client"
import { useState, useEffect } from 'react'
import { clientFetch } from '@/lib/api/api-client'
interface Vendor {
_id: string;
username: string;
storeId: string;
pgpKey: string;
__v: number;
}
interface User {
vendor: Vendor;
}
export function useUser() {
const [user, setUser] = useState<User | null>(null)
const [loading, setLoading] = useState(true)
const [error, setError] = useState<string | null>(null)
useEffect(() => {
const fetchUser = async () => {
try {
setLoading(true)
const userData = await clientFetch<User>("/auth/me")
setUser(userData)
setError(null)
} catch (err) {
console.error("Failed to fetch user:", err)
setError("Failed to fetch user data")
setUser(null)
} finally {
setLoading(false)
}
}
fetchUser()
}, [])
const isAdmin = user?.vendor?.username === 'admin1'
return {
user,
loading,
error,
isAdmin
}
}