Sign Up Page
My first full-stack authentication exercise, connecting validated React forms to Express and MySQL for a basic sign-up and login flow.

React Login & Signup System
This was the first project where I learned authentication. I built a full-stack sign-up and login flow with React, Express, and MySQL, using Axios to connect the frontend to a REST API.
Signup and login API
The Express server has two routes. Signup inserts a new user into MySQL, and login looks the user up by email and password. Both use parameterized queries (? placeholders) to prevent SQL injection, and both return clear status codes: 400 for missing fields, 401 for bad credentials, 500 for server errors.
app.post('/signup', async (req, res) => {
const { name, email, password } = req.body;
if (!name || !email || !password || !name.trim() || !password.trim()) {
return res.status(400).json({ message: "All fields are required." });
}
const sql = "INSERT INTO login (name, email, password) VALUES (?, ?, ?)";
const [result] = await db.query(sql, [name.trim(), email.trim(), password.trim()]);
return res.status(201).json({ id: result.insertId, message: "Sign up successfully!" });
});const sql = "SELECT * FROM login WHERE `email` = ? AND `password` = ?";
const [rows] = await db.query(sql, [email.trim(), password.trim()]);
if (rows.length > 0) {
return res.status(200).json({
message: "Login successfully!",
user: { id: rows[0].id, name: rows[0].name, email: rows[0].email },
});
} else {
return res.status(401).json({ message: "Invalid email or password" });
}Client-side validation
Before sending anything to the server, a validation function checks the email format and enforces a strong password rule with regex:
const email_pattern = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
const password_pattern = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/The validation returns an errors object. The form only submits if the object is empty, and the error messages appear under each input:
const handleSubmit = (e) => {
e.preventDefault();
const validationErrors = validation(values);
setErrors(validationErrors);
if (Object.keys(validationErrors).length === 0) {
axios.post(apiURL, values)
.then(res => navigate('/home'))
.catch(err => alert(err.response?.data?.message || "Login failed. Please try again."));
}
}Routing
React Router handles the three pages: login, signup, and a home page shown after a successful login.
<Routes>
<Route path='/' element={<Login />} />
<Route path='/signup' element={<Signup />} />
<Route path='/home' element={<Home />} />
</Routes>What I learned
- How a signup and login flow works end to end, from form to database
- Controlled inputs in React with
useState, and validating with regex - Building a REST API with Express and MySQL, and returning proper HTTP status codes
- Connecting the frontend and backend with Axios, and handling CORS
What I'd improve next
Since this was my first auth project, it covers the basic flow but not production-level security yet. The next steps would be:
- Hash passwords with bcrypt instead of storing them as plain text
- Use sessions or JWT so
/homeis actually protected and users stay logged in