user read

This commit is contained in:
Yanis Rigaudeau 2022-10-14 19:18:28 +02:00
parent 5538a042df
commit b2024bf4aa
Signed by: yanis
GPG Key ID: 4DD2841DF1C94D83
5 changed files with 42 additions and 13 deletions

View File

@ -4,7 +4,7 @@ import {
UserInfoWithPassword,
UserWithPasswordCtor,
} from '@core';
import { createHash, Hash } from 'crypto';
import { createHash } from 'crypto';
import { Entity } from './entity';
export class User extends Entity implements UserInfo {
@ -25,11 +25,21 @@ export class User extends Entity implements UserInfo {
}
export class UserWithPassword extends User implements UserInfoWithPassword {
password: Hash;
password: string;
constructor(raw: UserWithPasswordCtor) {
super(raw);
this.password = createHash('sha256').update(`${this.uuid}+${raw.password}`);
this.password = createHash('sha256')
.update(`${this.uuid}+${raw.password}`)
.digest('hex');
}
Info(): UserInfoWithPassword {
return {
uuid: this.uuid,
name: this.name,
password: this.password,
};
}
}

View File

@ -1,6 +1,6 @@
import { RequestHandler, Router } from 'express';
import { Services } from '../../app';
import { Create } from '../../functions/user';
import { Create, Read } from '../../functions/user';
import { getId } from './middleware';
export function LoginHandler(services: Services): RequestHandler {
@ -19,10 +19,21 @@ export function CreateHandler(services: Services): RequestHandler {
};
}
export function ReadHandler(services: Services): RequestHandler {
const readUser = Read(services);
return async (req, res) => {
const user = await readUser(getId(req), req.params.uuid);
res.send(user);
};
}
export function getRoutes(services: Services) {
const router = Router();
router.post('/login', LoginHandler(services));
router.get('/read/:uuid', ReadHandler(services));
router.post('/create', CreateHandler(services));
return router;

View File

@ -1,5 +1,5 @@
import { Collection, Db } from 'mongodb';
import { User } from '../../entities/user';
import { User, UserWithPassword } from '../../entities/user';
class UserModel {
private collection: Collection<User>;
@ -8,7 +8,7 @@ class UserModel {
this.collection = db.collection<User>('users');
}
public async create(tracker: string, user: User) {
public async create(tracker: string, user: UserWithPassword) {
await this.collection.insertOne(user);
return this.read(tracker, user.uuid);
}

View File

@ -1,6 +1,6 @@
import { CreateUserBody, UserInfo } from '@core';
import { Services } from '../app';
import { User } from '../entities/user';
import { User, UserWithPassword } from '../entities/user';
export function Create(
services: Services,
@ -8,7 +8,18 @@ export function Create(
const { userModel } = services;
return async (tracker, raw) => {
const user = await userModel.create(tracker, new User(raw));
const user = await userModel.create(tracker, new UserWithPassword(raw));
return user.Info();
};
}
export function Read(
services: Services,
): (tracker: string, uuid: string) => Promise<UserInfo> {
const { userModel } = services;
return async (tracker, uuid) => {
const user = await userModel.read(tracker, uuid);
return user.Info();
};
}

View File

@ -1,5 +1,4 @@
import { EntityInfo } from './entity';
import { Hash } from 'crypto';
export type UserInfo = {
uuid: string;
@ -7,7 +6,7 @@ export type UserInfo = {
};
export type UserInfoWithPassword = {
password: Hash;
password: string;
} & UserInfo;
export type User = UserInfo & EntityInfo;
@ -23,6 +22,4 @@ export type LoginUserBody = CreateUserBody;
export type UserCtor = Partial<User>;
export type UserWithPasswordCtor = {
password: string;
} & Partial<Omit<UserWithPassword, 'password'>>;
export type UserWithPasswordCtor = Partial<UserWithPassword>;