diff --git a/client/src/app/screens/screen-profile/screen-profile.html b/client/src/app/screens/screen-profile/screen-profile.html
index 7eb931c..ee40f38 100644
--- a/client/src/app/screens/screen-profile/screen-profile.html
+++ b/client/src/app/screens/screen-profile/screen-profile.html
@@ -6,7 +6,7 @@
- @if (transaction.receiverID == userID) {
+ @if (transaction.receiverID == this.api.currentUser) {
{{ transaction.senderID }}
}@else {
{{ transaction.receiverID }}
@@ -40,7 +40,7 @@
- @if (transaction.receiverID == userID) {
+ @if (transaction.receiverID == this.api.currentUser) {
{{ transaction.amount | currency }}
diff --git a/client/src/app/screens/screen-profile/screen-profile.ts b/client/src/app/screens/screen-profile/screen-profile.ts
index f16a718..c1ca96c 100644
--- a/client/src/app/screens/screen-profile/screen-profile.ts
+++ b/client/src/app/screens/screen-profile/screen-profile.ts
@@ -1,5 +1,5 @@
import { CommonModule, CurrencyPipe, DatePipe } from '@angular/common';
-import { Component, OnInit } from '@angular/core';
+import { Component, inject, OnInit } from '@angular/core';
import { APIService } from '../../services/api';
import Transaction from '@model/transaction';
import { Router } from '@angular/router';
@@ -13,12 +13,11 @@ import { Router } from '@angular/router';
export class ScreenProfile implements OnInit{
// TODO display real data
username = 'John Doe';
- userID = 'testuser';
balance = 200;
transactions!: Transaction[];
constructor(
- private api: APIService,
+ protected api: APIService,
private router: Router,
){}
diff --git a/client/src/app/screens/screen-receive/screen-receive.html b/client/src/app/screens/screen-receive/screen-receive.html
index 9b5df88..201925a 100644
--- a/client/src/app/screens/screen-receive/screen-receive.html
+++ b/client/src/app/screens/screen-receive/screen-receive.html
@@ -40,7 +40,7 @@
diff --git a/client/src/app/screens/screen-receive/screen-receive.ts b/client/src/app/screens/screen-receive/screen-receive.ts
index 6b7b67f..da09c80 100644
--- a/client/src/app/screens/screen-receive/screen-receive.ts
+++ b/client/src/app/screens/screen-receive/screen-receive.ts
@@ -2,6 +2,7 @@ import { Component, inject, TemplateRef } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { QrCodeComponent } from 'ng-qrcode';
+import { APIService } from '../../services/api';
@Component({
selector: 'app-screen-receive',
@@ -11,12 +12,12 @@ import { QrCodeComponent } from 'ng-qrcode';
})
export class ScreenReceive {
private modalService = inject(NgbModal);
+ api = inject(APIService);
- user = 'DemoUser';
amount: number = 0;
get shareableLink(): string {
const currentDomain = window.location.origin;
- return `${currentDomain}/send/${this.user}?amount=${this.amount}`;
+ return `${currentDomain}/send/${this.api.currentUser}?amount=${this.amount}`;
}
copyLink() {
diff --git a/client/src/app/services/api.ts b/client/src/app/services/api.ts
index b4ea88b..e69be4a 100644
--- a/client/src/app/services/api.ts
+++ b/client/src/app/services/api.ts
@@ -13,6 +13,9 @@ export class APIService {
private isAuthenticatedSubject = new BehaviorSubject
(false);
isAuthenticated$ = this.isAuthenticatedSubject.asObservable();
+ // TODO use real user here
+ currentUser = 'DemoUser';
+
constructor(private http: HttpClient){}
login(username: string, password: string): Observable{
diff --git a/server/package.json b/server/package.json
index 5e443bd..e2284f9 100644
--- a/server/package.json
+++ b/server/package.json
@@ -16,7 +16,8 @@
"teardown": "docker compose down",
"keygen": "ts-node src/scripts/keygen.ts",
"hash": "ts-node src/scripts/hash.ts",
- "verify": "ts-node src/scripts/verify.ts",
+ "verify-hash": "ts-node src/scripts/verify-hash.ts",
+ "create-user": "ts-node src/scripts/create-user.ts",
"dev": "npm run setup && npm run node; npm run teardown"
},
"dependencies": {
diff --git a/server/src/messages/User.ts b/server/src/messages/User.ts
index 057678d..3246358 100644
--- a/server/src/messages/User.ts
+++ b/server/src/messages/User.ts
@@ -1,4 +1,4 @@
-import User from "../model/user";
+import Account from "../model/user";
export class UserRequest{
constructor(
@@ -6,6 +6,6 @@ export class UserRequest{
}
export class UserResponse{
constructor(
- public user: User
+ public user: Account
){}
}
\ No newline at end of file
diff --git a/server/src/model/transaction.ts b/server/src/model/transaction.ts
index 5a84480..23eff8c 100644
--- a/server/src/model/transaction.ts
+++ b/server/src/model/transaction.ts
@@ -1,5 +1,5 @@
import { Table, Column, Model, CreatedAt, ForeignKey, BelongsTo} from 'sequelize-typescript';
-import User from './user';
+import Account from './user';
@Table
export default class Transaction extends Model{
@@ -10,18 +10,18 @@ export default class Transaction extends Model{
declare reference: string;
@Column
- @ForeignKey(()=> User)
+ @ForeignKey(()=> Account)
declare senderID: string;
- @BelongsTo(() => User, 'senderID')
- declare sender: User;
+ @BelongsTo(() => Account, 'senderID')
+ declare sender: Account;
@Column
- @ForeignKey(()=> User)
+ @ForeignKey(()=> Account)
declare receiverID: string;
- @BelongsTo(() => User, 'receiverID')
- declare receiver: User;
+ @BelongsTo(() => Account, 'receiverID')
+ declare receiver: Account;
@CreatedAt
declare date: Date;
diff --git a/server/src/model/user.ts b/server/src/model/user.ts
index 1085d81..471a017 100644
--- a/server/src/model/user.ts
+++ b/server/src/model/user.ts
@@ -1,4 +1,4 @@
-import { Table, Column, Model, CreatedAt, DataType, Scopes, DefaultScope} from 'sequelize-typescript';
+import { Table, Column, Model, DataType, Scopes, DefaultScope, DeletedAt, BelongsToMany, ForeignKey} from 'sequelize-typescript';
@DefaultScope(() => ({
attributes:{ exclude: ['password']}
@@ -9,10 +9,13 @@ import { Table, Column, Model, CreatedAt, DataType, Scopes, DefaultScope} from '
}
}))
@Table
-export default class User extends Model{
+export default class Account extends Model{
@Column({primaryKey: true, unique: true, allowNull: false})
- declare userID: string;
+ declare id: string;
+
+ @Column
+ declare isBusiness: boolean;
@Column
declare displayName: string;
@@ -22,8 +25,21 @@ export default class User extends Model{
@Column
declare password: string;
+
+ @DeletedAt
+ declare deletedAt: Date | null;
+
+}
+@Table
+export class BusinessOwnership extends Model {
+ @ForeignKey(() => Account)
+ @Column
+ ownerAccountId!: number;
- @CreatedAt
- declare creationDate: Date;
+ @ForeignKey(() => Account)
+ @Column
+ ownedAccountId!: number;
+ @DeletedAt
+ declare deletedAt: Date | null;
}
\ No newline at end of file
diff --git a/server/src/routes/auth.ts b/server/src/routes/auth.ts
index df9ef2f..d88e0e4 100644
--- a/server/src/routes/auth.ts
+++ b/server/src/routes/auth.ts
@@ -1,7 +1,7 @@
import { compare } from 'bcrypt';
import express from 'express';
import { logger } from '../util/logging';
-import User from '../model/user';
+import Account from '../model/user';
import { Scope } from '../util/db';
import { getJWT, requireAuth } from '../util/auth';
import { LoginRequest } from '../messages/Login';
@@ -12,7 +12,7 @@ const router = express.Router();
router.post('/login', async (req, res) => {
try {
const data : LoginRequest = req.body;
- const user = await User.scope(Scope.withPassword).findOne({where: { userID: data.username}});
+ const user = await Account.scope(Scope.withPassword).findOne({where: { id: data.username}});
if (!user) return res.status(401).json(new Msg('Invalid credentials'));
const isMatch = await compare(data.password, user.password);
if (!isMatch) return res.status(401).json(new Msg('Invalid credentials'));
diff --git a/server/src/routes/send.ts b/server/src/routes/send.ts
index ff03dd3..96ad1ff 100644
--- a/server/src/routes/send.ts
+++ b/server/src/routes/send.ts
@@ -1,7 +1,7 @@
import express from 'express';
import { logger } from '../util/logging';
import { requireAuth } from '../util/auth';
-import User from '../model/user';
+import Account from '../model/user';
import { db } from '../util/db';
import Transaction from '../model/transaction';
import { SendRequest, SendResponse} from '../messages/Send';
@@ -12,9 +12,9 @@ const router = express.Router();
router.post('/', requireAuth, async (req, res) => {
try {
- const sender = res.locals.user as User;
+ const sender = res.locals.user as Account;
const data : SendRequest = req.body;
- const recipient = await User.findOne({where: {userID: data.recipientID}})
+ const recipient = await Account.findOne({where: {id: data.recipientID}})
if ( Number(data.amount) <= 0) {
return res.status(400).json(new Err('Invalid transfer amount'));
}
@@ -31,8 +31,8 @@ router.post('/', requireAuth, async (req, res) => {
await recipient.increment({balance: data.amount});
await Transaction.create({
amount: data.amount,
- senderID: sender.userID,
- receiverID: recipient.userID,
+ senderID: sender.id,
+ receiverID: recipient.id,
reference: data.reference
});
})
diff --git a/server/src/scripts/create-user.ts b/server/src/scripts/create-user.ts
new file mode 100644
index 0000000..4b8bda3
--- /dev/null
+++ b/server/src/scripts/create-user.ts
@@ -0,0 +1,12 @@
+import { db, testConnection } from "../util/db";
+import { hash } from "bcrypt";
+import Account from '../model/user';
+(async () =>{
+ await testConnection();
+ await Account.create({
+ id: process.argv[2],
+ displayName: process.argv[3],
+ balance: process.argv[4],
+ password: await hash(process.argv[5], 10),
+ })
+})();
\ No newline at end of file
diff --git a/server/src/scripts/verify.ts b/server/src/scripts/verify-hash.ts
similarity index 100%
rename from server/src/scripts/verify.ts
rename to server/src/scripts/verify-hash.ts
diff --git a/server/src/util/auth.ts b/server/src/util/auth.ts
index 118bab2..3415cad 100644
--- a/server/src/util/auth.ts
+++ b/server/src/util/auth.ts
@@ -1,5 +1,5 @@
import { NextFunction, Request, Response } from "express";
-import User from "../model/user"
+import Account from "../model/user"
import { importJWK, SignJWT, jwtVerify } from "jose";
@@ -9,9 +9,9 @@ async function setKeyFromEnv() {
key = await importJWK(JSON.parse(process.env.FM_PRIVATE_KEY));
}
-async function getJWT(user: User){
+async function getJWT(user: Account){
let jwt = await new SignJWT()
- .setSubject(user.userID)
+ .setSubject(user.id)
.setProtectedHeader({ alg: 'HS256' })
.setIssuedAt()
.sign(key);
@@ -26,7 +26,7 @@ async function requireAuth(req: Request, res: Response, next: NextFunction) {
return res.status(401).json({ error: 'Unauthorized: No token provided' });
}
const jwt= await jwtVerify(token, key);
- const user = await User.findOne({where: { userID: jwt.payload.sub}});
+ const user = await Account.findOne({where: { id: jwt.payload.sub}});
if (!user) {
return res.status(401).json({ error: 'Unauthorized: User not found' });
}
diff --git a/server/src/util/db.ts b/server/src/util/db.ts
index ac904f1..ab7320f 100644
--- a/server/src/util/db.ts
+++ b/server/src/util/db.ts
@@ -1,6 +1,6 @@
import { Sequelize } from 'sequelize-typescript';
import { logger } from './logging';
-import User from '../model/user';
+import Account, { BusinessOwnership } from '../model/user';
import Transaction from '../model/transaction';
enum Scope{
@@ -19,7 +19,7 @@ const db = new Sequelize({
logging: logger.debug.bind(logger),
});
-db.addModels([User, Transaction ])
+db.addModels([Account, BusinessOwnership, Transaction ])
// Test the connection
async function testConnection() {