chore(repo): removed LocalAuthStrategy

This commit is contained in:
Francesco Spilla 2025-02-12 17:14:23 +01:00
parent b180968def
commit 0148082d0a
5 changed files with 7 additions and 36 deletions

View File

@ -1,11 +1,10 @@
import { Body, Controller, HttpCode, HttpStatus, Post, UseGuards } from '@nestjs/common';
import { Body, Controller, HttpCode, HttpStatus, Post, UnauthorizedException, UseGuards } from '@nestjs/common';
import { ApiBearerAuth } from '@nestjs/swagger';
import { AccountsEntity } from '../database/entities';
import { LoginDto, LoginResDto } from './auth.dto';
import { AuthService } from './auth.service';
import { AuthenticatedUser } from './authenticated-user.decorator';
import { Public } from './strategies/jwt/jwt-auth.guard';
import { LocalAuthGuard } from './strategies/local/local-auth.guard';
@ApiBearerAuth()
@Controller('auth')
@ -13,10 +12,12 @@ export class AuthController {
constructor(private readonly authService: AuthService) {}
@HttpCode(HttpStatus.OK)
@UseGuards(LocalAuthGuard)
@Public()
@Post('login')
async logIn(@AuthenticatedUser() user: AccountsEntity, @Body() body: LoginDto): Promise<LoginResDto> {
async logIn(@Body() body: LoginDto): Promise<LoginResDto> {
const { username, password } = body;
const user = await this.authService.getAuthenticatedUser(username, password);
const { accessToken, refreshToken } = await this.authService.signJwts(user);
return {
accessToken: accessToken,

View File

@ -10,7 +10,6 @@ import { JwtAccessTokenModule } from './strategies/jwt/jwt-access-token.module';
import { JwtAuthGuard } from './strategies/jwt/jwt-auth.guard';
import { JwtRefreshTokenAuthStrategy } from './strategies/jwt/jwt-refresh-token-auth.strategy';
import { JwtRefreshTokenModule } from './strategies/jwt/jwt-refresh-token.module';
import { LocalAuthStrategy } from './strategies/local/local-auth.strategy';
import { UsersAuthModule } from './users/users.module';
@Module({
@ -29,7 +28,6 @@ import { UsersAuthModule } from './users/users.module';
inject: [Reflector],
},
AuthService,
LocalAuthStrategy,
JwtAccessTokenAuthStrategy,
JwtRefreshTokenAuthStrategy,
],

View File

@ -35,8 +35,7 @@ export class AuthService {
await this.verifyPassword(password, account.password);
return account;
} catch (error) {
this.logger.error(error);
throw new UnauthorizedException(`Unknown error`);
throw new UnauthorizedException(`Wrong credentials`);
}
}
@ -44,7 +43,7 @@ export class AuthService {
const isPasswordMatching =
hashedPassword && !isEmpty(hashedPassword) ? await bcrypt.compare(plainTextPassword, hashedPassword) : null;
if (!isPasswordMatching) {
throw new UnauthorizedException(`Wrong credentials`);
throw new UnauthorizedException(`Wrong password`);
}
}
}

View File

@ -1,5 +0,0 @@
import { Injectable } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
@Injectable()
export class LocalAuthGuard extends AuthGuard('local') {}

View File

@ -1,22 +0,0 @@
import { Injectable } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { Strategy } from 'passport-local';
import { AuthService } from '../../auth.service';
import { RequestWithUser } from '../../constants/request-with-user';
import { AccountsEntity } from '../../../database/entities';
@Injectable()
export class LocalAuthStrategy extends PassportStrategy(Strategy, 'local') {
constructor(private readonly authService: AuthService) {
super({
usernameField: 'username',
passwordField: 'password',
passReqToCallback: true,
});
}
async validate(request: RequestWithUser, username: string, password: string): Promise<AccountsEntity> {
const account = await this.authService.getAuthenticatedUser(username, password);
return account;
}
}