Browse Source

send the bearer token to the frontend

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

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

@@ -5,9 +5,11 @@ import {
   HttpException,
   HttpStatus,
   Query,
+  Redirect,
 } from '@nestjs/common';
 import { ApiQuery } from '@nestjs/swagger';
 import { ConfigService } from '@nestjs/config';
+import { JwtService } from '@nestjs/jwt';
 import { catchError, lastValueFrom, map } from 'rxjs';
 
 @Controller('auth')
@@ -15,6 +17,7 @@ export class AuthController {
   constructor(
     private config: ConfigService,
     private httpService: HttpService,
+    private jwtService: JwtService,
   ) {}
 
   @Get('/vatsim')
@@ -23,7 +26,8 @@ export class AuthController {
     description: 'The authorization code',
     type: String,
   })
-  async vatsim(@Query('code') code): Promise<void> {
+  @Redirect()
+  async vatsim(@Query('code') code) {
     if (!code) {
       throw new HttpException(
         'Code not found in the query',
@@ -53,7 +57,7 @@ export class AuthController {
         ),
     );
 
-    const user = await lastValueFrom(
+    const userdata = await lastValueFrom(
       this.httpService
         .get(
           `${this.config.get<string>(
@@ -73,6 +77,23 @@ export class AuthController {
           }),
         ),
     );
-    console.log(user);
+
+    if (userdata.oauth.token_valid) {
+      const payload = { username: userdata.cid, sub: token };
+      const accessToken = this.jwtService.sign(payload);
+      return {
+        url: `${this.config.get<string>(
+          'frontend.base-url',
+        )}/${this.config.get<string>(
+          'frontend.login-endpoint',
+        )}?token=${accessToken}`,
+      };
+    } else {
+      return {
+        url: `${this.config.get<string>(
+          'frontend.base-url',
+        )}/${this.config.get<string>('frontend.login-endpoint')}`,
+      };
+    }
   }
 }