Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
🐛 Fix use authentication in class
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Oct 29, 2020
1 parent e362889 commit a166eaa
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/modules/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,30 @@ import {
Injectable,
NotFoundException,
UnauthorizedException,
UnprocessableEntityException,
UnprocessableEntityException
} from '@nestjs/common';
import { randomStringGenerator } from '@nestjs/common/utils/random-string-generator.util';
import { ConfigService } from '@nestjs/config';
import qrcode from 'qrcode';
import { JwtService } from '@nestjs/jwt';
import { authenticator } from 'otplib';
import type { Authenticator } from '@otplib/core';
import { users } from '@prisma/client';
import { compare, hash } from 'bcrypt';
import { authenticator } from 'otplib';
import qrcode from 'qrcode';
import { safeEmail } from 'src/helpers/safe-email';
import { EmailService } from '../email/email.service';
import { Expose } from '../prisma/prisma.interface';
import { PrismaService } from '../prisma/prisma.service';
import { PwnedService } from '../pwned/pwned.service';
import { TWO_FACTOR_TOKEN } from '../tokens/tokens.constants';
import { TokensService } from '../tokens/tokens.service';
import { RegisterDto } from './auth.dto';
import { AccessTokenClaims } from './auth.interface';
import { TokensService } from '../tokens/tokens.service';
import { TWO_FACTOR_TOKEN } from '../tokens/tokens.constants';

@Injectable()
export class AuthService {
authenticator: Authenticator;

constructor(
private prisma: PrismaService,
private email: EmailService,
Expand All @@ -34,10 +37,10 @@ export class AuthService {
private pwnedService: PwnedService,
private tokensService: TokensService,
) {
authenticator.options.window = [
this.authenticator = authenticator.create({ window: [
this.configService.get<number>('security.totpWindowPast'),
this.configService.get<number>('security.totpWindowFuture'),
];
] });
}

async validateUser(email: string, password?: string): Promise<number> {
Expand Down Expand Up @@ -159,7 +162,7 @@ export class AuthService {
where: { id: userId },
data: { twoFactorSecret: secret },
});
const otpauth = authenticator.keyuri(
const otpauth = this.authenticator.keyuri(
userId.toString(),
this.configService.get<string>('meta.totpServiceName'),
secret,
Expand All @@ -178,7 +181,7 @@ export class AuthService {
throw new BadRequestException(
'Two-factor authentication is already enabled',
);
if (!authenticator.check(code, user.twoFactorSecret))
if (!this.authenticator.check(code, user.twoFactorSecret))
throw new UnauthorizedException(
'Two-factor authentication code is invalid',
);
Expand Down Expand Up @@ -214,7 +217,7 @@ export class AuthService {
if (!user) throw new NotFoundException();
if (!user.twoFactorEnabled)
throw new BadRequestException('Two-factor authentication is not enabled');
if (!authenticator.check(code, user.twoFactorSecret))
if (!this.authenticator.check(code, user.twoFactorSecret))
throw new UnauthorizedException(
'Two-factor authentication code is invalid',
);
Expand Down
2 changes: 2 additions & 0 deletions src/modules/emails/emails.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { AuthService } from '../auth/auth.service';
import { EmailModule } from '../email/email.module';
import { PrismaModule } from '../prisma/prisma.module';
import { PwnedModule } from '../pwned/pwned.module';
import { TokensModule } from '../tokens/tokens.module';
import { UsersService } from '../users/users.service';
import { EmailController } from './emails.controller';
import { EmailsService } from './emails.service';
Expand All @@ -15,6 +16,7 @@ import { EmailsService } from './emails.service';
EmailModule,
ConfigModule,
PwnedModule,
TokensModule,
JwtModule.register({
secret: process.env.JWT_SECRET ?? 'staart',
}),
Expand Down

0 comments on commit a166eaa

Please sign in to comment.