Browse Source

add an endpoint to receive the user data

Sven Czarnian 2 years ago
parent
commit
572a571acf
1 changed files with 23 additions and 3 deletions
  1. 23 3
      src/auth/auth.controller.ts

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

@@ -1,4 +1,3 @@
-import { HttpService } from '@nestjs/axios';
 import {
   Controller,
   Get,
@@ -6,12 +5,15 @@ import {
   HttpStatus,
   Query,
   Redirect,
+  Req,
+  UseGuards,
 } from '@nestjs/common';
 import { ApiQuery } from '@nestjs/swagger';
 import { ConfigService } from '@nestjs/config';
-import { JwtService } from '@nestjs/jwt';
-import { catchError, lastValueFrom, map } from 'rxjs';
 import { AuthService } from './auth.service';
+import { JwtGuard } from './guards/jwt.guard';
+import { UserDto } from './dto/user.dto';
+import { Request } from 'express';
 
 @Controller('auth')
 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,
+      };
+    });
+  }
 }