add an endpoint to receive the user data

This commit is contained in:
Sven Czarnian
2022-11-03 02:00:33 +01:00
parent dfeaaf3e0a
commit 572a571acf

View File

@@ -1,4 +1,3 @@
import { HttpService } from '@nestjs/axios';
import { import {
Controller, Controller,
Get, Get,
@@ -6,12 +5,15 @@ import {
HttpStatus, HttpStatus,
Query, Query,
Redirect, Redirect,
Req,
UseGuards,
} from '@nestjs/common'; } from '@nestjs/common';
import { ApiQuery } from '@nestjs/swagger'; import { ApiQuery } from '@nestjs/swagger';
import { ConfigService } from '@nestjs/config'; import { ConfigService } from '@nestjs/config';
import { JwtService } from '@nestjs/jwt';
import { catchError, lastValueFrom, map } from 'rxjs';
import { AuthService } from './auth.service'; import { AuthService } from './auth.service';
import { JwtGuard } from './guards/jwt.guard';
import { UserDto } from './dto/user.dto';
import { Request } from 'express';
@Controller('auth') @Controller('auth')
export class AuthController { export class AuthController {
@@ -53,4 +55,22 @@ export class AuthController {
}; };
} }
} }
@UseGuards(JwtGuard)
@Get('/user')
async user(@Req() request: Request): Promise<UserDto> {
const token = request.headers.authorization.replace('Bearer ', '');
return this.authService.user(token).then((user) => {
if (!user) {
throw new HttpException('Invalid user request', HttpStatus.NOT_FOUND);
}
return {
vatsimId: user.vatsimId,
fullName: user.fullName,
administrator: user.administrator,
airportConfigurationAccess: user.airportConfigurationAccess,
};
});
}
} }