Skip to content

Commit

Permalink
feat: user role on login
Browse files Browse the repository at this point in the history
  • Loading branch information
Matheusafonsouza committed Jan 10, 2025
1 parent 8a7b7c9 commit 2c50c3b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
10 changes: 6 additions & 4 deletions src/auth/auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,24 +130,26 @@ describe('AuthService', () => {
it('should throw an UnauthorizedException for invalid credentials', async () => {
const email = '[email protected]';
const password = 'wrongPassword';
const role = UserRoles.User;
const user = new User();
user.email = email;
user.password = 'hashedPassword';

jest.spyOn(userRepository, 'findOneBy').mockResolvedValueOnce(user);
jest.spyOn(bcrypt, 'compare').mockResolvedValueOnce(false);

await expect(service.signIn({ email, password })).rejects.toThrow(
await expect(service.signIn({ email, password, role })).rejects.toThrow(
UnauthorizedException,
);

expect(userRepository.findOneBy).toHaveBeenCalledWith({ email });
expect(userRepository.findOneBy).toHaveBeenCalledWith({ email, role });
expect(bcrypt.compare).toHaveBeenCalledWith(password, user.password);
});

it('should return a signed token on successful login', async () => {
const email = '[email protected]';
const password = 'validPassword';
const role = UserRoles.User;
const user = new User();
user.id = '18ea976e-367b-4138-b68e-7aff3f7ae4de';
user.email = email;
Expand All @@ -159,14 +161,14 @@ describe('AuthService', () => {
jest.spyOn(userRepository, 'findOneBy').mockResolvedValueOnce(user);
jest.spyOn(bcrypt, 'compare').mockResolvedValueOnce(true);

const response = await service.signIn({ email, password });
const response = await service.signIn({ email, password, role });

expect(response).toEqual({
accessToken: 'access-token',
refreshToken: 'access-token',
});

expect(userRepository.findOneBy).toHaveBeenCalledWith({ email });
expect(userRepository.findOneBy).toHaveBeenCalledWith({ email, role });
expect(bcrypt.compare).toHaveBeenCalledWith(password, user.password);
expect(jwtService.signAsync).toHaveBeenCalledTimes(2);
expect(jwtService.signAsync).toHaveBeenCalledWith(payload);
Expand Down
14 changes: 11 additions & 3 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ export class AuthService {
private jwtService: JwtService,
) {}

async signIn({ email, password }: SignInDto): Promise<SignInResponseDto> {
const user = await this.usersRepository.findOneBy({ email });
async signIn({
email,
password,
role,
}: SignInDto): Promise<SignInResponseDto> {
const user = await this.usersRepository.findOneBy({ email, role });
if (!user || !(await bcrypt.compare(password, user.password))) {
throw new UnauthorizedException('E-mail ou senha inválidos.');
}
Expand All @@ -41,7 +45,11 @@ export class AuthService {
password: await bcrypt.hash(dto.password, await bcrypt.genSalt(10)),
});
await this.usersRepository.save(user);
return this.signIn({ email: dto.email, password: dto.password });
return this.signIn({
email: dto.email,
password: dto.password,
role: user.role,
});
}

async getProfile(data: { sub: string; email: string }): Promise<User> {
Expand Down
4 changes: 4 additions & 0 deletions src/auth/dtos/signIn.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IsEmail, IsNotEmpty } from 'class-validator';
import { UserRoles } from '../../database/entities/user.entity';

export class SignInDto {
@IsNotEmpty()
Expand All @@ -7,4 +8,7 @@ export class SignInDto {

@IsNotEmpty()
password: string;

@IsNotEmpty()
role: UserRoles;
}

0 comments on commit 2c50c3b

Please sign in to comment.