diff --git a/src/controllers/users.controller.ts b/src/controllers/users.controller.ts index 836dc1c..01444c0 100644 --- a/src/controllers/users.controller.ts +++ b/src/controllers/users.controller.ts @@ -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(); diff --git a/src/models/users.model.ts b/src/models/users.model.ts index 4b2c351..133f137 100644 --- a/src/models/users.model.ts +++ b/src/models/users.model.ts @@ -46,17 +46,17 @@ class Users { /** * @returns The *id* of the user. */ - static async signIn(email: string, password: string): Promise { + 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 { diff --git a/src/routes/mainRouter.ts b/src/routes/mainRouter.ts index 34e397c..01a73b4 100644 --- a/src/routes/mainRouter.ts +++ b/src/routes/mainRouter.ts @@ -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);