179 lines
6.4 KiB
TypeScript
179 lines
6.4 KiB
TypeScript
'use client';
|
|
|
|
import { useState } from 'react';
|
|
import { useRouter } from 'next/navigation';
|
|
import Link from 'next/link';
|
|
import { authApi } from '@/lib/api';
|
|
import { BrandMark } from '@/components/BrandMark';
|
|
|
|
export default function LoginPage() {
|
|
const router = useRouter();
|
|
const [email, setEmail] = useState('');
|
|
const [password, setPassword] = useState('');
|
|
const [error, setError] = useState('');
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
const handleSubmit = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
setError('');
|
|
setLoading(true);
|
|
|
|
try {
|
|
const response = await authApi.login({ username: email, password });
|
|
localStorage.setItem('access_token', response.access_token);
|
|
|
|
// Redirect to dashboard
|
|
router.push('/dashboard');
|
|
} catch (err: unknown) {
|
|
const error = err as { response?: { data?: { detail?: string } } };
|
|
setError(error.response?.data?.detail || 'Login failed. Please try again.');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
const handleGoogleLogin = async () => {
|
|
try {
|
|
const redirectUri = `${window.location.origin}/auth/callback`;
|
|
const authUrl = await authApi.getGoogleAuthUrl(redirectUri);
|
|
window.location.href = authUrl;
|
|
} catch {
|
|
setError('Failed to initialize Google login');
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="flex min-h-screen items-center justify-center bg-[#f7faff] px-4 py-12 sm:px-6 lg:px-8">
|
|
<div className="w-full max-w-md">
|
|
<div className="mb-8 flex justify-center">
|
|
<Link href="/">
|
|
<BrandMark />
|
|
</Link>
|
|
</div>
|
|
<div className="ic-panel p-8">
|
|
<div>
|
|
<h2 className="text-center text-3xl font-extrabold text-slate-950">
|
|
Sign in
|
|
</h2>
|
|
<p className="mt-2 text-center text-sm text-slate-500">
|
|
Continue to your mailbox delivery dashboard.
|
|
</p>
|
|
</div>
|
|
<form className="mt-8 space-y-6" onSubmit={handleSubmit}>
|
|
{error && (
|
|
<div className="rounded-md border border-red-200 bg-red-50 p-4">
|
|
<p className="text-sm text-red-800">{error}</p>
|
|
</div>
|
|
)}
|
|
<div className="space-y-4">
|
|
<div>
|
|
<label htmlFor="email" className="block text-sm font-bold text-slate-700">
|
|
Email address
|
|
</label>
|
|
<input
|
|
id="email"
|
|
name="email"
|
|
type="email"
|
|
autoComplete="email"
|
|
required
|
|
value={email}
|
|
onChange={(e) => setEmail(e.target.value)}
|
|
className="ic-focus mt-1 relative block w-full rounded-md border border-[#b8c8df] px-3 py-2.5 text-slate-950 placeholder-slate-400 focus:border-[#0b63f6] sm:text-sm"
|
|
placeholder="Email address"
|
|
/>
|
|
</div>
|
|
<div>
|
|
<label htmlFor="password" className="block text-sm font-bold text-slate-700">
|
|
Password
|
|
</label>
|
|
<input
|
|
id="password"
|
|
name="password"
|
|
type="password"
|
|
autoComplete="current-password"
|
|
required
|
|
value={password}
|
|
onChange={(e) => setPassword(e.target.value)}
|
|
className="ic-focus mt-1 relative block w-full rounded-md border border-[#b8c8df] px-3 py-2.5 text-slate-950 placeholder-slate-400 focus:border-[#0b63f6] sm:text-sm"
|
|
placeholder="Password"
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div>
|
|
<button
|
|
type="submit"
|
|
disabled={loading}
|
|
className="ic-button-primary w-full disabled:opacity-50"
|
|
>
|
|
{loading ? 'Signing in...' : 'Sign in'}
|
|
</button>
|
|
</div>
|
|
|
|
<div className="relative">
|
|
<div className="absolute inset-0 flex items-center">
|
|
<div className="w-full border-t border-[#d9e3f2]" />
|
|
</div>
|
|
<div className="relative flex justify-center text-sm">
|
|
<span className="bg-white px-2 text-slate-500">Or continue with</span>
|
|
</div>
|
|
</div>
|
|
|
|
<div>
|
|
<button
|
|
type="button"
|
|
onClick={handleGoogleLogin}
|
|
className="ic-button-secondary w-full"
|
|
>
|
|
<svg className="w-5 h-5 mr-2" viewBox="0 0 24 24">
|
|
<path
|
|
fill="currentColor"
|
|
d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92c-.26 1.37-1.04 2.53-2.21 3.31v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.09z"
|
|
/>
|
|
<path
|
|
fill="currentColor"
|
|
d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z"
|
|
/>
|
|
<path
|
|
fill="currentColor"
|
|
d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z"
|
|
/>
|
|
<path
|
|
fill="currentColor"
|
|
d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z"
|
|
/>
|
|
</svg>
|
|
Sign in with Google
|
|
</button>
|
|
</div>
|
|
|
|
<div className="text-center">
|
|
<p className="text-sm text-slate-600">
|
|
Don't have an account?{' '}
|
|
<Link href="/register" className="font-bold text-[#0b63f6] hover:text-[#0649bf]">
|
|
Sign up
|
|
</Link>
|
|
</p>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
|
|
<div className="mt-6 flex justify-center gap-4 text-xs text-slate-400">
|
|
<Link href="/privacy" className="hover:text-slate-600 transition-colors">
|
|
Privacy Policy
|
|
</Link>
|
|
<Link href="/terms" className="hover:text-slate-600 transition-colors">
|
|
Terms
|
|
</Link>
|
|
<Link href="/impressum" className="hover:text-slate-600 transition-colors">
|
|
Impressum
|
|
</Link>
|
|
<Link href="/datenschutz" className="hover:text-slate-600 transition-colors">
|
|
Datenschutz
|
|
</Link>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|