Felix Zieger
fine tuning
8ec33d8
import { useState } from "react";
import { useNavigate } from "react-router-dom";
import { supabase } from "@/integrations/supabase/client";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { useToast } from "@/components/ui/use-toast";
export const AdminLogin = () => {
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [isLoading, setIsLoading] = useState(false);
const navigate = useNavigate();
const { toast } = useToast();
const handleLogin = async (e: React.FormEvent) => {
e.preventDefault();
setIsLoading(true);
try {
const { data: { user }, error } = await supabase.auth.signInWithPassword({
email,
password,
});
if (error) throw error;
if (user) {
const { data: isAdmin, error: adminError } = await supabase.rpc('is_admin', {
user_id: user.id
});
if (adminError || !isAdmin) {
throw new Error("Not authorized as admin");
}
toast({
title: "Login successful",
description: "Welcome to the admin area",
});
navigate("/admin");
}
} catch (error) {
console.error("Login error:", error);
toast({
title: "Login failed",
description: "Please check your credentials and try again",
variant: "destructive",
});
} finally {
setIsLoading(false);
}
};
return (
<div className="flex min-h-screen items-center justify-center">
<div className="w-full max-w-md space-y-8 p-8">
<div className="text-center">
<h2 className="text-2xl font-bold">Admin Login</h2>
</div>
<form onSubmit={handleLogin} className="space-y-6">
<div>
<Input
type="email"
placeholder="Email"
value={email}
onChange={(e) => setEmail(e.target.value)}
required
/>
</div>
<div>
<Input
type="password"
placeholder="Password"
value={password}
onChange={(e) => setPassword(e.target.value)}
required
/>
</div>
<Button
type="submit"
className="w-full"
disabled={isLoading}
>
{isLoading ? "Logging in..." : "Login"}
</Button>
</form>
</div>
</div>
);
};