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();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue