- 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>
131 lines
3.1 KiB
TypeScript
131 lines
3.1 KiB
TypeScript
'use client'
|
|
|
|
import { useState } from 'react'
|
|
import { useTranslations } from 'next-intl'
|
|
import { useAuth } from './auth-provider'
|
|
import {
|
|
Box,
|
|
TextField,
|
|
Button,
|
|
Alert,
|
|
CircularProgress,
|
|
InputAdornment,
|
|
IconButton
|
|
} from '@mui/material'
|
|
import {
|
|
Email,
|
|
Lock,
|
|
Visibility,
|
|
VisibilityOff
|
|
} from '@mui/icons-material'
|
|
|
|
interface LoginFormProps {
|
|
onSuccess?: () => void
|
|
}
|
|
|
|
export function LoginForm({ onSuccess }: LoginFormProps) {
|
|
const [email, setEmail] = useState('')
|
|
const [password, setPassword] = useState('')
|
|
const [loading, setLoading] = useState(false)
|
|
const [error, setError] = useState('')
|
|
const [showPassword, setShowPassword] = useState(false)
|
|
const { login } = useAuth()
|
|
const t = useTranslations('auth')
|
|
|
|
const handleSubmit = async (e: React.FormEvent) => {
|
|
e.preventDefault()
|
|
setLoading(true)
|
|
setError('')
|
|
|
|
try {
|
|
const result = await login(email, password)
|
|
|
|
if (result.success) {
|
|
if (onSuccess) {
|
|
onSuccess()
|
|
}
|
|
} else {
|
|
setError(result.error || t('loginError'))
|
|
}
|
|
} catch (error) {
|
|
setError(t('connectionError'))
|
|
} finally {
|
|
setLoading(false)
|
|
}
|
|
}
|
|
|
|
const handleClickShowPassword = () => {
|
|
setShowPassword(!showPassword)
|
|
}
|
|
|
|
return (
|
|
<Box component="form" onSubmit={handleSubmit} sx={{ width: '100%' }}>
|
|
<TextField
|
|
fullWidth
|
|
type="email"
|
|
label={t('email')}
|
|
value={email}
|
|
onChange={(e) => setEmail(e.target.value)}
|
|
required
|
|
margin="normal"
|
|
variant="outlined"
|
|
disabled={loading}
|
|
InputProps={{
|
|
startAdornment: (
|
|
<InputAdornment position="start">
|
|
<Email color="action" />
|
|
</InputAdornment>
|
|
),
|
|
}}
|
|
/>
|
|
|
|
<TextField
|
|
fullWidth
|
|
type={showPassword ? 'text' : 'password'}
|
|
label={t('password')}
|
|
value={password}
|
|
onChange={(e) => setPassword(e.target.value)}
|
|
required
|
|
margin="normal"
|
|
variant="outlined"
|
|
disabled={loading}
|
|
InputProps={{
|
|
startAdornment: (
|
|
<InputAdornment position="start">
|
|
<Lock color="action" />
|
|
</InputAdornment>
|
|
),
|
|
endAdornment: (
|
|
<InputAdornment position="end">
|
|
<IconButton
|
|
aria-label="toggle password visibility"
|
|
onClick={handleClickShowPassword}
|
|
edge="end"
|
|
disabled={loading}
|
|
>
|
|
{showPassword ? <VisibilityOff /> : <Visibility />}
|
|
</IconButton>
|
|
</InputAdornment>
|
|
),
|
|
}}
|
|
/>
|
|
|
|
{error && (
|
|
<Alert severity="error" sx={{ mt: 2 }}>
|
|
{error}
|
|
</Alert>
|
|
)}
|
|
|
|
<Button
|
|
type="submit"
|
|
fullWidth
|
|
variant="contained"
|
|
disabled={loading}
|
|
sx={{ mt: 3, mb: 2, py: 1.5 }}
|
|
startIcon={loading ? <CircularProgress size={20} color="inherit" /> : undefined}
|
|
>
|
|
{loading ? t('logging_in') : t('login')}
|
|
</Button>
|
|
</Box>
|
|
)
|
|
} |