bolt.diy/app/components/auth/Auth.tsx
2025-03-13 07:09:36 -07:00

85 lines
2.6 KiB
TypeScript

import { useState } from 'react';
import { toast } from 'react-toastify';
import { signInWithEmail, signUp } from '~/lib/stores/auth';
interface AuthProps {
onClose: () => void;
}
export function Auth({ onClose }: AuthProps) {
const [isSignUp, setIsSignUp] = useState(false);
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [loading, setLoading] = useState(false);
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setLoading(true);
try {
if (isSignUp) {
await signUp(email, password);
toast.success('Check your email for the confirmation link');
} else {
await signInWithEmail(email, password);
toast.success('Signed in successfully');
onClose();
}
} catch (error) {
toast.error(error instanceof Error ? error.message : 'Authentication failed');
} finally {
setLoading(false);
}
};
return (
<div className="p-6 bg-bolt-elements-background-depth-1 rounded-lg shadow-lg max-w-md w-full">
<h2 className="text-2xl font-bold mb-6 text-center">{isSignUp ? 'Create Account' : 'Sign In'}</h2>
<form onSubmit={handleSubmit} className="space-y-4">
<div>
<label htmlFor="email" className="block text-sm font-medium mb-1">
Email
</label>
<input
id="email"
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
className="w-full p-2 border rounded bg-bolt-elements-background-depth-2 text-bolt-elements-textPrimary"
required
/>
</div>
<div>
<label htmlFor="password" className="block text-sm font-medium mb-1">
Password
</label>
<input
id="password"
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
className="w-full p-2 border rounded bg-bolt-elements-background-depth-2 text-bolt-elements-textPrimary"
required
/>
</div>
<button
type="submit"
disabled={loading}
className="w-full py-2 px-4 bg-blue-500 text-white rounded hover:bg-blue-600 disabled:opacity-50"
>
{loading ? 'Loading...' : isSignUp ? 'Sign Up' : 'Sign In'}
</button>
</form>
<div className="mt-4 text-center">
<button onClick={() => setIsSignUp(!isSignUp)} className="text-blue-500 hover:underline">
{isSignUp ? 'Already have an account? Sign in' : "Don't have an account? Sign up"}
</button>
</div>
</div>
);
}