From 700996ac579f87db2a8815e929e76d69128286db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Peveri?= Date: Tue, 23 Jan 2024 19:25:20 +0100 Subject: [PATCH] Add nest config service #89 --- package-lock.json | 55 +++++++++++++++++++ package.json | 1 + src/app.module.ts | 3 +- .../oauth/googleSocialAuthenticator.ts | 9 ++- .../api/guards/nestJwtAuthGuard.ts | 5 +- 5 files changed, 67 insertions(+), 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5102600b..c0d74bb1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,7 @@ "license": "ISC", "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", @@ -2043,6 +2044,29 @@ } } }, + "node_modules/@nestjs/config": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/@nestjs/config/-/config-3.1.1.tgz", + "integrity": "sha512-qu5QlNiJdqQtOsnB6lx4JCXPQ96jkKUsOGd+JXfXwqJqZcOSAq6heNFg0opW4pq4J/VZoNwoo87TNnx9wthnqQ==", + "dependencies": { + "dotenv": "16.3.1", + "dotenv-expand": "10.0.0", + "lodash": "4.17.21", + "uuid": "9.0.0" + }, + "peerDependencies": { + "@nestjs/common": "^8.0.0 || ^9.0.0 || ^10.0.0", + "reflect-metadata": "^0.1.13" + } + }, + "node_modules/@nestjs/config/node_modules/uuid": { + "version": "9.0.0", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-9.0.0.tgz", + "integrity": "sha512-MXcSTerfPa4uqyzStbRoTgt5XIe3x5+42+q1sDuy3R5MDk66URdLMOZe5aPX/SQd+kuYAh0FdP/pO28IkQyTeg==", + "bin": { + "uuid": "dist/bin/uuid" + } + }, "node_modules/@nestjs/core": { "version": "10.2.9", "resolved": "https://registry.npmjs.org/@nestjs/core/-/core-10.2.9.tgz", @@ -4964,6 +4988,14 @@ "url": "https://github.com/motdotla/dotenv?sponsor=1" } }, + "node_modules/dotenv-expand": { + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/dotenv-expand/-/dotenv-expand-10.0.0.tgz", + "integrity": "sha512-GopVGCpVS1UKH75VKHGuQFqS1Gusej0z4FyQkPdwjil2gNIv+LNsqBlboOzpJFZKVT95GkCyWJbBSdFEFUWI2A==", + "engines": { + "node": ">=12" + } + }, "node_modules/eastasianwidth": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/eastasianwidth/-/eastasianwidth-0.2.0.tgz", @@ -12832,6 +12864,24 @@ "uid": "2.0.2" } }, + "@nestjs/config": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/@nestjs/config/-/config-3.1.1.tgz", + "integrity": "sha512-qu5QlNiJdqQtOsnB6lx4JCXPQ96jkKUsOGd+JXfXwqJqZcOSAq6heNFg0opW4pq4J/VZoNwoo87TNnx9wthnqQ==", + "requires": { + "dotenv": "16.3.1", + "dotenv-expand": "10.0.0", + "lodash": "4.17.21", + "uuid": "9.0.0" + }, + "dependencies": { + "uuid": { + "version": "9.0.0", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-9.0.0.tgz", + "integrity": "sha512-MXcSTerfPa4uqyzStbRoTgt5XIe3x5+42+q1sDuy3R5MDk66URdLMOZe5aPX/SQd+kuYAh0FdP/pO28IkQyTeg==" + } + } + }, "@nestjs/core": { "version": "10.2.9", "resolved": "https://registry.npmjs.org/@nestjs/core/-/core-10.2.9.tgz", @@ -14986,6 +15036,11 @@ "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.3.1.tgz", "integrity": "sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==" }, + "dotenv-expand": { + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/dotenv-expand/-/dotenv-expand-10.0.0.tgz", + "integrity": "sha512-GopVGCpVS1UKH75VKHGuQFqS1Gusej0z4FyQkPdwjil2gNIv+LNsqBlboOzpJFZKVT95GkCyWJbBSdFEFUWI2A==" + }, "eastasianwidth": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/eastasianwidth/-/eastasianwidth-0.2.0.tgz", diff --git a/package.json b/package.json index 2a9ed294..ca5d3318 100755 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/app.module.ts b/src/app.module.ts index cd8a602e..a9b9bd35 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -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 {} diff --git a/src/languages/infrastructure/oauth/googleSocialAuthenticator.ts b/src/languages/infrastructure/oauth/googleSocialAuthenticator.ts index cba02e40..e9855ffa 100644 --- a/src/languages/infrastructure/oauth/googleSocialAuthenticator.ts +++ b/src/languages/infrastructure/oauth/googleSocialAuthenticator.ts @@ -1,13 +1,16 @@ 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('GOOGLE_CLIENT_ID'); + this.client = new OAuth2Client(this.clientId); } async login(token: string): Promise { @@ -15,7 +18,7 @@ export default class GoogleSocialAuthenticator implements SocialAuthenticator { try { await this.client.verifyIdToken({ idToken: token, - audience: process.env.GOOGLE_CLIENT_ID, + audience: this.clientId, }); isValid = true; diff --git a/src/shared/infrastructure/api/guards/nestJwtAuthGuard.ts b/src/shared/infrastructure/api/guards/nestJwtAuthGuard.ts index 8c0ed7ac..1280c79f 100644 --- a/src/shared/infrastructure/api/guards/nestJwtAuthGuard.ts +++ b/src/shared/infrastructure/api/guards/nestJwtAuthGuard.ts @@ -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 { const request = context.switchToHttp().getRequest(); @@ -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('JWT_SECRET'), }); } catch { throw new UnauthorizedException();