Skip to content

Commit d61bf73

Browse files
authored
Revert "Centralise env variables handling"
1 parent dfbf3b2 commit d61bf73

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+142
-267
lines changed

backend/auth-service/src/app.controller.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ describe('AppController', () => {
1313

1414
appController = app.get<AppController>(AppController);
1515
});
16+
1617
});

backend/auth-service/src/app.module.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ import { AppController } from './app.controller';
33
import { AppService } from './app.service';
44
import { PassportModule } from '@nestjs/passport';
55
import { JwtModule } from '@nestjs/jwt';
6-
import { ClientsModule } from '@nestjs/microservices';
6+
import { ClientsModule, Transport } from '@nestjs/microservices';
77
import { HttpModule } from '@nestjs/axios';
8-
import { config } from './configs';
98
import {
109
AccessTokenStrategy,
1110
RefreshTokenStrategy,
@@ -15,18 +14,16 @@ import {
1514

1615
@Module({
1716
imports: [
18-
PassportModule.register({
19-
defaultStrategy: config.strategies.accessTokenStrategy
20-
}),
17+
PassportModule.register({ defaultStrategy: 'jwt' }),
2118
HttpModule,
2219
JwtModule.register({}),
2320
ClientsModule.register([
2421
{
2522
name: 'USER_SERVICE',
26-
transport: config.userService.transport,
23+
transport: Transport.TCP,
2724
options: {
28-
host: config.userService.host,
29-
port: config.userService.port,
25+
host: 'user-service',
26+
port: 3001,
3027
},
3128
},
3229
]),

backend/auth-service/src/app.service.ts

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import axios, { AxiosResponse } from 'axios';
1717
import { Token, TokenPayload } from './interfaces';
1818
import { AccountProvider } from './constants/account-provider.enum';
1919
import * as nodemailer from 'nodemailer';
20-
import { config } from 'src/configs';
2120

2221
const SALT_ROUNDS = 10;
2322

@@ -31,9 +30,9 @@ export class AppService {
3130
@Inject('USER_SERVICE') private readonly userClient: ClientProxy,
3231
) {
3332
this.oauthClient = new OAuth2Client({
34-
clientId: config.auth.google.clientId,
35-
clientSecret: config.auth.google.clientSecret,
36-
redirectUri: config.auth.google.callbackUrl,
33+
clientId: process.env.GOOGLE_CLIENT_ID,
34+
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
35+
redirectUri: process.env.GOOGLE_CALLBACK_URL,
3736
});
3837
}
3938

@@ -174,7 +173,7 @@ export class AppService {
174173
const resetToken = this.jwtService.sign(
175174
{ userId: user._id.toString(), email: dto.email, type: 'reset-password' },
176175
{
177-
secret: config.auth.local.jwtSecret,
176+
secret: process.env.JWT_SECRET,
178177
expiresIn: '1hr',
179178
},
180179
);
@@ -207,7 +206,7 @@ export class AppService {
207206
public async validatePasswordResetToken(token: string): Promise<any> {
208207
try {
209208
const decoded = this.jwtService.verify(token, {
210-
secret: config.auth.local.jwtSecret,
209+
secret: process.env.JWT_SECRET,
211210
});
212211
const { userId, email, type } = decoded;
213212
if (type !== 'reset-password') {
@@ -226,16 +225,16 @@ export class AppService {
226225
}
227226

228227
private async sendResetEmail(email: string, token: string) {
229-
const resetUrl = `${config.frontendUrl}/reset-password?token=${token}`; // To change next time
228+
const resetUrl = `${process.env.FRONTEND_URL}/reset-password?token=${token}`; // To change next time
230229

231230
const transporter = nodemailer.createTransport({
232231
service: 'gmail',
233232
host: 'smtp.gmail.com',
234233
port: 465,
235234
secure: true,
236235
auth: {
237-
user: config.mailer.user,
238-
pass: config.mailer.password,
236+
user: process.env.NODEMAILER_GMAIL_USER,
237+
pass: process.env.NODEMAILER_GMAIL_PASSWORD,
239238
},
240239
});
241240

@@ -256,7 +255,7 @@ export class AppService {
256255
public async validateAccessToken(accessToken: string): Promise<any> {
257256
try {
258257
const decoded = this.jwtService.verify(accessToken, {
259-
secret: config.auth.local.jwtSecret,
258+
secret: process.env.JWT_SECRET,
260259
});
261260
return decoded;
262261
} catch (error) {
@@ -267,7 +266,7 @@ export class AppService {
267266
public async validateRefreshToken(refreshToken: string): Promise<any> {
268267
try {
269268
const decoded = this.jwtService.verify(refreshToken, {
270-
secret: config.auth.local.jwtRefreshSecret,
269+
secret: process.env.JWT_REFRESH_SECRET,
271270
});
272271
return decoded;
273272
} catch (error) {
@@ -301,7 +300,7 @@ export class AppService {
301300
...rest,
302301
},
303302
{
304-
secret: config.auth.local.jwtSecret,
303+
secret: process.env.JWT_SECRET,
305304
expiresIn: '1h', // 1 hour
306305
},
307306
),
@@ -311,7 +310,7 @@ export class AppService {
311310
...rest,
312311
},
313312
{
314-
secret: config.auth.local.jwtRefreshSecret,
313+
secret: process.env.JWT_REFRESH_SECRET,
315314
expiresIn: '7d', // 1 week
316315
},
317316
),
@@ -324,8 +323,8 @@ export class AppService {
324323
}
325324

326325
getGoogleOAuthUrl(): string {
327-
const clientId = config.auth.google.clientId;
328-
const redirectUri = config.auth.google.callbackUrl;
326+
const clientId = process.env.GOOGLE_CLIENT_ID;
327+
const redirectUri = process.env.GOOGLE_CALLBACK_URL;
329328
const scope = encodeURIComponent('email profile');
330329
const responseType = 'code';
331330
const state = 'secureRandomState';
@@ -405,7 +404,7 @@ export class AppService {
405404

406405
const ticket = await this.oauthClient.verifyIdToken({
407406
idToken: tokens.id_token,
408-
audience: config.auth.google.clientId,
407+
audience: process.env.GOOGLE_CLIENT_ID,
409408
});
410409

411410
const payload = ticket.getPayload();
@@ -429,8 +428,8 @@ export class AppService {
429428
}
430429

431430
getGithubOAuthUrl(): string {
432-
const clientId = config.auth.github.clientId;
433-
const redirectUri = config.auth.github.callbackUrl;
431+
const clientId = process.env.GITHUB_CLIENT_ID;
432+
const redirectUri = process.env.GITHUB_CALLBACK_URL;
434433
const scope = 'user:email';
435434

436435
const githubLoginUrl = `https://github.com/login/oauth/authorize?client_id=${clientId}&redirect_uri=${encodeURIComponent(
@@ -487,10 +486,10 @@ export class AppService {
487486
private async exchangeGithubCodeForTokens(code: string) {
488487
try {
489488
const params = {
490-
client_id: config.auth.github.clientId,
491-
client_secret: config.auth.github.clientSecret,
489+
client_id: process.env.GITHUB_CLIENT_ID,
490+
client_secret: process.env.GITHUB_CLIENT_SECRET,
492491
code: code,
493-
redirect_uri: config.auth.github.callbackUrl,
492+
redirect_uri: process.env.GITHUB_CALLBACK_URL,
494493
};
495494
const headers = {
496495
Accept: 'application/json',

backend/auth-service/src/configs/env.config.ts

Lines changed: 0 additions & 41 deletions
This file was deleted.

backend/auth-service/src/configs/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

backend/auth-service/src/main.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,20 @@ import { NestFactory } from '@nestjs/core';
22
import { AppModule } from './app.module';
33
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
44
import { ValidationPipe } from '@nestjs/common';
5-
import { config } from './configs';
65

76
async function bootstrap() {
87
const app = await NestFactory.createMicroservice<MicroserviceOptions>(
98
AppModule,
109
{
11-
transport: config.authService.transport,
10+
transport: Transport.TCP,
1211
options: {
13-
host: config.authService.host,
14-
port: config.authService.port,
12+
host: '0.0.0.0',
13+
port: 3003,
1514
},
1615
},
1716
);
1817
app.useGlobalPipes(new ValidationPipe());
1918
await app.listen();
20-
console.log('Auth Service is listening on port', config.authService.port);
19+
console.log('Auth Service is listening on port 3003');
2120
}
2221
bootstrap();

backend/auth-service/src/strategies/access-token.strategy.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import { Injectable } from '@nestjs/common';
22
import { PassportStrategy } from '@nestjs/passport';
33
import { Strategy, ExtractJwt } from 'passport-jwt';
4-
import { config } from 'src/configs';
54

65
@Injectable()
7-
export class AccessTokenStrategy extends PassportStrategy(Strategy, config.strategies.accessTokenStrategy) {
6+
export class AccessTokenStrategy extends PassportStrategy(Strategy, 'jwt') {
87
constructor() {
98
super({
109
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
1110
ignoreExpiration: false,
12-
secretOrKey: config.auth.local.jwtSecret,
11+
secretOrKey: process.env.JWT_SECRET,
1312
});
1413
}
1514

backend/auth-service/src/strategies/github.strategy.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
import { PassportStrategy } from '@nestjs/passport';
22
import { Strategy, VerifyCallback } from 'passport-google-oauth20';
33
import { Injectable } from '@nestjs/common';
4-
import { config } from 'src/configs';
54

65
@Injectable()
7-
export class GithubStrategy extends PassportStrategy(
8-
Strategy,
9-
config.strategies.githubStrategy
10-
) {
6+
export class GithubStrategy extends PassportStrategy(Strategy, 'github') {
117
constructor() {
128
super({
13-
clientID: config.auth.github.clientId,
14-
clientSecret: config.auth.github.clientSecret,
15-
callbackURL: config.auth.github.callbackUrl,
9+
clientID: process.env.GITHUB_CLIENT_ID,
10+
clientSecret: process.env.GITHUB_CLIENT_SECRET,
11+
callbackURL: process.env.GITHUB_CALLBACK_URL,
1612
scope: ['user:email'],
1713
});
1814
}

backend/auth-service/src/strategies/google.strategy.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
import { PassportStrategy } from '@nestjs/passport';
22
import { Strategy, VerifyCallback } from 'passport-google-oauth20';
33
import { Injectable } from '@nestjs/common';
4-
import { config } from 'src/configs';
54

65
@Injectable()
7-
export class GoogleStrategy extends PassportStrategy(
8-
Strategy,
9-
config.strategies.googleStrategy
10-
) {
6+
export class GoogleStrategy extends PassportStrategy(Strategy, 'google') {
117
constructor() {
128
super({
13-
clientID: config.auth.google.clientId,
14-
clientSecret: config.auth.google.clientSecret,
15-
callbackURL: config.auth.google.callbackUrl,
9+
clientID: process.env.GOOGLE_CLIENT_ID,
10+
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
11+
callbackURL: process.env.GOOGLE_CALLBACK_URL,
1612
scope: ['email', 'profile'],
1713
});
1814
}

backend/auth-service/src/strategies/refresh-token.strategy.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@ import { PassportStrategy } from '@nestjs/passport';
22
import { Strategy, ExtractJwt } from 'passport-jwt';
33
import { Request } from 'express';
44
import { Injectable } from '@nestjs/common';
5-
import { config } from 'src/configs';
65

76
@Injectable()
87
export class RefreshTokenStrategy extends PassportStrategy(
98
Strategy,
10-
config.strategies.refreshTokenStrategy,
9+
'jwt-refresh',
1110
) {
1211
constructor() {
1312
super({
1413
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
1514
ignoreExpiration: false,
16-
secretOrKey: config.auth.local.jwtRefreshSecret,
15+
secretOrKey: process.env.JWT_REFRESH_SECRET,
1716
passReqToCallback: true, // Pass the token back to the callback
1817
});
1918
}

0 commit comments

Comments
 (0)