From ceab2dee0aa247d64e727b490376fcc2c6811aa1 Mon Sep 17 00:00:00 2001 From: eneller Date: Fri, 26 Jun 2026 12:49:17 +0200 Subject: [PATCH] refactor: messaging --- server/src/messages/Login.ts | 4 ++-- server/src/messages/Message.ts | 5 +++++ server/src/messages/Send.ts | 1 - server/src/routes/auth.ts | 23 +++++++++++------------ server/src/routes/send.ts | 14 +++++++------- 5 files changed, 25 insertions(+), 22 deletions(-) create mode 100644 server/src/messages/Message.ts diff --git a/server/src/messages/Login.ts b/server/src/messages/Login.ts index ad1dbd9..1c652e6 100644 --- a/server/src/messages/Login.ts +++ b/server/src/messages/Login.ts @@ -1,7 +1,7 @@ export class LoginRequest{ constructor( - username: string, - password: string + public username: string, + public password: string ){} } export class LoginResponse{ diff --git a/server/src/messages/Message.ts b/server/src/messages/Message.ts new file mode 100644 index 0000000..6ffc291 --- /dev/null +++ b/server/src/messages/Message.ts @@ -0,0 +1,5 @@ +export class GenericMessage{ + constructor( + public message: string + ){} +} \ No newline at end of file diff --git a/server/src/messages/Send.ts b/server/src/messages/Send.ts index 1c94fbc..56384f0 100644 --- a/server/src/messages/Send.ts +++ b/server/src/messages/Send.ts @@ -9,6 +9,5 @@ export class SendRequest{ export class SendResponse{ constructor( public balance: number, - public message: string ){} } \ No newline at end of file diff --git a/server/src/routes/auth.ts b/server/src/routes/auth.ts index 5689924..d764e02 100644 --- a/server/src/routes/auth.ts +++ b/server/src/routes/auth.ts @@ -2,34 +2,33 @@ import express from 'express'; import { logger } from '../util/logging'; import User from '../model/user'; import { getJWT, requireAuth } from '../util/auth'; -import { LoginRequest } from '@message/Login'; -import { SendRequest, SendResponse } from '@message/Send'; - +import { LoginRequest } from '../messages/Login'; +import { GenericMessage as Msg } from '../messages/Message'; const router = express.Router(); router.post('/login', async (req, res) => { try { - const { username, password } = req.body; - const user = await User.findOne({where: { userID: username}}); - if (!user) return res.status(401).json({ message: 'Invalid credentials' }); - const isMatch = (password == user.password); + const data : LoginRequest = req.body; + const user = await User.findOne({where: { userID: data.username}}); + if (!user) return res.status(401).json(new Msg('Invalid credentials')); + const isMatch = (data.password == user.password); //TODO hash passwords //const isMatch = await bcrypt.compare(password, user.passwordHash); - if (!isMatch) return res.status(401).json({ message: 'Invalid credentials' }); + if (!isMatch) return res.status(401).json(new Msg('Invalid credentials')); // successfully authenticated let jwt = await getJWT(user); res.cookie('jwt', jwt, { - httpOnly: true, // Prevent XSS - secure: process.env.NODE_ENV === 'production',// HTTPS only + httpOnly: true, // Prevent XSS + secure: process.env.NODE_ENV === 'production', // HTTPS only sameSite: 'strict', // CSRF protection maxAge: 86400000, // 1 day }); - res.json({ message: 'Logged in successfully' }); + res.json(new Msg('Logged in successfully')); }catch (err) { logger.error('Failed to authenticate:', err); - res.status(500).json({ message: 'Failed to authenticate' }); + res.status(500).json(new Msg('Failed to authenticate')); } }); diff --git a/server/src/routes/send.ts b/server/src/routes/send.ts index 4af957f..ff03dd3 100644 --- a/server/src/routes/send.ts +++ b/server/src/routes/send.ts @@ -5,6 +5,7 @@ import User from '../model/user'; import { db } from '../util/db'; import Transaction from '../model/transaction'; import { SendRequest, SendResponse} from '../messages/Send'; +import { GenericMessage as Err} from '../messages/Message'; const router = express.Router(); @@ -15,15 +16,14 @@ router.post('/', requireAuth, async (req, res) => { const data : SendRequest = req.body; const recipient = await User.findOne({where: {userID: data.recipientID}}) if ( Number(data.amount) <= 0) { - // TODO return SendResponse here and everywhere else in this file - return res.status(400).json({ error: 'Invalid transfer amount' }); + return res.status(400).json(new Err('Invalid transfer amount')); } if (!recipient) { - return res.status(404).json({ error: 'Recipient not found' }); + return res.status(404).json(new Err('Recipient not found' )); } if (Number(sender.balance) < Number(data.amount)){ - logger.error(`Insufficient balance: ${sender.balance} < ${data.amount}`) - return res.status(402).json({error: 'Insufficient balance'}) + logger.debug(`Insufficient balance: ${sender.balance} < ${data.amount}`) + return res.status(402).json(new Err('Insufficient balance')) } await db.transaction(async (t) =>{ @@ -36,10 +36,10 @@ router.post('/', requireAuth, async (req, res) => { reference: data.reference }); }) - return res.status(200).json({balance: sender.balance, amount: data.amount}); + return res.status(200).json(new SendResponse(sender.balance)) } catch (err) { logger.error('Failed to commit transaction:', err); - return res.status(500).json({ error: 'Failed to commit transaction' }); + return res.status(500).json(new Err('Failed to commit transaction' )); } });