Fix authentication state persistence and admin role display
- Implement complete authentication system with JWT token validation - Add auth provider with persistent login state across page refreshes - Create multilingual login/register forms with Material-UI components - Fix token validation using raw SQL queries to bypass Prisma sync issues - Add comprehensive error handling for expired/invalid tokens - Create profile and settings pages with full i18n support - Add proper user role management (admin/user) with database sync - Implement secure middleware with CSRF protection and auth checks - Add debug endpoints for troubleshooting authentication issues - Fix Zustand store persistence for authentication state 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,44 +1,2 @@
|
||||
import { useEffect } from 'react'
|
||||
import { useStore } from '@/lib/store'
|
||||
|
||||
export function useAuth() {
|
||||
const { user, setUser } = useStore()
|
||||
|
||||
useEffect(() => {
|
||||
const token = localStorage.getItem('authToken')
|
||||
if (token && !user) {
|
||||
// Validate token and get user info
|
||||
fetch('/api/auth/me', {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`
|
||||
}
|
||||
})
|
||||
.then(res => res.json())
|
||||
.then(data => {
|
||||
if (data.user) {
|
||||
setUser(data.user)
|
||||
} else {
|
||||
localStorage.removeItem('authToken')
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
localStorage.removeItem('authToken')
|
||||
})
|
||||
}
|
||||
}, [user, setUser])
|
||||
|
||||
const logout = () => {
|
||||
setUser(null)
|
||||
localStorage.removeItem('authToken')
|
||||
}
|
||||
|
||||
const isAuthenticated = !!user
|
||||
const token = typeof window !== 'undefined' ? localStorage.getItem('authToken') : null
|
||||
|
||||
return {
|
||||
user,
|
||||
isAuthenticated,
|
||||
token,
|
||||
logout
|
||||
}
|
||||
}
|
||||
// Re-export the auth context hook
|
||||
export { useAuth } from '@/components/auth/auth-provider'
|
||||
Reference in New Issue
Block a user