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:
29
lib/auth/client.ts
Normal file
29
lib/auth/client.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
export function isTokenExpired(token: string): boolean {
|
||||
try {
|
||||
const payload = JSON.parse(atob(token.split('.')[1])) as { exp?: number }
|
||||
if (!payload || !payload.exp) {
|
||||
console.log('Token has no expiration data')
|
||||
return true
|
||||
}
|
||||
|
||||
const currentTime = Math.floor(Date.now() / 1000)
|
||||
const isExpired = payload.exp < currentTime
|
||||
console.log(`Token expiration check: exp=${payload.exp}, now=${currentTime}, expired=${isExpired}`)
|
||||
return isExpired
|
||||
} catch (error) {
|
||||
console.log('Token validation error:', error)
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
export function clearExpiredToken(): void {
|
||||
const token = localStorage.getItem('authToken')
|
||||
if (token && isTokenExpired(token)) {
|
||||
console.log('Clearing expired token from localStorage')
|
||||
localStorage.removeItem('authToken')
|
||||
} else if (token) {
|
||||
console.log('Token exists and is valid')
|
||||
} else {
|
||||
console.log('No token in localStorage')
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user