import { useEffect, useState } from 'preact/hooks'

export const useUser = () => {
    const [user, setUser] = useState(null)

    const logout = () => {
        setUser(null)
    }

    useEffect(async () => {
        const res = await fetch(`/api/current-user`, {
            credentials: 'include',
        })

        if (res.ok) {
            const user = await res.json()
            setUser(user)
        }
    }, [])

    return [user, logout]
}
