'use client'; // Mark as a Client Component import { useState } from 'react'; import { useRouter } from 'next/navigation'; import { query } from '../../../lib/db'; import bcrypt from 'bcryptjs'; export default function Register() { const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const [error, setError] = useState(''); const router = useRouter(); const handleRegister = async (e) => { e.preventDefault(); const hashedPassword = bcrypt.hashSync(password, 10); try { await query( 'INSERT INTO users (username, password, role) VALUES (?, ?, ?)', [username, hashedPassword, 'user'] ); router.push('/login'); // Redirect to the login page after registration } catch (err) { setError('Registration failed. Username may already exist.'); } }; return (
); }