Skip to content

Commit 2b60646

Browse files
committed
Demo accounts in own REST endpoint
1 parent 1678004 commit 2b60646

4 files changed

Lines changed: 23 additions & 15 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,6 @@
8585
},
8686
"coverageDirectory": "../coverage",
8787
"testEnvironment": "node"
88-
}
88+
},
89+
"packageManager": "yarn@1.22.22+sha1.ac34549e6aa8e7ead463a7407e1c7390f61a6610"
8990
}

src/app.controller.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
import { Controller, Get, Res } from '@nestjs/common';
1+
import { Controller, ForbiddenException, Get, Res } from '@nestjs/common';
22
import { Response } from 'express';
33
import { AppService } from './app.service';
44
import { ApiExcludeEndpoint, ApiOperation } from '@nestjs/swagger';
5+
import { AuthService } from '@/common/auth/auth.service';
56

67
@Controller()
78
export class AppController {
8-
constructor(private readonly appService: AppService) {}
9+
constructor(
10+
private readonly app: AppService,
11+
private readonly auth: AuthService,
12+
) {}
913

1014
@Get('/')
1115
@ApiExcludeEndpoint()
@@ -20,8 +24,17 @@ export class AppController {
2024
version: string;
2125
description: string;
2226
env: string;
23-
accounts?: Array<{ id: string; name?: string; roles?: string[]; token: string }>;
2427
} {
25-
return this.appService.info;
28+
return this.app.info;
29+
}
30+
31+
@ApiOperation({ summary: 'Get demo accounts' })
32+
@Get('/demo-accounts')
33+
getDemoAccounts(): Array<{ id: string; name?: string; roles?: string[]; token: string }> {
34+
if (!this.auth.demoAccounts) {
35+
throw new ForbiddenException('Demo accounts are disabled');
36+
}
37+
38+
return this.auth.demoAccounts;
2639
}
2740
}

src/app.service.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Injectable, OnModuleInit } from '@nestjs/common';
22
import { ConfigService } from './common/config/config.service';
3-
import { AuthService } from './common/auth/auth.service';
43

54
@Injectable()
65
export class AppService implements OnModuleInit {
@@ -9,13 +8,9 @@ export class AppService implements OnModuleInit {
98
version: string;
109
description: string;
1110
env: string;
12-
accounts?: Array<{ id: string; name?: string; roles?: string[]; token: string }>;
1311
};
1412

15-
constructor(
16-
private config: ConfigService,
17-
private auth: AuthService,
18-
) {}
13+
constructor(private config: ConfigService) {}
1914

2015
onModuleInit() {
2116
this.initInfo();
@@ -30,7 +25,6 @@ export class AppService implements OnModuleInit {
3025
version: packageInfo.version,
3126
description: packageInfo.description,
3227
env: this.config.get('env'),
33-
accounts: this.auth.demoAccounts,
3428
};
3529
}
3630
}

src/common/auth/auth.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,19 @@ export class AuthService implements OnModuleInit {
3636
{
3737
id: 'alice',
3838
name: 'Alice',
39-
roles: [],
39+
roles: ['support'],
4040
token: this.jwt.sign({ id: 'alice', name: 'Alice' }),
4141
},
4242
{
4343
id: 'bob',
4444
name: 'Bob',
45-
roles: [],
45+
roles: ['management'],
4646
token: this.jwt.sign({ id: 'bob', name: 'Bob' }),
4747
},
4848
{
4949
id: 'claire',
5050
name: 'Claire',
51-
roles: [],
51+
roles: ['client'],
5252
token: this.jwt.sign({ id: 'claire', name: 'Claire' }),
5353
},
5454
{

0 commit comments

Comments
 (0)