-
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.
Hashing passwords works well | Login and Register well with swagger
- Loading branch information
Showing
17 changed files
with
206 additions
and
201 deletions.
There are no files selected for viewing
Binary file not shown.
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
79 changes: 49 additions & 30 deletions
79
server/src/application/auth/register/register.use-case.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 |
---|---|---|
@@ -1,33 +1,52 @@ | ||
// import 'reflect-metadata'; | ||
// import { AuthResponseDto } from '@contracts/dtos/auth'; | ||
// import { CreateUserDto } from '@contracts/dtos/users'; | ||
// import { TYPES } from '@infrastructure/shared/ioc/types'; | ||
// import { inject, injectable } from 'inversify'; | ||
// import { BaseUseCase } from '@application/shared'; | ||
// import { UserRepository } from '@domain/repositories/user.repository'; | ||
// import { log } from 'console'; | ||
// import { TokenProviderDomainService } from '@domain/shared/services/token-provider.domain-service'; | ||
import 'reflect-metadata'; | ||
import { AuthResponseDto } from '@contracts/dtos/auth'; | ||
import { CreateUserDto } from '@contracts/dtos/users'; | ||
import { BaseUseCase, UseCase } from '@application/shared'; | ||
import { TokenProviderDomainService } from '@domain/shared/services/token-provider.domain-service'; | ||
import { UserRepository } from '@domain/entities'; | ||
import { JwtPayload } from '@contracts/services/IJwt'; | ||
import { HasherDomainService } from '@domain/shared/services'; | ||
import { Hash } from 'crypto'; | ||
|
||
// @injectable() | ||
// class RegisterUsecase extends BaseUseCase<CreateUserDto, AuthResponseDto> { | ||
// private _userRepository: UserRepository, | ||
// private _jwtService: TokenProviderDomainService, | ||
@UseCase() | ||
class RegisterUsecase extends BaseUseCase<CreateUserDto, AuthResponseDto> { | ||
private _userRepository: UserRepository; | ||
private _jwtService: TokenProviderDomainService; | ||
private _hasherService: HasherDomainService; | ||
|
||
// public async performOperation( | ||
// request: CreateUserDto, | ||
// ): Promise<AuthResponseDto> { | ||
// const user = request.toEntity(); | ||
// const createdUser = await this._userRepository.saveUser(user); | ||
// log('createdUser', createdUser); | ||
// const result: AuthResponseDto = { | ||
// token: this._jwtService.({ userId: createdUser.id as string }), | ||
// tokenExpiration: new Date(Date.now() + 1000 * 60 * 60), | ||
// // refreshToken: this._jwtService.sign({ userId: createdUser.id as string }), | ||
// // refreshTokenExpiration: new Date(Date.now() + 1000 * 60 * 60 * 24), | ||
// userDetails: createdUser, | ||
// }; | ||
// return result; | ||
// } | ||
// } | ||
constructor( | ||
userRepository: UserRepository, | ||
jwtService: TokenProviderDomainService, | ||
hasherService: HasherDomainService, | ||
) { | ||
super(); | ||
this._userRepository = userRepository; | ||
this._jwtService = jwtService; | ||
this._hasherService = hasherService; | ||
} | ||
|
||
// export { RegisterUsecase }; | ||
public async performOperation( | ||
request: CreateUserDto, | ||
): Promise<AuthResponseDto> { | ||
const user = request.toEntity(); | ||
user.password = await HasherDomainService.hash(user.password); | ||
const createdUser = await this._userRepository.saveUser(user); | ||
const payload = { | ||
email: { value: user.email }, | ||
roles: user.roles.map(role => ({ value: role })), | ||
username: { value: user.username }, | ||
userUuid: { value: user.id as string }, | ||
} as JwtPayload; | ||
|
||
const token = this._jwtService.createAccessToken(payload); | ||
|
||
const result: AuthResponseDto = { | ||
token, | ||
tokenExpiration: new Date(Date.now() + 1000 * 60 * 60), | ||
userDetails: createdUser, | ||
}; | ||
return result; | ||
} | ||
} | ||
|
||
export { RegisterUsecase }; |
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
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 |
---|---|---|
@@ -1,18 +0,0 @@ | ||
import bcrypt from 'bcrypt'; | ||
import { IHasherService } from '@contracts/services/IHasher'; | ||
import { injectable } from 'inversify'; | ||
|
||
@injectable() | ||
class HasherService implements IHasherService { | ||
private readonly saltRounds = 10; | ||
|
||
async hash(password: string): Promise<string> { | ||
return bcrypt.hash(password, this.saltRounds); | ||
} | ||
|
||
async compare(password: string, hash: string): Promise<boolean> { | ||
return bcrypt.compare(password, hash); | ||
} | ||
} | ||
|
||
export { HasherService }; | ||
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 |
---|---|---|
@@ -1,44 +1,44 @@ | ||
import 'reflect-metadata'; | ||
import { Container } from 'inversify'; | ||
import { TYPES } from './types'; | ||
import { JwtService } from '@infrastructure/shared/jwt/jwt.service.impl'; | ||
import { UserMapper, UserRepository } from '@infrastructure/users'; | ||
|
||
import { HasherService } from '@infrastructure/shared/hasher/hasher.service'; | ||
import { AuthController } from '@/web/rest/controllers'; | ||
import { LoginUseCase } from '@application/auth/login/login.use-case'; | ||
import { RegisterUsecase } from '@application/auth/register/register.use-case'; | ||
import { DataSource } from 'typeorm'; | ||
import { appDataSource } from '@infrastructure/shared/persistence/data-source'; | ||
import { PostsController } from '@/web/rest/controllers/posts.controller'; | ||
import { PostRepositoryImp } from '@infrastructure/posts'; | ||
import { CreatePostUseCase } from '@application/post'; | ||
import { FindAllPostUseCase } from '@application/post/find-all/find-all-post.usecase'; | ||
|
||
const container = new Container(); | ||
// Bind the extrernal dependencies | ||
container.bind(TYPES.IJwtService).to(JwtService); | ||
container.bind(TYPES.IHasherService).to(HasherService); | ||
container.bind(TYPES.IUserMapper).to(UserMapper); | ||
|
||
// Inject the repositories | ||
container.bind(TYPES.IUserRepository).to(UserRepository); | ||
container.bind(TYPES.IPostRepository).to(PostRepositoryImp); | ||
|
||
// Inject input ports | ||
container.bind(TYPES.ILoginInputPort).to(LoginUseCase); | ||
container.bind(TYPES.IRegisterInputPort).to(RegisterUsecase); | ||
|
||
container.bind(TYPES.ICreatePostInputPort).to(CreatePostUseCase); | ||
container.bind(TYPES.IFindAllPostInputPort).to(FindAllPostUseCase); | ||
|
||
// Bind the controllers | ||
container.bind(TYPES.IAuthController).to(AuthController); | ||
container.bind(TYPES.IPostController).to(PostsController); | ||
|
||
// Bind ApplicationRouter | ||
|
||
// bind the datastore | ||
container.bind<DataSource>(DataSource).toConstantValue(appDataSource); | ||
|
||
export { container }; | ||
// import 'reflect-metadata'; | ||
// import { Container } from 'inversify'; | ||
// import { TYPES } from './types'; | ||
// import { JwtService } from '@infrastructure/shared/jwt/jwt.service.impl'; | ||
// import { UserMapper, UserRepository } from '@infrastructure/users'; | ||
|
||
// import { HasherService } from '@infrastructure/shared/hasher/hasher.service'; | ||
// import { AuthController } from '@/web/rest/controllers'; | ||
// import { LoginUseCase } from '@application/auth/login/login.use-case'; | ||
// import { RegisterUsecase } from '@application/auth/register/register.use-case'; | ||
// import { DataSource } from 'typeorm'; | ||
// import { appDataSource } from '@infrastructure/shared/persistence/data-source'; | ||
// import { PostsController } from '@/web/rest/controllers/posts.controller'; | ||
// import { PostRepositoryImp } from '@infrastructure/posts'; | ||
// import { CreatePostUseCase } from '@application/post'; | ||
// import { FindAllPostUseCase } from '@application/post/find-all/find-all-post.usecase'; | ||
|
||
// const container = new Container(); | ||
// // Bind the extrernal dependencies | ||
// container.bind(TYPES.IJwtService).to(JwtService); | ||
// container.bind(TYPES.IHasherService).to(HasherService); | ||
// container.bind(TYPES.IUserMapper).to(UserMapper); | ||
|
||
// // Inject the repositories | ||
// container.bind(TYPES.IUserRepository).to(UserRepository); | ||
// container.bind(TYPES.IPostRepository).to(PostRepositoryImp); | ||
|
||
// // Inject input ports | ||
// container.bind(TYPES.ILoginInputPort).to(LoginUseCase); | ||
// container.bind(TYPES.IRegisterInputPort).to(RegisterUsecase); | ||
|
||
// container.bind(TYPES.ICreatePostInputPort).to(CreatePostUseCase); | ||
// container.bind(TYPES.IFindAllPostInputPort).to(FindAllPostUseCase); | ||
|
||
// // Bind the controllers | ||
// container.bind(TYPES.IAuthController).to(AuthController); | ||
// container.bind(TYPES.IPostController).to(PostsController); | ||
|
||
// // Bind ApplicationRouter | ||
|
||
// // bind the datastore | ||
// container.bind<DataSource>(DataSource).toConstantValue(appDataSource); | ||
|
||
// export { container }; |
Oops, something went wrong.