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

Commit 48e6bab

Browse files
Refactored codebase
1 parent df9a922 commit 48e6bab

File tree

4 files changed

+17
-15
lines changed

4 files changed

+17
-15
lines changed

controllers/auth.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default class AuthController {
1313
user = await Users.getUserByEmail(req.body.email);
1414
if (user == null) {
1515
throw new Error("Email and password combination do not match a user in our system.");
16-
} else if (!await bcrypt.compare(req.body.password, String(user.password))) {
16+
} else if (!await bcrypt.compare(req.body.password, user.password)) {
1717
throw new Error("Email and password combination do not match a user in our system.");
1818
}
1919
} catch (e) {
@@ -28,12 +28,13 @@ export default class AuthController {
2828
}
2929

3030
static async signup(req: Request, res: Response) {
31-
let user: User = {};
31+
let user: User | undefined;
3232

3333
try {
34-
user.email = req.body.email;
35-
user.password = await bcrypt.hash(req.body.password, 10);
36-
user = await Users.save(user);
34+
user = await Users.save({
35+
email: req.body.email,
36+
password: await bcrypt.hash(req.body.password, 10)
37+
});
3738
} catch (e) {
3839
return ServerError(res, (e as Error).message);
3940
}

index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ process.on('SIGINT', () => process.exit());
3030
}
3131
});
3232
global.db = db;
33+
console.log("Connected to db");
3334

3435
// Configure server.
3536
const app = express();

middleware/auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default async function AuthMiddleware(req: Request, res: Response, next:
2222
user = await Users.getUserByID(id);
2323
if (user == null) {
2424
throw new Error('User is not Authenticated.');
25-
} else if (await Users.checkUserHasAuthToken(Number(user.id), token)) {
25+
} else if (await Users.checkUserHasAuthToken(user.id, token)) {
2626
throw new Error('Bad/Expired auth token.');
2727
}
2828
} catch (e) {

models/users.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
export interface User {
2-
id?: number;
3-
created_at?: Date;
4-
updated_at?: Date;
5-
email?: string;
6-
password?: string;
2+
id: number;
3+
created_at: Date;
4+
updated_at: Date;
5+
email: string;
6+
password: string;
77
}
88

99
export interface UserJSON extends Omit<User, "password"> {
@@ -62,17 +62,17 @@ export default class Users {
6262
return insertId.length == 1;
6363
}
6464

65-
static async save(user: User) {
65+
static async save(user: Partial<User>) {
6666
const insertId = await Users
6767
.getQueryBuilder()
6868
.insert(user);
6969
if (insertId.length != 1) throw new Error("An error occured while creating the user");
70-
return Users.getUserByID(insertId[0]) as User;
70+
return await Users.getUserByID(insertId[0]) as User;
7171
}
7272

73-
static toJSON(user: User): UserJSON {
73+
static toJSON(user: User) {
7474
const hiddenFields = ['password'];
7575
let data = Object.entries(user).filter(entry => !hiddenFields.includes(entry[0]));
76-
return Object.fromEntries(data);
76+
return Object.fromEntries(data) as UserJSON;
7777
}
7878
}

0 commit comments

Comments
 (0)