|
| 1 | +import { useState } from 'react'; |
| 2 | +import { useNavigate, Link } from 'react-router-dom'; |
| 3 | + |
| 4 | +const API = import.meta.env.VITE_API_URL ?? 'http://localhost:3000'; |
| 5 | + |
| 6 | +export default function Register() { |
| 7 | + const navigate = useNavigate(); |
| 8 | + const [address, setAddress] = useState(''); |
| 9 | + const [usernameHash, setUsernameHash] = useState(''); |
| 10 | + const [contactHash, setContactHash] = useState(''); |
| 11 | + const [error, setError] = useState(''); |
| 12 | + const [loading, setLoading] = useState(false); |
| 13 | + |
| 14 | + async function handleSubmit(e: React.FormEvent) { |
| 15 | + e.preventDefault(); |
| 16 | + setError(''); |
| 17 | + setLoading(true); |
| 18 | + try { |
| 19 | + const res = await fetch(`${API}/users`, { |
| 20 | + method: 'POST', |
| 21 | + headers: { 'Content-Type': 'application/json' }, |
| 22 | + body: JSON.stringify({ |
| 23 | + address, |
| 24 | + username_hash: usernameHash, |
| 25 | + contact_hash: contactHash, |
| 26 | + }), |
| 27 | + }); |
| 28 | + if (!res.ok) { |
| 29 | + const body = await res.json().catch(() => ({})); |
| 30 | + throw new Error(body?.error?.detail ?? `HTTP ${res.status}`); |
| 31 | + } |
| 32 | + navigate(`/users/${address}`); |
| 33 | + } catch (err: any) { |
| 34 | + setError(err.message); |
| 35 | + } finally { |
| 36 | + setLoading(false); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + return ( |
| 41 | + <div style={styles.card}> |
| 42 | + <h2>Register</h2> |
| 43 | + <p style={styles.hint}> |
| 44 | + Hashes are SHA-256 of the plaintext, computed client-side before submission. |
| 45 | + </p> |
| 46 | + <form onSubmit={handleSubmit} style={styles.form}> |
| 47 | + <label style={styles.label}> |
| 48 | + Stellar Address |
| 49 | + <input |
| 50 | + style={styles.input} |
| 51 | + value={address} |
| 52 | + onChange={(e) => setAddress(e.target.value)} |
| 53 | + placeholder="G…" |
| 54 | + required |
| 55 | + /> |
| 56 | + </label> |
| 57 | + <label style={styles.label}> |
| 58 | + Username Hash (SHA-256) |
| 59 | + <input |
| 60 | + style={styles.input} |
| 61 | + value={usernameHash} |
| 62 | + onChange={(e) => setUsernameHash(e.target.value)} |
| 63 | + placeholder="64-char hex" |
| 64 | + required |
| 65 | + /> |
| 66 | + </label> |
| 67 | + <label style={styles.label}> |
| 68 | + Contact Hash (SHA-256) |
| 69 | + <input |
| 70 | + style={styles.input} |
| 71 | + value={contactHash} |
| 72 | + onChange={(e) => setContactHash(e.target.value)} |
| 73 | + placeholder="64-char hex" |
| 74 | + required |
| 75 | + /> |
| 76 | + </label> |
| 77 | + {error && <p style={styles.error}>{error}</p>} |
| 78 | + <button style={styles.btn} type="submit" disabled={loading}> |
| 79 | + {loading ? 'Registering…' : 'Register'} |
| 80 | + </button> |
| 81 | + </form> |
| 82 | + <p style={{ marginTop: '1rem', fontSize: '0.875rem' }}> |
| 83 | + Already registered? <Link to="/login">Login</Link> |
| 84 | + </p> |
| 85 | + </div> |
| 86 | + ); |
| 87 | +} |
| 88 | + |
| 89 | +const styles: Record<string, React.CSSProperties> = { |
| 90 | + card: { maxWidth: 480, margin: '2rem auto', padding: '2rem', border: '1px solid #e2e8f0', borderRadius: 8 }, |
| 91 | + hint: { fontSize: '0.8rem', color: '#666', marginBottom: '1rem' }, |
| 92 | + form: { display: 'flex', flexDirection: 'column', gap: '1rem' }, |
| 93 | + label: { display: 'flex', flexDirection: 'column', gap: 4, fontSize: '0.875rem', fontWeight: 500 }, |
| 94 | + input: { padding: '0.5rem', border: '1px solid #cbd5e0', borderRadius: 4, fontSize: '0.875rem' }, |
| 95 | + btn: { padding: '0.6rem', background: '#1a1a2e', color: '#fff', border: 'none', borderRadius: 6, cursor: 'pointer' }, |
| 96 | + error: { color: '#e53e3e', fontSize: '0.875rem' }, |
| 97 | +}; |
0 commit comments