Skip to content

Commit

Permalink
refactor(service): refactor method to env
Browse files Browse the repository at this point in the history
  • Loading branch information
CleilsonAndrade committed Oct 8, 2023
1 parent a415bfb commit 423a21e
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 10 deletions.
6 changes: 4 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
########################## NestJS Initialize API Project
######################### NestJS Initialize API Project
NODE_ENV="development" || "production"

PORT=3000

############# JWT
JWT_SECRET=""
JWT_TOKEN=""

JWT_BASE_URL_TOKEN="localhost:3000/login"
JWT_TOKEN_USERNAME=""
JWT_TOKEN_PASSWORD=""


########################## MySQL
######################### MySQL
MYSQL_HOST="127.0.0.1"
MYSQL_USER="root"
MYSQL_PASSWORD="root"
Expand Down
13 changes: 10 additions & 3 deletions src/app/infra/health/token.health.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { HealthIndicator } from '@nestjs/terminus';
import axios from 'axios';

@Injectable()
export class TokenHealthIndicator extends HealthIndicator {
constructor(private readonly configService: ConfigService) {
super();
}

async checkTokenServiceHealth(): Promise<any> {
try {
const username = process.env.JWT_TOKEN_USERNAME;
const password = process.env.JWT_TOKEN_PASSWORD;
const response = await axios.post(`localhost:3000/login`, {
const username = this.configService.get('JWT_TOKEN_USERNAME');
const password = this.configService.get('JWT_TOKEN_PASSWORD');
const baseURL = this.configService.get('JWT_BASE_URL_TOKEN');
// URL to get token via axios
const response = await axios.post(baseURL, {
username,
password,
});
Expand Down
11 changes: 8 additions & 3 deletions src/app/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ValidationPipe } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { NestFactory } from '@nestjs/core';
import {
FastifyAdapter,
Expand All @@ -17,6 +18,10 @@ async function bootstrap(): Promise<void> {
{ cors: true },
);

const configService = app.get(ConfigService);
const NODE_ENV = configService.get('NODE_ENV');
const PORT = configService.get('PORT');

let options;

let config;
Expand All @@ -40,7 +45,7 @@ async function bootstrap(): Promise<void> {
.readFileSync(path.resolve(__dirname, 'docs', 'assets', 'swagger-ui.css'))
.toString();

if (process.env.NODE_ENV === 'production') {
if (NODE_ENV === 'production') {
options = {
swaggerOptions: {
supportedSubmitMethods: [],
Expand All @@ -65,7 +70,7 @@ async function bootstrap(): Promise<void> {
`https://raw.githubusercontent.com/nestjs/nest/master/LICENSE`,
)
.build();
} else if (process.env.NODE_ENV === 'development') {
} else if (NODE_ENV === 'development') {
options = {
customfavIcon: faviconPath,
customCss: darkStyle + cssPath,
Expand Down Expand Up @@ -102,7 +107,7 @@ async function bootstrap(): Promise<void> {

SwaggerModule.setup('docs', app, document, options);

await app.listen(process.env.PORT ?? 3000, '0.0.0.0');
await app.listen(PORT ?? 3000, '0.0.0.0');
console.log(`[🤖]: Application is running on: ${await app.getUrl()}`);
}

Expand Down
8 changes: 7 additions & 1 deletion src/app/security/strategy/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import { ConfigService } from '@nestjs/config';

const configService = new ConfigService();

const jwtSecret = configService.get('JWT_SECRET') || 'defaultSecret';

export const jwtConstants = {
secret: process.env.JWT_SECRET ?? 'defaultSecret',
secret: jwtSecret,
};
3 changes: 2 additions & 1 deletion src/app/security/strategy/jwt-auth.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { PassportStrategy } from '@nestjs/passport';
import { ExtractJwt, Strategy } from 'passport-jwt';
import { AuthService } from '../auth.service';
import { type JWTPayload } from '../dto/JWTPayload';
import { jwtConstants } from '../strategy/constants';

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor(private readonly authService: AuthService) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: process.env.JWT_SECRET,
secretOrKey: jwtConstants.secret,
ignoreExpiration: false,
});
}
Expand Down

0 comments on commit 423a21e

Please sign in to comment.