basic login/logout

This commit is contained in:
eneller
2026-03-07 14:48:19 +01:00
parent 111d1a7b48
commit f5ae9ac9e6
7 changed files with 49 additions and 16 deletions

View File

@@ -4,22 +4,22 @@ import User from './user';
@Table
export default class Transaction extends Model{
@Column
amount!: number;
declare amount: number;
@Column
@ForeignKey(()=> User)
senderID!: string;
declare senderID: string;
@BelongsTo(() => User, 'senderID')
sender!: User;
declare sender: User;
@Column
@ForeignKey(()=> User)
receiverID!: string;
declare receiverID: string;
@BelongsTo(() => User, 'receiverID')
receiver!: User;
declare receiver: User;
@CreatedAt
date!: Date;
declare date: Date;
}

View File

@@ -4,18 +4,18 @@ import { Table, Column, Model, CreatedAt, DataType} from 'sequelize-typescript';
export default class User extends Model{
@Column({primaryKey: true, unique: true, allowNull: false})
userID!: string;
declare userID: string;
@Column
displayName!: string;
declare displayName: string;
@Column(DataType.DECIMAL(20,2))
balance!: number;
declare balance: number;
@Column
password!: string;
declare password: string;
@CreatedAt
creationDate!: Date;
declare creationDate: Date;
}

View File

@@ -1,14 +1,28 @@
import express from 'express';
import { logger } from '../util/logging';
import User from '../model/user';
const router = express.Router();
router.post('/login', async (req, res) => {
try {
res.json('abc');
} catch (err) {
console.error('Failed to authenticate:', err);
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);
//TODO hash passwords
//const isMatch = await bcrypt.compare(password, user.passwordHash);
if (!isMatch) return res.status(401).json({ message: 'Invalid credentials' });
res.json({ message: 'Logged in successfully' });
}catch (err) {
logger.error('Failed to authenticate:', err);
res.status(500).json({ error: 'Failed to authenticate' });
}
});
router.post('/logout', (req, res) => {
res.clearCookie('jwt');
res.json({ message: 'Logged out successfully' });
});
export default router;

View File

@@ -1,4 +1,5 @@
import express from 'express';
import { logger } from '../util/logging';
import Transaction from '../model/transaction';
const router = express.Router();
@@ -8,7 +9,7 @@ router.get('/', async (req, res) => {
const transactions = await Transaction.findAll({ limit: 10 });
res.json(transactions);
} catch (err) {
console.error('Failed to fetch transactions:', err);
logger.error('Failed to fetch transactions:', err);
res.status(500).json({ error: 'Failed to fetch transactions' });
}
});