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 75390ee
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ 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 +41,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 75390ee

Please sign in to comment.