send the bearer token to the frontend

This commit is contained in:
Sven Czarnian
2022-11-03 01:11:03 +01:00
parent dbe7e7ef42
commit ce17a29f7c

View File

@@ -5,9 +5,11 @@ import {
HttpException, HttpException,
HttpStatus, HttpStatus,
Query, Query,
Redirect,
} 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 { catchError, lastValueFrom, map } from 'rxjs';
@Controller('auth') @Controller('auth')
@@ -15,6 +17,7 @@ export class AuthController {
constructor( constructor(
private config: ConfigService, private config: ConfigService,
private httpService: HttpService, private httpService: HttpService,
private jwtService: JwtService,
) {} ) {}
@Get('/vatsim') @Get('/vatsim')
@@ -23,7 +26,8 @@ export class AuthController {
description: 'The authorization code', description: 'The authorization code',
type: String, type: String,
}) })
async vatsim(@Query('code') code): Promise<void> { @Redirect()
async vatsim(@Query('code') code) {
if (!code) { if (!code) {
throw new HttpException( throw new HttpException(
'Code not found in the query', 'Code not found in the query',
@@ -53,7 +57,7 @@ export class AuthController {
), ),
); );
const user = await lastValueFrom( const userdata = await lastValueFrom(
this.httpService this.httpService
.get( .get(
`${this.config.get<string>( `${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')}`,
};
}
} }
} }