From f5ae9ac9e6c988d7fbc6b9f64284fef7ce479b96 Mon Sep 17 00:00:00 2001 From: eneller Date: Sat, 7 Mar 2026 14:48:19 +0100 Subject: [PATCH] basic login/logout --- .../screen-profile/screen-profile.html | 1 + .../screens/screen-profile/screen-profile.ts | 16 ++++++++++++++- client/src/app/services/api.ts | 3 +++ server/src/model/transaction.ts | 12 +++++------ server/src/model/user.ts | 10 +++++----- server/src/routes/auth.ts | 20 ++++++++++++++++--- server/src/routes/transactions.ts | 3 ++- 7 files changed, 49 insertions(+), 16 deletions(-) diff --git a/client/src/app/screens/screen-profile/screen-profile.html b/client/src/app/screens/screen-profile/screen-profile.html index 7b99d45..7eb931c 100644 --- a/client/src/app/screens/screen-profile/screen-profile.html +++ b/client/src/app/screens/screen-profile/screen-profile.html @@ -11,6 +11,7 @@

{{ balance | currency}}

+ diff --git a/client/src/app/screens/screen-profile/screen-profile.ts b/client/src/app/screens/screen-profile/screen-profile.ts index 2705b2d..6f44ce0 100644 --- a/client/src/app/screens/screen-profile/screen-profile.ts +++ b/client/src/app/screens/screen-profile/screen-profile.ts @@ -2,6 +2,7 @@ import { CommonModule, CurrencyPipe, DatePipe } from '@angular/common'; import { Component, OnInit } from '@angular/core'; import { APIService } from '../../services/api'; import Transaction from '@model/transaction'; +import { Router } from '@angular/router'; @Component({ selector: 'app-screen-profile', @@ -15,7 +16,10 @@ export class ScreenProfile implements OnInit{ balance = 200; transactions!: Transaction[]; - constructor(private api: APIService){} + constructor( + private api: APIService, + private router: Router, + ){} ngOnInit(): void { // FIXME transactions displaying delayed (only on second nav click) @@ -28,5 +32,15 @@ export class ScreenProfile implements OnInit{ }, }) } + logOut(){ + this.api.logout().subscribe({ + next: () => { + this.router.navigate(['login']) + }, + error: (err) => { + console.error('Error logging out:', err) + } + }) + } } diff --git a/client/src/app/services/api.ts b/client/src/app/services/api.ts index 507b0d8..f720f94 100644 --- a/client/src/app/services/api.ts +++ b/client/src/app/services/api.ts @@ -17,4 +17,7 @@ export class APIService { login(username: string, password: string): Observable{ return this.http.post(this.apiUrl + '/auth/login',{ 'username': username, 'password': password}); } + logout(): Observable{ + return this.http.post(this.apiUrl + '/auth/logout', {}); + } } diff --git a/server/src/model/transaction.ts b/server/src/model/transaction.ts index 5b5400e..56228da 100644 --- a/server/src/model/transaction.ts +++ b/server/src/model/transaction.ts @@ -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; } \ No newline at end of file diff --git a/server/src/model/user.ts b/server/src/model/user.ts index e5c7b5a..8bc839a 100644 --- a/server/src/model/user.ts +++ b/server/src/model/user.ts @@ -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; } \ No newline at end of file diff --git a/server/src/routes/auth.ts b/server/src/routes/auth.ts index 4212c57..e9a2d81 100644 --- a/server/src/routes/auth.ts +++ b/server/src/routes/auth.ts @@ -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; diff --git a/server/src/routes/transactions.ts b/server/src/routes/transactions.ts index 9484404..3e3c239 100644 --- a/server/src/routes/transactions.ts +++ b/server/src/routes/transactions.ts @@ -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' }); } });