Skip to content

Commit

Permalink
Add name to the token and display it in the welcome screen
Browse files Browse the repository at this point in the history
  • Loading branch information
ab-elhaddad committed Oct 30, 2023
1 parent 37f5a62 commit ecb5578
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/controllers/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ export const signIn = {
post: async (req: Request, res: Response, next: NextFunction) => {
try {
const { u_email, u_password } = req.body;
const userID = await Users.signIn(u_email, u_password);
res.cookie('token', jwt.sign({ id: userID, email: u_email }, config.jwtSecretKey)).redirect('/welcome');
const { u_id, u_name } = await Users.signIn(u_email, u_password);
res.cookie('token', jwt.sign({ id: u_id, email: u_email, name: u_name }, config.jwtSecretKey)).redirect('/welcome');
} catch (err) {
res.locals.err = err;
next();
Expand Down
6 changes: 3 additions & 3 deletions src/models/users.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ class Users {
/**
* @returns The *id* of the user.
*/
static async signIn(email: string, password: string): Promise<number> {
static async signIn(email: string, password: string): Promise<{ u_id: number; u_name: string; }> {
const user = await prisma.user.findFirst({
where: { u_email: email },
select: { u_password: true, u_is_confirmed: true, u_id: true }
select: { u_password: true, u_is_confirmed: true, u_id: true, u_name: true }
});
if (user === null) throw { message: `Wrong Email or Password!`, statusCode: 403 };
if (!user.u_is_confirmed) throw { message: `Wrong Email or Password!`, statusCode: 403 }; // Forbidden

if (!bcrypt.compareSync(password, user?.u_password as string)) throw { message: `Wrong Email or Password!`, statusCode: 403 }; // Forbidden

return user.u_id;
return { u_id: user.u_id, u_name: user.u_name };
}

static async updatePassword(userID: number, newPassword: string): Promise<void> {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/mainRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const mainRouter = (app: Application) => {
res.redirect('/');
else {
const user = jwt?.decode(token) as jwt.JwtPayload;
res.render('welcome', { name: user.email });
res.render('welcome', { name: user.name });
}
});
userRouter(app);
Expand Down

0 comments on commit ecb5578

Please sign in to comment.