Skip to content

Commit

Permalink
Merge pull request #19 from Bricks666/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Bricks666 authored Jan 27, 2024
2 parents b643b1e + 6515434 commit ca13068
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 23 deletions.
6 changes: 5 additions & 1 deletion src/auth/controllers/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
LoginRequestDto,
TokensDto
} from '../dto';
import { DisableAuthCheck } from '../lib';
import { DisableAuthCheck, DisableIsActivatedCheck } from '../lib';

@ApiTags('Авторизация')
@Controller('auth')
Expand All @@ -45,6 +45,7 @@ export class AuthController {
type: AuthenticationResultDto,
})
@ApiCookieAuth()
@DisableIsActivatedCheck()
@DisableAuthCheck()
@Get('/')
async authentication(
Expand Down Expand Up @@ -73,6 +74,7 @@ export class AuthController {
type: SecurityUserDto,
description: 'Подтверждение успешности регистрации',
})
@DisableIsActivatedCheck()
@DisableAuthCheck()
@Post('registration')
async registration(@Body() body: CreateUserDto): Promise<SecurityUserDto> {
Expand All @@ -95,6 +97,7 @@ export class AuthController {
@ApiNotFoundResponse({
description: 'Пользователь не найден',
})
@DisableIsActivatedCheck()
@DisableAuthCheck()
@Put('registration/activate')
async activate(@Query('token') token: string): Promise<boolean> {
Expand All @@ -107,6 +110,7 @@ export class AuthController {
type: AuthenticationResultDto,
description: 'Данные пользователя и пара токенов',
})
@DisableIsActivatedCheck()
@DisableAuthCheck()
@Post('login')
async login(
Expand Down
25 changes: 6 additions & 19 deletions src/auth/lib/activated/is-activated.guard.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import {
CanActivate,
ExecutionContext,
Injectable,
InternalServerErrorException
} from '@nestjs/common';
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { Request } from 'express';
import { SecurityUserDto } from '@/users/dto';
import { UsersService } from '@/users/services';
import { DISABLE_AUTH_CHECK_FLAG } from '../auth';
import { DISABLE_IS_ACTIVATED_FLAG } from './config';

@Injectable()
Expand All @@ -19,15 +13,10 @@ export class IsActivatedGuard implements CanActivate {
) {}

async canActivate(context: ExecutionContext): Promise<boolean> {
const disable =
this.reflector.get<boolean | undefined>(
DISABLE_IS_ACTIVATED_FLAG,
context.getHandler()
) ||
this.reflector.get<boolean | undefined>(
DISABLE_AUTH_CHECK_FLAG,
context.getHandler()
);
const disable = this.reflector.getAllAndOverride<boolean | undefined>(
DISABLE_IS_ACTIVATED_FLAG,
[context.getHandler(), context.getClass()]
);

if (disable) {
return true;
Expand All @@ -37,9 +26,7 @@ export class IsActivatedGuard implements CanActivate {
const user = (req as any).user as SecurityUserDto;

if (!user) {
throw new InternalServerErrorException(
'Is activated must be used only with authorized users'
);
return true;
}

return this.usersService.isActivated({ id: user.id, });
Expand Down
6 changes: 5 additions & 1 deletion src/auth/services/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,17 @@ export class AuthService {

const user = await this.usersService.getInsecure({ email, });

if (!user.activated) {
throw new ConflictException('User is not activated');
}

const isValidPassword = await compare(password, user.password);

if (!isValidPassword) {
throw new ForbiddenException('Incorrect password');
}

user.password = undefined;
delete user.password;

const tokens = await this.#generateTokens(user);

Expand Down
2 changes: 0 additions & 2 deletions src/shared/lib/cookie.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ export const Cookie = createParamDecorator<string, ExecutionContext>(
(cookie, context) => {
const req: Request = context.switchToHttp().getRequest();

console.log(req.cookies);

return req.cookies[cookie] ?? null;
}
);

0 comments on commit ca13068

Please sign in to comment.