From b2024bf4aace30e2c1dce64c4720bc3410ed2e14 Mon Sep 17 00:00:00 2001 From: Yanis Rigaudeau Date: Fri, 14 Oct 2022 19:18:28 +0200 Subject: [PATCH] user read --- api/src/entities/user.ts | 16 +++++++++++++--- api/src/framework/express/user.ts | 13 ++++++++++++- api/src/framework/mongo/user.ts | 4 ++-- api/src/functions/user.ts | 15 +++++++++++++-- core/src/user.ts | 7 ++----- 5 files changed, 42 insertions(+), 13 deletions(-) diff --git a/api/src/entities/user.ts b/api/src/entities/user.ts index f82ce97..473d8e2 100644 --- a/api/src/entities/user.ts +++ b/api/src/entities/user.ts @@ -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, + }; } } diff --git a/api/src/framework/express/user.ts b/api/src/framework/express/user.ts index e60957d..0073a97 100644 --- a/api/src/framework/express/user.ts +++ b/api/src/framework/express/user.ts @@ -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; diff --git a/api/src/framework/mongo/user.ts b/api/src/framework/mongo/user.ts index 5bc96b6..8780f60 100644 --- a/api/src/framework/mongo/user.ts +++ b/api/src/framework/mongo/user.ts @@ -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; @@ -8,7 +8,7 @@ class UserModel { this.collection = db.collection('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); } diff --git a/api/src/functions/user.ts b/api/src/functions/user.ts index 8ad5bb1..fbee4ae 100644 --- a/api/src/functions/user.ts +++ b/api/src/functions/user.ts @@ -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 { + const { userModel } = services; + + return async (tracker, uuid) => { + const user = await userModel.read(tracker, uuid); return user.Info(); }; } diff --git a/core/src/user.ts b/core/src/user.ts index 66c0487..34ddf82 100644 --- a/core/src/user.ts +++ b/core/src/user.ts @@ -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; -export type UserWithPasswordCtor = { - password: string; -} & Partial>; +export type UserWithPasswordCtor = Partial;