|
@@ -11,13 +11,13 @@ 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';
|
|
|
|
|
|
@Controller('auth')
|
|
|
export class AuthController {
|
|
|
constructor(
|
|
|
private config: ConfigService,
|
|
|
- private httpService: HttpService,
|
|
|
- private jwtService: JwtService,
|
|
|
+ private authService: AuthService,
|
|
|
) {}
|
|
|
|
|
|
@Get('/vatsim')
|
|
@@ -35,58 +35,15 @@ export class AuthController {
|
|
|
);
|
|
|
}
|
|
|
|
|
|
- const token = await lastValueFrom(
|
|
|
- this.httpService
|
|
|
- .post(
|
|
|
- `${this.config.get<string>(
|
|
|
- 'vatsim-auth.base-url',
|
|
|
- )}/${this.config.get<string>('vatsim-auth.token-endpoint')}`,
|
|
|
- {
|
|
|
- grant_type: 'authorization_code',
|
|
|
- client_id: this.config.get<string>('vatsim-auth.client-id'),
|
|
|
- client_secret: this.config.get<string>('vatsim-auth.client-secret'),
|
|
|
- redirect_uri: 'http://localhost:3000/auth/vatsim',
|
|
|
- code,
|
|
|
- },
|
|
|
- )
|
|
|
- .pipe(
|
|
|
- map((response) => response.data.access_token),
|
|
|
- catchError((err) => {
|
|
|
- throw new HttpException(err.response.data, err.response.status);
|
|
|
- }),
|
|
|
- ),
|
|
|
- );
|
|
|
+ const token = await this.authService.login(code);
|
|
|
|
|
|
- const userdata = await lastValueFrom(
|
|
|
- this.httpService
|
|
|
- .get(
|
|
|
- `${this.config.get<string>(
|
|
|
- 'vatsim-auth.base-url',
|
|
|
- )}/${this.config.get<string>('vatsim-auth.user-endpoint')}`,
|
|
|
- {
|
|
|
- headers: {
|
|
|
- Authorization: `Bearer ${token}`,
|
|
|
- Accept: 'application/json',
|
|
|
- },
|
|
|
- },
|
|
|
- )
|
|
|
- .pipe(
|
|
|
- map((response) => response.data.data),
|
|
|
- catchError((err) => {
|
|
|
- throw new HttpException(err.response.data, err.response.status);
|
|
|
- }),
|
|
|
- ),
|
|
|
- );
|
|
|
-
|
|
|
- if (userdata.oauth.token_valid) {
|
|
|
- const payload = { username: userdata.cid, sub: token };
|
|
|
- const accessToken = this.jwtService.sign(payload);
|
|
|
+ if (token !== undefined) {
|
|
|
return {
|
|
|
url: `${this.config.get<string>(
|
|
|
'frontend.base-url',
|
|
|
)}/${this.config.get<string>(
|
|
|
'frontend.login-endpoint',
|
|
|
- )}?token=${accessToken}`,
|
|
|
+ )}?token=${token}`,
|
|
|
};
|
|
|
} else {
|
|
|
return {
|