All files / server/api/auth login.post.ts

100% Statements 21/21
100% Branches 7/7
100% Functions 0/0
100% Lines 21/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 221x 5x 5x 5x 5x 5x 5x 5x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
export default defineEventHandler(async event => {
    const body = await readBody(event);
    const email = String(body.email || "").trim().toLowerCase();
    const password = String(body.password || "");
    const db = database(event);
    await ready(db);
    const user = await db.prepare("SELECT id,email,password_hash,role,active,must_change_password FROM users WHERE email=?").bind(email).first<any>();
    if (!user || !user.active || !await verifyPassword(password, user.password_hash)) throw createError({
        statusCode: 401,
        statusMessage: "E-mail ou mot de passe incorrect"
    });
    await signSession(event, user);
    return {
        user: {
            id: user.id,
            email: user.email,
            role: user.role,
            mustChangePassword: Boolean(user.must_change_password)
        }
    };
});