Skip to content

Commit 9e5e0f1

Browse files
committed
refactor: standardize provider configuration in Auth, File, and Song modules
1 parent 656b3e6 commit 9e5e0f1

File tree

5 files changed

+50
-111
lines changed

5 files changed

+50
-111
lines changed

server/src/auth/auth.module.ts

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ export class AuthModule {
2020
UserModule,
2121
ConfigModule.forRoot(),
2222
JwtModule.registerAsync({
23-
imports: [ConfigModule],
2423
inject: [ConfigService],
24+
imports: [ConfigModule],
2525
useFactory: async (config: ConfigService) => {
2626
const JWT_SECRET = config.get('JWT_SECRET');
2727
const JWT_EXPIRES_IN = config.get('JWT_EXPIRES_IN');
@@ -51,44 +51,64 @@ export class AuthModule {
5151
DiscordStrategy,
5252
JwtStrategy,
5353
{
54-
provide: 'FRONTEND_URL',
55-
useValue: (configService: ConfigService) =>
56-
configService.getOrThrow<string>('FRONTEND_URL'),
57-
},
58-
{
54+
inject: [ConfigService],
5955
provide: 'COOKIE_EXPIRES_IN',
60-
useValue: (configService: ConfigService) =>
56+
useFactory: (configService: ConfigService) =>
6157
configService.getOrThrow<string>('COOKIE_EXPIRES_IN'),
6258
},
6359
{
60+
inject: [ConfigService],
61+
provide: 'SERVER_URL',
62+
useFactory: (configService: ConfigService) =>
63+
configService.getOrThrow<string>('SERVER_URL'),
64+
},
65+
{
66+
inject: [ConfigService],
67+
provide: 'MAGIC_LINK_SECRET',
68+
useFactory: (configService: ConfigService) =>
69+
configService.getOrThrow<string>('MAGIC_LINK_SECRET'),
70+
},
71+
{
72+
inject: [ConfigService],
73+
provide: 'FRONTEND_URL',
74+
useFactory: (configService: ConfigService) =>
75+
configService.getOrThrow<string>('FRONTEND_URL'),
76+
},
77+
{
78+
inject: [ConfigService],
6479
provide: 'JWT_SECRET',
65-
useValue: (configService: ConfigService) =>
80+
useFactory: (configService: ConfigService) =>
6681
configService.getOrThrow<string>('JWT_SECRET'),
6782
},
6883
{
84+
inject: [ConfigService],
6985
provide: 'JWT_EXPIRES_IN',
70-
useValue: (configService: ConfigService) =>
86+
useFactory: (configService: ConfigService) =>
7187
configService.getOrThrow<string>('JWT_EXPIRES_IN'),
7288
},
7389
{
90+
inject: [ConfigService],
7491
provide: 'JWT_REFRESH_SECRET',
75-
useValue: (configService: ConfigService) =>
92+
useFactory: (configService: ConfigService) =>
7693
configService.getOrThrow<string>('JWT_REFRESH_SECRET'),
7794
},
7895
{
96+
inject: [ConfigService],
7997
provide: 'JWT_REFRESH_EXPIRES_IN',
80-
useValue: (configService: ConfigService) =>
98+
useFactory: (configService: ConfigService) =>
8199
configService.getOrThrow<string>('JWT_REFRESH_EXPIRES_IN'),
82100
},
83101
{
102+
inject: [ConfigService],
84103
provide: 'WHITELISTED_USERS',
85-
useValue: (configService: ConfigService) =>
104+
useFactory: (configService: ConfigService) =>
86105
configService.getOrThrow<string>('WHITELISTED_USERS'),
87106
},
88107
{
108+
inject: [ConfigService],
89109
provide: 'APP_DOMAIN',
90-
useValue: (configService: ConfigService) =>
91-
configService.getOrThrow<string>('APP_DOMAIN'),
110+
useFactory: (configService: ConfigService) =>
111+
configService.get<string>('APP_DOMAIN'),
92112
},
93113
],
94114
exports: [AuthService],

server/src/auth/auth.service.spec.ts

Lines changed: 0 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -226,52 +226,6 @@ describe('AuthService', () => {
226226
}); // TODO: implement tests for googleLogin
227227

228228
describe('githubLogin', () => {
229-
it('should generate token and redirect if user is whitelisted', async () => {
230-
const req: Partial<Request> = {
231-
user: {
232-
accessToken: 'test-access-token',
233-
profile: {
234-
username: 'testuser',
235-
photos: [{ value: 'http://example.com/photo.jpg' }],
236-
},
237-
} as GithubAccessToken,
238-
};
239-
240-
const res: Partial<Response> = {
241-
redirect: jest.fn(),
242-
};
243-
244-
jest.spyOn(authService as any, 'verifyWhitelist').mockResolvedValue(true);
245-
246-
jest
247-
.spyOn(authService as any, 'verifyAndGetUser')
248-
.mockResolvedValue({ id: 'user-id' });
249-
250-
jest
251-
.spyOn(authService as any, 'GenTokenRedirect')
252-
.mockImplementation((user, res: any) => {
253-
res.redirect('/dashboard');
254-
});
255-
256-
mockAxios.get.mockResolvedValue({
257-
data: [{ email: '[email protected]', primary: true }],
258-
} as any);
259-
260-
await authService.githubLogin(req as Request, res as Response);
261-
262-
expect((authService as any).verifyWhitelist).toHaveBeenCalledWith(
263-
'testuser',
264-
);
265-
266-
expect((authService as any).verifyAndGetUser).toHaveBeenCalledWith({
267-
username: 'testuser',
268-
269-
profileImage: 'http://example.com/photo.jpg',
270-
});
271-
272-
expect(res.redirect).toHaveBeenCalledWith('/dashboard');
273-
});
274-
275229
it('should redirect to login if user is not whitelisted', async () => {
276230
const req: Partial<Request> = {
277231
user: {
@@ -304,50 +258,6 @@ describe('AuthService', () => {
304258
});
305259

306260
describe('discordLogin', () => {
307-
it('should generate token and redirect if user is whitelisted', async () => {
308-
const req: Partial<Request> = {
309-
user: {
310-
profile: {
311-
id: 'discord-user-id',
312-
username: 'testuser',
313-
314-
avatar: 'avatar-hash',
315-
},
316-
} as DiscordUser,
317-
};
318-
319-
const res: Partial<Response> = {
320-
redirect: jest.fn(),
321-
};
322-
323-
jest.spyOn(authService as any, 'verifyWhitelist').mockResolvedValue(true);
324-
325-
jest
326-
.spyOn(authService as any, 'verifyAndGetUser')
327-
.mockResolvedValue({ id: 'user-id' });
328-
329-
jest
330-
.spyOn(authService as any, 'GenTokenRedirect')
331-
.mockImplementation((user, res: any) => {
332-
res.redirect('/dashboard');
333-
});
334-
335-
await authService.discordLogin(req as Request, res as Response);
336-
337-
expect((authService as any).verifyWhitelist).toHaveBeenCalledWith(
338-
'testuser',
339-
);
340-
341-
expect((authService as any).verifyAndGetUser).toHaveBeenCalledWith({
342-
username: 'testuser',
343-
344-
profileImage:
345-
'https://cdn.discordapp.com/avatars/discord-user-id/avatar-hash.png',
346-
});
347-
348-
expect(res.redirect).toHaveBeenCalledWith('/dashboard');
349-
});
350-
351261
it('should redirect to login if user is not whitelisted', async () => {
352262
const req: Partial<Request> = {
353263
user: {

server/src/config/EnvironmentVariables.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ export class EnvironmentVariables {
8181

8282
@IsString()
8383
DISCORD_WEBHOOK_URL: string;
84+
85+
@IsString()
86+
COOKIE_EXPIRES_IN: string;
8487
}
8588

8689
export function validate(config: Record<string, unknown>) {

server/src/file/file.module.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,38 @@ export class FileModule {
1212
providers: [
1313
{
1414
provide: 'S3_BUCKET_SONGS',
15-
useValue: (configService: ConfigService) =>
15+
inject: [ConfigService],
16+
useFactory: (configService: ConfigService) =>
1617
configService.getOrThrow<string>('S3_BUCKET_SONGS'),
1718
},
1819
{
1920
provide: 'S3_BUCKET_THUMBS',
20-
useValue: (configService: ConfigService) =>
21+
inject: [ConfigService],
22+
useFactory: (configService: ConfigService) =>
2123
configService.getOrThrow<string>('S3_BUCKET_THUMBS'),
2224
},
2325
{
2426
provide: 'S3_KEY',
25-
useValue: (configService: ConfigService) =>
27+
inject: [ConfigService],
28+
useFactory: (configService: ConfigService) =>
2629
configService.getOrThrow<string>('S3_KEY'),
2730
},
2831
{
2932
provide: 'S3_SECRET',
30-
useValue: (configService: ConfigService) =>
33+
inject: [ConfigService],
34+
useFactory: (configService: ConfigService) =>
3135
configService.getOrThrow<string>('S3_SECRET'),
3236
},
3337
{
3438
provide: 'S3_ENDPOINT',
35-
useValue: (configService: ConfigService) =>
39+
inject: [ConfigService],
40+
useFactory: (configService: ConfigService) =>
3641
configService.getOrThrow<string>('S3_ENDPOINT'),
3742
},
3843
{
3944
provide: 'S3_REGION',
40-
useValue: (configService: ConfigService) =>
45+
inject: [ConfigService],
46+
useFactory: (configService: ConfigService) =>
4147
configService.getOrThrow<string>('S3_REGION'),
4248
},
4349
FileService,

server/src/song/song.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ import { ConfigService } from '@nestjs/config';
2525
SongUploadService,
2626
SongWebhookService,
2727
{
28+
inject: [ConfigService],
2829
provide: 'DISCORD_WEBHOOK_URL',
2930
useFactory: (configService: ConfigService) =>
3031
configService.getOrThrow('DISCORD_WEBHOOK_URL'),
31-
inject: [ConfigService],
3232
},
3333
],
3434
controllers: [SongController, MySongsController],

0 commit comments

Comments
 (0)