Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/account activation #18

Merged
merged 3 commits into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
);
Loading