148 lines
4.2 KiB
TypeScript
148 lines
4.2 KiB
TypeScript
'use client';
|
|
|
|
import { useState } from 'react';
|
|
import { useRouter } from 'next/navigation';
|
|
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
|
|
import { Button } from '@/components/ui/button';
|
|
import { Input } from '@/components/ui/input';
|
|
import { Label } from '@/components/ui/label';
|
|
import { setCookie } from 'cookies-next';
|
|
|
|
export default function LoginPage() {
|
|
const router = useRouter();
|
|
const [email, setEmail] = useState('');
|
|
const [password, setPassword] = useState('');
|
|
const [host, setHost] = useState('mail.infomaniak.com');
|
|
const [port, setPort] = useState('993');
|
|
const [error, setError] = useState('');
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
const handleSubmit = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
setError('');
|
|
setLoading(true);
|
|
|
|
try {
|
|
// Test the connection first
|
|
const testResponse = await fetch('/api/mail/test-connection', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({
|
|
email,
|
|
password,
|
|
host,
|
|
port,
|
|
}),
|
|
});
|
|
|
|
const testData = await testResponse.json();
|
|
|
|
if (!testResponse.ok) {
|
|
throw new Error(testData.error || 'Failed to connect to email server');
|
|
}
|
|
|
|
// Store all credentials in a single cookie
|
|
const credentials = {
|
|
email,
|
|
password,
|
|
host,
|
|
port: parseInt(port),
|
|
};
|
|
|
|
console.log('Storing credentials in cookie:', {
|
|
...credentials,
|
|
password: '***'
|
|
});
|
|
|
|
// Store as a single cookie with proper options
|
|
setCookie('imap_credentials', JSON.stringify(credentials), {
|
|
maxAge: 60 * 60 * 24, // 1 day
|
|
path: '/',
|
|
sameSite: 'lax',
|
|
secure: process.env.NODE_ENV === 'production',
|
|
httpOnly: false // Allow access from JavaScript
|
|
});
|
|
|
|
// Verify cookie was set
|
|
const stored = document.cookie.split(';').find(c => c.trim().startsWith('imap_credentials='));
|
|
console.log('Cookie verification:', stored ? 'Cookie found' : 'Cookie not found');
|
|
|
|
if (!stored) {
|
|
throw new Error('Failed to store credentials');
|
|
}
|
|
|
|
// Redirect to mail page
|
|
router.push('/mail');
|
|
} catch (err) {
|
|
setError(err instanceof Error ? err.message : 'An error occurred');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="min-h-screen flex items-center justify-center bg-gray-50">
|
|
<Card className="w-full max-w-md">
|
|
<CardHeader>
|
|
<CardTitle>Email Login</CardTitle>
|
|
</CardHeader>
|
|
<CardContent>
|
|
<form onSubmit={handleSubmit} className="space-y-4">
|
|
<div>
|
|
<Label htmlFor="email">Email</Label>
|
|
<Input
|
|
id="email"
|
|
type="email"
|
|
value={email}
|
|
onChange={(e) => setEmail(e.target.value)}
|
|
required
|
|
/>
|
|
</div>
|
|
<div>
|
|
<Label htmlFor="password">Password</Label>
|
|
<Input
|
|
id="password"
|
|
type="password"
|
|
value={password}
|
|
onChange={(e) => setPassword(e.target.value)}
|
|
required
|
|
/>
|
|
</div>
|
|
<div>
|
|
<Label htmlFor="host">IMAP Host</Label>
|
|
<Input
|
|
id="host"
|
|
type="text"
|
|
value={host}
|
|
onChange={(e) => setHost(e.target.value)}
|
|
required
|
|
/>
|
|
</div>
|
|
<div>
|
|
<Label htmlFor="port">IMAP Port</Label>
|
|
<Input
|
|
id="port"
|
|
type="text"
|
|
value={port}
|
|
onChange={(e) => setPort(e.target.value)}
|
|
required
|
|
/>
|
|
</div>
|
|
{error && (
|
|
<div className="text-red-500 text-sm">{error}</div>
|
|
)}
|
|
<Button
|
|
type="submit"
|
|
className="w-full"
|
|
disabled={loading}
|
|
>
|
|
{loading ? 'Connecting...' : 'Connect'}
|
|
</Button>
|
|
</form>
|
|
</CardContent>
|
|
</Card>
|
|
</div>
|
|
);
|
|
}
|