60 lines
1.9 KiB
JavaScript
60 lines
1.9 KiB
JavaScript
import { useState } from 'react';
|
|
import { useRouter } from 'next/router';
|
|
|
|
export default function Signup() {
|
|
const [username, setUsername] = useState('');
|
|
const [password, setPassword] = useState('');
|
|
const [error, setError] = useState(null);
|
|
const router = useRouter();
|
|
|
|
const handleSubmit = async e => {
|
|
e.preventDefault();
|
|
setError(null);
|
|
try {
|
|
const res = await fetch('/api/signup', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ username, password })
|
|
});
|
|
const data = await res.json();
|
|
if (!res.ok) throw new Error(data.message || 'Signup failed');
|
|
alert('Conta criada! Redirecionando para login...');
|
|
router.push('/login');
|
|
} catch (err) {
|
|
setError(err.message);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div style={{ maxWidth: 400, margin: '2rem auto', padding: '1rem', border: '1px solid #ddd', borderRadius: 8 }}>
|
|
<h2>Cadastro - Expedição Gold</h2>
|
|
{error && <p style={{ color: 'red' }}>{error}</p>}
|
|
<form onSubmit={handleSubmit}>
|
|
<div>
|
|
<label>Usuário:</label>
|
|
<input
|
|
type="text"
|
|
value={username}
|
|
onChange={e => setUsername(e.target.value)}
|
|
required
|
|
style={{ width: '100%', padding: '0.5rem', marginTop: 0.5 }}
|
|
/>
|
|
</div>
|
|
<div>
|
|
<label>Senha:</label>
|
|
<input
|
|
type="password"
|
|
value={password}
|
|
onChange={e => setPassword(e.target.value)}
|
|
required
|
|
style={{ width: '100%', padding: '0.5rem', marginTop: 0.5 }}
|
|
/>
|
|
</div>
|
|
<button type="submit" style={{ marginTop: 1, padding: '0.5rem 1rem' }}>Criar conta</button>
|
|
</form>
|
|
<p>
|
|
Já tem conta? <a href="/login" style={{ color: '#0066cc' }}>Faça login</a>
|
|
</p>
|
|
</div>
|
|
);
|
|
} |