This repository has been archived by the owner on May 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create user entity, db stuff and service for createing new users by http
- Loading branch information
1 parent
13818bf
commit a9cd6e9
Showing
14 changed files
with
473 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Inject } from '@nestjs/common' | ||
import { TypeOrmModuleOptions, TypeOrmOptionsFactory } from '@nestjs/typeorm' | ||
|
||
import { Configuration } from '@back/config/Configuration' | ||
|
||
export class DbOptionsFactory implements TypeOrmOptionsFactory { | ||
public constructor( | ||
@Inject(Configuration) private readonly config: Configuration, | ||
) {} | ||
|
||
public createTypeOrmOptions(): TypeOrmModuleOptions { | ||
return { | ||
type: 'mysql', | ||
host: this.config.getStringOrElse('DB_HOST', '127.0.0.1'), | ||
port: this.config.getNumberOrElse('DB_PORT', 3306), | ||
username: this.config.getStringOrElse('DB_USER', 'admin'), | ||
password: this.config.getStringOrElse('DB_PASSWORD', 'admin'), | ||
database: this.config.getStringOrElse('DB_NAME', 'oncohelp'), | ||
entities: [__dirname + `/../**/*.{entity,vo}.{ts,js}`], | ||
synchronize: !this.config.getBooleanOrElse('PRODUCTION_READY', true), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Injectable } from '@nestjs/common' | ||
import { InjectEntityManager } from '@nestjs/typeorm' | ||
import { EntityManager } from 'typeorm' | ||
|
||
@Injectable() | ||
export class EntitySaver { | ||
public constructor( | ||
@InjectEntityManager() private readonly em: EntityManager, | ||
) {} | ||
|
||
public async save<Entity>(...entities: Entity[]): Promise<void> { | ||
await this.em.save(entities) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common' | ||
import { TypeOrmModule } from '@nestjs/typeorm' | ||
|
||
import { ConfigModule } from '@back/config/config.module' | ||
|
||
import { DbOptionsFactory } from './DbOptionsFactory' | ||
import { EntitySaver } from './EntitySaver' | ||
|
||
@Module({ | ||
imports: [ | ||
TypeOrmModule.forRootAsync({ | ||
imports: [ConfigModule], | ||
useClass: DbOptionsFactory, | ||
}), | ||
], | ||
providers: [EntitySaver], | ||
exports: [EntitySaver], | ||
}) | ||
export class DbModule implements NestModule { | ||
public configure(consumer: MiddlewareConsumer) { | ||
// pass | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Injectable } from '@nestjs/common' | ||
|
||
import { EntitySaver } from '@back/db/EntitySaver' | ||
|
||
import { User } from '../domain/User.entity' | ||
import { PasswordEncoder } from '../infrastructure/PasswordEncoder/PasswordEncoder' | ||
|
||
@Injectable() | ||
export class Registrator { | ||
public constructor( | ||
private readonly passwordEncoder: PasswordEncoder, | ||
private readonly entitySaver: EntitySaver, | ||
) {} | ||
|
||
public async signUp(login: string, password: string): Promise<void> { | ||
const user = new User(login) | ||
|
||
await user.changePassword(password, this.passwordEncoder) | ||
|
||
await this.entitySaver.save(user) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export class Profile { | ||
private name?: string | ||
|
||
public constructor(name?: string) { | ||
this.name = name | ||
} | ||
|
||
public changeName(newName: string) { | ||
if (newName.length < 1) { | ||
// TODO: throw error | ||
} | ||
|
||
this.name = newName | ||
} | ||
|
||
public removeName() { | ||
this.name = undefined | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Column, Entity, PrimaryColumn } from 'typeorm' | ||
|
||
import { PasswordEncoder } from '../infrastructure/PasswordEncoder/PasswordEncoder' | ||
import { Profile } from './Profile.vo' | ||
|
||
@Entity() | ||
export class User { | ||
@PrimaryColumn() | ||
public readonly login: string | ||
|
||
@Column(type => Profile) | ||
public readonly profile: Profile | ||
|
||
@Column() | ||
private password: string | undefined | ||
|
||
public constructor(login: string) { | ||
this.login = login | ||
|
||
this.profile = new Profile() | ||
} | ||
|
||
public async changePassword( | ||
rawPassword: string, | ||
encoder: PasswordEncoder, | ||
): Promise<void> { | ||
this.password = await encoder.encodePassword(rawPassword) | ||
} | ||
|
||
public async isPasswordValid( | ||
rawPassword: string, | ||
encoder: PasswordEncoder, | ||
): Promise<boolean> { | ||
return encoder.isPasswordValid(this.password, rawPassword) | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
back/src/user/infrastructure/PasswordEncoder/BcryptPasswordEncoder.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import * as bcrypt from 'bcryptjs' | ||
import { promisify } from 'util' | ||
|
||
import { PasswordEncoder } from './PasswordEncoder' | ||
|
||
export class BcryptPasswordEncoder implements PasswordEncoder { | ||
private readonly getHash: (raw: string, saltRounds: number) => Promise<string> | ||
private readonly compare: (raw: string, encoded: string) => Promise<boolean> | ||
|
||
public constructor() { | ||
this.getHash = promisify(bcrypt.hash) | ||
this.compare = promisify(bcrypt.compare) | ||
} | ||
|
||
public encodePassword(raw: string) { | ||
const SALT_ROUNDS = 12 | ||
|
||
return this.getHash(raw, SALT_ROUNDS) | ||
} | ||
|
||
public isPasswordValid(encoded: string, raw: string) { | ||
return this.compare(raw, encoded) | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
back/src/user/infrastructure/PasswordEncoder/PasswordEncoder.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export abstract class PasswordEncoder { | ||
public abstract encodePassword(raw: string, salt?: string): Promise<string> | ||
public abstract isPasswordValid( | ||
encoded: string, | ||
raw: string, | ||
salt?: string, | ||
): Promise<boolean> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.