Spaces:
Running
Running
Create AuthForm.jsx
Browse files- AuthForm.jsx +47 -0
AuthForm.jsx
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import React, { useState } from 'react';
|
| 2 |
+
import axios from 'axios';
|
| 3 |
+
|
| 4 |
+
const AuthForm = ({ type = "login" }) => {
|
| 5 |
+
const [username, setUsername] = useState('');
|
| 6 |
+
const [password, setPassword] = useState('');
|
| 7 |
+
const [msg, setMsg] = useState('');
|
| 8 |
+
|
| 9 |
+
const handleSubmit = async (e) => {
|
| 10 |
+
e.preventDefault();
|
| 11 |
+
try {
|
| 12 |
+
const endpoint = type === 'signup' ? '/signup' : '/login';
|
| 13 |
+
const res = await axios.post(`https://your-hf-space.hf.space${endpoint}`, {
|
| 14 |
+
username,
|
| 15 |
+
password,
|
| 16 |
+
});
|
| 17 |
+
if (type === 'login') {
|
| 18 |
+
localStorage.setItem('token', res.data.token);
|
| 19 |
+
window.location.href = "/search";
|
| 20 |
+
} else {
|
| 21 |
+
setMsg("Account created! You can now login.");
|
| 22 |
+
}
|
| 23 |
+
} catch (err) {
|
| 24 |
+
setMsg(err.response?.data?.message || 'Something went wrong');
|
| 25 |
+
}
|
| 26 |
+
};
|
| 27 |
+
|
| 28 |
+
return (
|
| 29 |
+
<div className="max-w-md mx-auto p-6 bg-white rounded-2xl shadow-lg">
|
| 30 |
+
<h2 className="text-2xl font-bold mb-4">{type === 'signup' ? 'Sign Up' : 'Login'}</h2>
|
| 31 |
+
<form onSubmit={handleSubmit} className="space-y-4">
|
| 32 |
+
<input type="text" placeholder="Username"
|
| 33 |
+
className="w-full px-4 py-2 border rounded-lg"
|
| 34 |
+
value={username} onChange={e => setUsername(e.target.value)} required />
|
| 35 |
+
<input type="password" placeholder="Password"
|
| 36 |
+
className="w-full px-4 py-2 border rounded-lg"
|
| 37 |
+
value={password} onChange={e => setPassword(e.target.value)} required />
|
| 38 |
+
<button className="w-full bg-blue-600 text-white py-2 rounded-lg hover:bg-blue-700">
|
| 39 |
+
{type === 'signup' ? 'Sign Up' : 'Login'}
|
| 40 |
+
</button>
|
| 41 |
+
{msg && <p className="text-red-500">{msg}</p>}
|
| 42 |
+
</form>
|
| 43 |
+
</div>
|
| 44 |
+
);
|
| 45 |
+
};
|
| 46 |
+
|
| 47 |
+
export default AuthForm;
|