Skip to content
This repository was archived by the owner on Nov 10, 2024. It is now read-only.

Commit

Permalink
Critical bugfix
Browse files Browse the repository at this point in the history
mikayelgr committed Jan 6, 2022
1 parent 1e35fdc commit e2c6326
Showing 2 changed files with 18 additions and 16 deletions.
10 changes: 7 additions & 3 deletions src/controllers/auth/login.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { isNil } from "lodash";
import { userDao } from "container";
import { createJwt } from "util/jwt";
import bcrypt from "@node-rs/bcrypt";
import getFirst from "util/sql/getFirst";
import type { User } from "dao/entities/User";
import type { Request, Response } from "express";

const login = async (req: Request, res: Response) => {
const { password } = req.body;
const email = String(req.body.email).toLowerCase();
const email = req.body.email.toLowerCase();

// Find the user by email
const user = await userDao.getUserByEmail(email);
const user = await getFirst<Partial<User>>(
"SELECT id, password FROM users WHERE email = $1",
[email]
);

if (!isNil(user)) {
const correctPassword = await bcrypt.verify(password, user?.password!);
24 changes: 11 additions & 13 deletions src/db/dao/UserDaoImpl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { nth } from "lodash";
import { Postgres } from "db/pg";
import { Service } from "typedi";
import { Logger } from "util/logger";
@@ -11,13 +10,12 @@ export class UserDaoImpl implements UserDao {
constructor(private readonly db: Postgres, private readonly logger: Logger) {}

public async getUserByEmail(email: string): Promise<Partial<User> | null> {
// prettier-ignore
const { rows: { 0: { id } } } = await this.db.query(
"SELECT id FROM users WHERE email = $1", [
email,
]);
const { rows } = await this.db.query(
"SELECT id FROM users WHERE email = $1",
[email]
);

const user = await this.getUserById(id);
const user = await this.getUserById(rows[0]?.id);
return user;
}

@@ -27,7 +25,7 @@ export class UserDaoImpl implements UserDao {

public async createUser(user: User): Promise<Partial<User> | null> {
try {
const result = await this.db.query(
const { rows } = await this.db.query(
`
INSERT INTO users (
email,
@@ -46,7 +44,7 @@ export class UserDaoImpl implements UserDao {
]
);

return nth(result.rows, 0);
return rows[0];
} catch (error: any) {
if (error?.code === "23505") throw new DuplicateRecordError(error);
else {
@@ -57,22 +55,22 @@ export class UserDaoImpl implements UserDao {
}

public async getUserById(id: string): Promise<Partial<User> | null> {
const result = await this.db.query(
const { rows } = await this.db.query(
"SELECT id, bio, username, last_name, first_name, created_at FROM users WHERE id = $1;",
[id]
);

return nth(result.rows, 0);
return rows[0];
}

// prettier-ignore
public async getUserByUsername(username: string): Promise<Partial<User> | null> {
const { rows: { 0: { id } } } = await this.db.query(
const { rows } = await this.db.query(
"SELECT id FROM users WHERE username = $1",
[username]
);

const user = await this.getUserById(id);
const user = await this.getUserById(rows[0]?.id);
return user;
}
}

0 comments on commit e2c6326

Please sign in to comment.