Skip to content

Commit 2e14811

Browse files
committed
Typescript Bootcamp
1 parent 08975db commit 2e14811

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

rest-api/src/routes/login.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {NextFunction, Request, Response} from "express";
22
import {logger} from "../logger";
33
import {AppDataSource} from "../data-source";
44
import {User} from "../models/user";
5+
import {calculatePasswordHash} from "../utils";
56

67

78
export async function login(
@@ -34,7 +35,28 @@ export async function login(
3435
return;
3536
}
3637

38+
const passwordHash = await calculatePasswordHash(password, user.passwordSalt);
3739

40+
if (passwordHash != user.passwordHash) {
41+
const message = `Login denied.`;
42+
logger.info(`${message} - user with ${email} has entered the wrong password.`);
43+
response.status(403).json({message});
44+
return;
45+
}
46+
47+
logger.info(`User ${email} has now logged in.`);
48+
49+
const {pictureUrl, isAdmin} = user;
50+
51+
52+
53+
response.status(200).json({
54+
user: {
55+
email,
56+
pictureUrl,
57+
isAdmin
58+
}
59+
});
3860

3961
}
4062
catch(error) {

0 commit comments

Comments
 (0)