Skip to content

Commit

Permalink
Add nest config service #89
Browse files Browse the repository at this point in the history
  • Loading branch information
mapeveri committed Jan 23, 2024
1 parent e31582c commit 700996a
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 6 deletions.
55 changes: 55 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
},
"dependencies": {
"@nestjs/common": "^10.2.9",
"@nestjs/config": "^3.1.1",
"@nestjs/core": "^10.2.9",
"@nestjs/cqrs": "^10.2.6",
"@nestjs/jwt": "^10.2.0",
Expand Down
3 changes: 2 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Module } from '@nestjs/common';
import { LanguageModule } from './languages/language.module';
import { SharedModule } from './shared/shared.module';
import { ConfigModule } from '@nestjs/config';

@Module({
imports: [SharedModule, LanguageModule],
imports: [ConfigModule.forRoot({ isGlobal: true }), SharedModule, LanguageModule],
})
export class AppModule {}
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import { Injectable } from '@nestjs/common';
import { SocialAuthenticator } from '@src/languages/domain/auth/socialAuthenticator';
import { OAuth2Client } from 'google-auth-library';
import { ConfigService } from '@nestjs/config';

@Injectable()
export default class GoogleSocialAuthenticator implements SocialAuthenticator {
private client: OAuth2Client;
private readonly clientId: string | undefined;

constructor() {
this.client = new OAuth2Client(process.env.GOOGLE_CLIENT_ID);
constructor(private configService: ConfigService) {
this.clientId = this.configService.get<string>('GOOGLE_CLIENT_ID');
this.client = new OAuth2Client(this.clientId);
}

async login(token: string): Promise<boolean> {
let isValid;
try {
await this.client.verifyIdToken({
idToken: token,
audience: process.env.GOOGLE_CLIENT_ID,
audience: this.clientId,
});

isValid = true;
Expand Down
5 changes: 3 additions & 2 deletions src/shared/infrastructure/api/guards/nestJwtAuthGuard.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { CanActivate, ExecutionContext, Injectable, UnauthorizedException } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { Request } from 'express';
import { ConfigService } from '@nestjs/config';

@Injectable()
export class NestJwtAuthGuard implements CanActivate {
constructor(private jwtService: JwtService) {}
constructor(private readonly jwtService: JwtService, private readonly configService: ConfigService) {}

async canActivate(context: ExecutionContext): Promise<boolean> {
const request = context.switchToHttp().getRequest();
Expand All @@ -15,7 +16,7 @@ export class NestJwtAuthGuard implements CanActivate {

try {
request['user'] = await this.jwtService.verifyAsync(token, {
secret: process.env.JWT_SECRET,
secret: this.configService.get<string>('JWT_SECRET'),
});
} catch {
throw new UnauthorizedException();
Expand Down

0 comments on commit 700996a

Please sign in to comment.