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,
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')}`,
};
}
}
}