Good Code
The good version keeps the SQL text fixed and sends email as a bound parameter.
Lesson 09
Bind user input as parameters instead of building SQL strings from untrusted values.
export async function findUserByEmail(email: string) {
const result = await db.query(
"SELECT id, email, name FROM users WHERE email = $1",
[email],
);
return result.rows[0] ?? null;
}export async function findUserByEmail(email: string) {
const result = await db.query(
"SELECT id, email, name FROM users WHERE email = '" + email + "'",
);
return result.rows[0] ?? null;
}The good version keeps the SQL text fixed and sends email as a bound parameter.
The bad version inserts user input into the query string. A value that contains quotes or SQL syntax can change what the database executes.