running now
This commit is contained in:
parent
85cd083750
commit
c17bb05f0a
25 changed files with 156 additions and 114 deletions
|
|
@ -1,9 +1,9 @@
|
|||
import { UserRepository } from "../repositories/UserRepository";
|
||||
import { encrypt } from "../utils/encryptionUtils";
|
||||
import { ValidationError, UnauthorizedError } from "../errors/httpErrors";
|
||||
import { UserDtoEntityMapper } from "../mappers/UserDtoEntityMapper";
|
||||
import { LoginResponseDto } from "../dtos/LoginResponseDto";
|
||||
import { LoginRequestDto } from "../dtos/LoginRequestDto";
|
||||
import { UserRepository } from "../repositories/UserRepository.js";
|
||||
import { encrypt } from "../utils/encryptionUtils.js";
|
||||
import { ValidationError, UnauthorizedError } from "../errors/httpErrors.js";
|
||||
import { UserDtoEntityMapper } from "../mappers/UserDtoEntityMapper.js";
|
||||
import { LoginResponseDto } from "../dtos/LoginResponseDto.js";
|
||||
import { LoginRequestDto } from "../dtos/LoginRequestDto.js";
|
||||
|
||||
export class AuthController {
|
||||
constructor(
|
||||
|
|
@ -12,25 +12,27 @@ export class AuthController {
|
|||
) {}
|
||||
|
||||
async login(loginRequest : LoginRequestDto): Promise<LoginResponseDto> {
|
||||
const userName :string = loginRequest.userName;
|
||||
const password :string = loginRequest.password;
|
||||
const userName :string|undefined = loginRequest.userName;
|
||||
const password :string|undefined = loginRequest.password;
|
||||
if (!userName || !password) {
|
||||
throw new ValidationError("Username and password are required");
|
||||
}
|
||||
|
||||
// Find user by userName
|
||||
const user = await this.userRepository.findByUserName(userName);
|
||||
// check user before trying to access password!
|
||||
if(!user){
|
||||
throw new UnauthorizedError("Invalid username or password");
|
||||
}
|
||||
// Compare password
|
||||
const passwordMatches = encrypt.comparepassword(user.password, password);
|
||||
if (!passwordMatches || !user ) {
|
||||
if (!passwordMatches) {
|
||||
throw new UnauthorizedError("Invalid username or password");
|
||||
}
|
||||
|
||||
// Create JWT
|
||||
const token = encrypt.generateToken({
|
||||
id: user.id,
|
||||
userName: user.userName,
|
||||
role: user.role,
|
||||
});
|
||||
|
||||
const responseDto = new LoginResponseDto();
|
||||
|
|
|
|||
|
|
@ -1,10 +1,9 @@
|
|||
import { ValidationError, ConflictError } from "../errors/httpErrors";
|
||||
import { CreateUserRequestDto } from "../dtos/CreateUserRequestDto";
|
||||
import { UserDto } from "../dtos/UserDto";
|
||||
import { encrypt } from "../utils/encryptionUtils";
|
||||
import { UserRepository } from "../repositories/UserRepository";
|
||||
import { UserDtoEntityMapper } from "../mappers/UserDtoEntityMapper";
|
||||
import { isNeitherNullNorEmpty } from "../utils/stringUtils";
|
||||
import { ValidationError, ConflictError } from "../errors/httpErrors.js";
|
||||
import { CreateUserRequestDto } from "../dtos/CreateUserRequestDto.js";
|
||||
import { UserDto } from "../dtos/UserDto.js";
|
||||
import { encrypt } from "../utils/encryptionUtils.js";
|
||||
import { UserRepository } from "../repositories/UserRepository.js";
|
||||
import { UserDtoEntityMapper } from "../mappers/UserDtoEntityMapper.js";
|
||||
|
||||
export class UserController {
|
||||
constructor(
|
||||
|
|
@ -14,23 +13,30 @@ export class UserController {
|
|||
|
||||
async createUser(dto: CreateUserRequestDto): Promise<UserDto> {
|
||||
// check mandatory fields
|
||||
if (!isNeitherNullNorEmpty(dto.userData.email)) {
|
||||
if(!dto.userData){
|
||||
throw new ValidationError("User data is required")
|
||||
}
|
||||
const email = dto.userData.email;
|
||||
if (!email || (email && email.length == 0)) {
|
||||
throw new ValidationError("Email is required");
|
||||
}
|
||||
if (!isNeitherNullNorEmpty(dto.userData.userName)) {
|
||||
throw new ValidationError("Username is required");
|
||||
}
|
||||
if(!isNeitherNullNorEmpty(dto.password){
|
||||
const password = dto.password;
|
||||
if(!password || (password && password.length == 0)){
|
||||
throw new ValidationError("Password is required");
|
||||
}
|
||||
// user name must be uniqu
|
||||
const existingUser = await this.userRepository.findByUserName(dto.userData.email);
|
||||
const userName = dto.userData.userName;
|
||||
if (!userName|| (userName && userName.length == 0)){
|
||||
throw new ValidationError("Username is required");
|
||||
}
|
||||
|
||||
// user name must be unique
|
||||
const existingUser = await this.userRepository.findByUserName(userName);
|
||||
if (existingUser) {
|
||||
throw new ConflictError("User with this user name already exists");
|
||||
}
|
||||
|
||||
const userEntity = this.mapper.toEntity(dto.userData);
|
||||
userEntity.password = await encrypt.encryptpass(dto.password);
|
||||
userEntity.password = await encrypt.encryptpass(password);
|
||||
|
||||
const savedUser = await this.userRepository.create(userEntity);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue