Browse Source

add endpoints to check if a token is valid

Sven Czarnian 1 year ago
parent
commit
cb9aff155d
2 changed files with 28 additions and 3 deletions
  1. 17 3
      src/auth/auth.controller.ts
  2. 11 0
      src/auth/dto/radartoken.dto.ts

+ 17 - 3
src/auth/auth.controller.ts

@@ -16,6 +16,7 @@ import { ConfigService } from '@nestjs/config';
 import { AuthService } from './auth.service';
 import { JwtGuard } from './guards/jwt.guard';
 import { RadarScopeDto } from './dto/radarscope.dto';
+import { RadarTokenDto } from './dto/radartoken.dto';
 import { UserDto } from './dto/user.dto';
 import { Request } from 'express';
 
@@ -68,7 +69,7 @@ export class AuthController {
   @ApiResponse({
     status: 200,
     description: 'The created Bearer token to use endpoints',
-    type: String,
+    type: RadarTokenDto,
   })
   @ApiResponse({
     status: 404,
@@ -76,7 +77,7 @@ export class AuthController {
   })
   async radarScope(
     @Body('scopeData') scopeData: RadarScopeDto,
-  ): Promise<string> {
+  ): Promise<RadarTokenDto> {
     return this.authService
       .loginRadarScope(scopeData.vatsimId, scopeData.key)
       .then((token) => {
@@ -86,10 +87,23 @@ export class AuthController {
             HttpStatus.NOT_FOUND,
           );
         }
-        return token;
+        return { token };
       });
   }
 
+  @UseGuards(JwtGuard)
+  @Get('/validate')
+  @ApiResponse({
+    status: 200,
+    description: 'The created Bearer token to use endpoints',
+  })
+  @ApiResponse({
+    status: 401,
+    description: 'The sent Bearer token is invalid',
+  })
+  // eslint-disable-next-line @typescript-eslint/no-empty-function
+  validate(): void {}
+
   @UseGuards(JwtGuard)
   @Get('/user')
   async user(@Req() request: Request): Promise<UserDto> {

+ 11 - 0
src/auth/dto/radartoken.dto.ts

@@ -0,0 +1,11 @@
+import { IsNotEmpty } from 'class-validator';
+import { ApiProperty } from '@nestjs/swagger';
+
+export class RadarTokenDto {
+  @IsNotEmpty()
+  @ApiProperty({
+    description: 'The usable Bearer token',
+    example: 'SECRET',
+  })
+  token: string;
+}