add:planned feats

This commit is contained in:
kokopi
2026-03-09 00:51:07 +09:00
parent 16bc00632d
commit fc611806a3
30 changed files with 950 additions and 129 deletions

View File

@@ -0,0 +1,35 @@
import { useState, useEffect, useCallback } from 'react'
import type { User } from '../lib/types.ts'
export type AuthState = 'pending' | 'authenticated' | 'unauthenticated'
export function useAuth() {
const [user, setUser] = useState<User | null>(null)
const [authState, setAuthState] = useState<AuthState>('pending')
useEffect(() => {
fetch('/api/auth/me', { credentials: 'include' })
.then(res => res.json())
.then(data => {
if (data.user) {
setUser(data.user)
setAuthState('authenticated')
} else {
setUser(null)
setAuthState('unauthenticated')
}
})
.catch(() => {
setUser(null)
setAuthState('unauthenticated')
})
}, [])
const logout = useCallback(async () => {
await fetch('/api/auth/logout', { method: 'POST', credentials: 'include' })
setUser(null)
setAuthState('unauthenticated')
}, [])
return { user, authState, logout }
}