add endpoints to check if a token is valid

This commit is contained in:
Sven Czarnian
2022-11-04 22:57:30 +01:00
parent 822b39f4fd
commit cb9aff155d
2 changed files with 28 additions and 3 deletions

View File

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

View File

@@ -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;
}