chore(repo): removed LocalAuthStrategy
This commit is contained in:
parent
b180968def
commit
0148082d0a
@ -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 { ApiBearerAuth } from '@nestjs/swagger';
|
||||||
import { AccountsEntity } from '../database/entities';
|
import { AccountsEntity } from '../database/entities';
|
||||||
import { LoginDto, LoginResDto } from './auth.dto';
|
import { LoginDto, LoginResDto } from './auth.dto';
|
||||||
import { AuthService } from './auth.service';
|
import { AuthService } from './auth.service';
|
||||||
import { AuthenticatedUser } from './authenticated-user.decorator';
|
import { AuthenticatedUser } from './authenticated-user.decorator';
|
||||||
import { Public } from './strategies/jwt/jwt-auth.guard';
|
import { Public } from './strategies/jwt/jwt-auth.guard';
|
||||||
import { LocalAuthGuard } from './strategies/local/local-auth.guard';
|
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@Controller('auth')
|
@Controller('auth')
|
||||||
@ -13,10 +12,12 @@ export class AuthController {
|
|||||||
constructor(private readonly authService: AuthService) {}
|
constructor(private readonly authService: AuthService) {}
|
||||||
|
|
||||||
@HttpCode(HttpStatus.OK)
|
@HttpCode(HttpStatus.OK)
|
||||||
@UseGuards(LocalAuthGuard)
|
|
||||||
@Public()
|
@Public()
|
||||||
@Post('login')
|
@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);
|
const { accessToken, refreshToken } = await this.authService.signJwts(user);
|
||||||
return {
|
return {
|
||||||
accessToken: accessToken,
|
accessToken: accessToken,
|
||||||
|
@ -10,7 +10,6 @@ import { JwtAccessTokenModule } from './strategies/jwt/jwt-access-token.module';
|
|||||||
import { JwtAuthGuard } from './strategies/jwt/jwt-auth.guard';
|
import { JwtAuthGuard } from './strategies/jwt/jwt-auth.guard';
|
||||||
import { JwtRefreshTokenAuthStrategy } from './strategies/jwt/jwt-refresh-token-auth.strategy';
|
import { JwtRefreshTokenAuthStrategy } from './strategies/jwt/jwt-refresh-token-auth.strategy';
|
||||||
import { JwtRefreshTokenModule } from './strategies/jwt/jwt-refresh-token.module';
|
import { JwtRefreshTokenModule } from './strategies/jwt/jwt-refresh-token.module';
|
||||||
import { LocalAuthStrategy } from './strategies/local/local-auth.strategy';
|
|
||||||
import { UsersAuthModule } from './users/users.module';
|
import { UsersAuthModule } from './users/users.module';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
@ -29,7 +28,6 @@ import { UsersAuthModule } from './users/users.module';
|
|||||||
inject: [Reflector],
|
inject: [Reflector],
|
||||||
},
|
},
|
||||||
AuthService,
|
AuthService,
|
||||||
LocalAuthStrategy,
|
|
||||||
JwtAccessTokenAuthStrategy,
|
JwtAccessTokenAuthStrategy,
|
||||||
JwtRefreshTokenAuthStrategy,
|
JwtRefreshTokenAuthStrategy,
|
||||||
],
|
],
|
||||||
|
@ -35,8 +35,7 @@ export class AuthService {
|
|||||||
await this.verifyPassword(password, account.password);
|
await this.verifyPassword(password, account.password);
|
||||||
return account;
|
return account;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.error(error);
|
throw new UnauthorizedException(`Wrong credentials`);
|
||||||
throw new UnauthorizedException(`Unknown error`);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,7 +43,7 @@ export class AuthService {
|
|||||||
const isPasswordMatching =
|
const isPasswordMatching =
|
||||||
hashedPassword && !isEmpty(hashedPassword) ? await bcrypt.compare(plainTextPassword, hashedPassword) : null;
|
hashedPassword && !isEmpty(hashedPassword) ? await bcrypt.compare(plainTextPassword, hashedPassword) : null;
|
||||||
if (!isPasswordMatching) {
|
if (!isPasswordMatching) {
|
||||||
throw new UnauthorizedException(`Wrong credentials`);
|
throw new UnauthorizedException(`Wrong password`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +0,0 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
|
||||||
import { AuthGuard } from '@nestjs/passport';
|
|
||||||
|
|
||||||
@Injectable()
|
|
||||||
export class LocalAuthGuard extends AuthGuard('local') {}
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
Reference in New Issue
Block a user