error handler + login

This commit is contained in:
2022-10-15 22:04:27 +02:00
parent b2024bf4aa
commit f6a7415884
15 changed files with 197 additions and 43 deletions

View File

@ -1,5 +1,8 @@
import { Collection, Db } from 'mongodb';
import { LoginUserBody } from '../../../../core/src/user';
import { User, UserWithPassword } from '../../entities/user';
import log from '../../functions/logger';
import { generateHash } from '../../functions/password';
class UserModel {
private collection: Collection<User>;
@ -8,14 +11,42 @@ class UserModel {
this.collection = db.collection<User>('users');
}
public async login(tracker: string, data: LoginUserBody) {
const checkUser = await this.collection.findOne({
username: data.username,
});
if (checkUser === null) {
log(tracker, 'User Not Found');
return null;
}
const user = await this.collection.findOne({
uuid: checkUser.uuid,
password: generateHash(checkUser.uuid, data.password),
});
if (user === null) {
log(tracker, 'Wrong Password');
return null;
}
log(tracker, 'LOG IN', user);
return new User(user);
}
public async create(tracker: string, user: UserWithPassword) {
await this.collection.insertOne(user);
log(tracker, 'CREATE USER', user);
return this.read(tracker, user.uuid);
}
public async read(tracker: string, uuid: string) {
const user = await this.collection.findOne({ uuid });
return new User(user || { name: 'not found' });
if (user === null) {
log(tracker, 'User Not Found');
return null;
}
log(tracker, 'READ USER', user);
return new User(user);
}
}