diff --git a/apps/ebitemp-api/src/app/modules/auth/auth.controller.ts b/apps/ebitemp-api/src/app/modules/auth/auth.controller.ts index b40558a..1746bee 100644 --- a/apps/ebitemp-api/src/app/modules/auth/auth.controller.ts +++ b/apps/ebitemp-api/src/app/modules/auth/auth.controller.ts @@ -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 { + async logIn(@Body() body: LoginDto): Promise { + const { username, password } = body; + const user = await this.authService.getAuthenticatedUser(username, password); + const { accessToken, refreshToken } = await this.authService.signJwts(user); return { accessToken: accessToken, diff --git a/apps/ebitemp-api/src/app/modules/auth/auth.module.ts b/apps/ebitemp-api/src/app/modules/auth/auth.module.ts index 3ae1354..ce409cd 100644 --- a/apps/ebitemp-api/src/app/modules/auth/auth.module.ts +++ b/apps/ebitemp-api/src/app/modules/auth/auth.module.ts @@ -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, ], diff --git a/apps/ebitemp-api/src/app/modules/auth/auth.service.ts b/apps/ebitemp-api/src/app/modules/auth/auth.service.ts index 9c4bcf5..962606a 100644 --- a/apps/ebitemp-api/src/app/modules/auth/auth.service.ts +++ b/apps/ebitemp-api/src/app/modules/auth/auth.service.ts @@ -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`); } } } diff --git a/apps/ebitemp-api/src/app/modules/auth/strategies/local/local-auth.guard.ts b/apps/ebitemp-api/src/app/modules/auth/strategies/local/local-auth.guard.ts deleted file mode 100644 index ccf962b..0000000 --- a/apps/ebitemp-api/src/app/modules/auth/strategies/local/local-auth.guard.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { Injectable } from '@nestjs/common'; -import { AuthGuard } from '@nestjs/passport'; - -@Injectable() -export class LocalAuthGuard extends AuthGuard('local') {} diff --git a/apps/ebitemp-api/src/app/modules/auth/strategies/local/local-auth.strategy.ts b/apps/ebitemp-api/src/app/modules/auth/strategies/local/local-auth.strategy.ts deleted file mode 100644 index 05cfb5a..0000000 --- a/apps/ebitemp-api/src/app/modules/auth/strategies/local/local-auth.strategy.ts +++ /dev/null @@ -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 { - const account = await this.authService.getAuthenticatedUser(username, password); - return account; - } -}