diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts index f43f7db..72f8e06 100644 --- a/src/auth/auth.controller.ts +++ b/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 { + @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( @@ -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( + 'frontend.base-url', + )}/${this.config.get( + 'frontend.login-endpoint', + )}?token=${accessToken}`, + }; + } else { + return { + url: `${this.config.get( + 'frontend.base-url', + )}/${this.config.get('frontend.login-endpoint')}`, + }; + } } }