== How to fix it in Sequelize

=== Code examples

include::../../common/fix/code-rationale.adoc[]

==== Noncompliant code example

[source,javascript,diff-id=11,diff-type=noncompliant]
----
async function index(req, res) {
    const { db, User } = req.app.get('sequelize');

    const user = req.query.user;
    const pass = req.query.pass;

    let loggedInUser = await db.query(
        `SELECT * FROM users WHERE user = '${user}' AND pass = '${pass}'`,
        {
            model: User,
        }
    ); // Noncompliant

    res.send(JSON.stringify(loggedInUser));
    res.end();
}
----

==== Compliant solution

[source,javascript,diff-id=11,diff-type=compliant]
----
async function index(req, res) {
    const { db, User, QueryTypes } = req.app.get('sequelize');

    const user = req.query.user;
    const pass = req.query.pass;

    let loggedInUser = await db.query(
        `SELECT * FROM users WHERE user = $user AND pass = $pass`,
        {
            bind: {
                user: user,
                pass: pass,
            },
            type: QueryTypes.SELECT,
            model: User,
        }
    );

    res.send(JSON.stringify(loggedInUser));
    res.end();
}
----

=== How does this work?

include::../../common/fix/prepared-statements.adoc[]
