-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.controller.ts
50 lines (48 loc) · 1.58 KB
/
app.controller.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import {
Controller,
ForbiddenException,
Get,
Req,
UnauthorizedException,
UseGuards,
UseInterceptors,
} from '@nestjs/common';
import { EnvModule } from './env/env.module';
import type { Request } from 'express';
import { HttpLoggingInterceptor } from '@spuxx/nest-utils';
import { AuthRole } from './auth/auth.config';
import { ApiException } from '@nanogiants/nestjs-swagger-api-exception-decorator';
import { AuthGuard, getSession, isAuthenticated, Roles } from '@spuxx/nest-auth';
@Controller()
@UseInterceptors(HttpLoggingInterceptor)
export class AppController {
@Get()
getHello(@Req() request: Request): object {
const response = {
message: 'Hello there!',
time: new Date().toLocaleTimeString(),
session: isAuthenticated(request)
? `Logged in as ${getSession(request).preferred_username}`
: 'Not logged in',
routes: {
auth: {
login: `${EnvModule.get('APP_BASE_URL')}/auth/login`,
logout: `${EnvModule.get('APP_BASE_URL')}/auth/logout`,
session: `${EnvModule.get('APP_BASE_URL')}/auth/session`,
},
docs: {
'swagger-ui': `${EnvModule.get('APP_BASE_URL')}/docs`,
'open-api-json': `${EnvModule.get('APP_BASE_URL')}/docs-json`,
},
},
};
return response;
}
@Get('/protected')
@UseGuards(AuthGuard)
@Roles(AuthRole.user)
@ApiException(() => [UnauthorizedException, ForbiddenException])
getProtectedHello(@Req() request: Request) {
return `Oh hello there, ${request.oidc.user.name}! This route is protected, but you can see it! Not bad!`;
}
}