introduce the vatsim oauth logic
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { HttpModule } from '@nestjs/axios';
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||
import { MongooseModule } from '@nestjs/mongoose';
|
||||
@@ -8,6 +9,8 @@ import { AirportModule } from './airport/airport.module';
|
||||
import { LoggingModule } from './logging/logging.module';
|
||||
import { InboundModule } from './inbound/inbound.module';
|
||||
import { WeatherModule } from './weather/weather.module';
|
||||
import { AuthController } from './auth/auth.controller';
|
||||
import { AuthModule } from './auth/auth.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@@ -26,12 +29,17 @@ import { WeatherModule } from './weather/weather.module';
|
||||
)}`,
|
||||
}),
|
||||
}),
|
||||
HttpModule.register({
|
||||
timeout: 5000,
|
||||
maxRedirects: 3,
|
||||
}),
|
||||
VersioningModule,
|
||||
PerformanceModule,
|
||||
AirportModule,
|
||||
LoggingModule,
|
||||
InboundModule,
|
||||
WeatherModule,
|
||||
AuthModule,
|
||||
],
|
||||
})
|
||||
export class AppModule {}
|
||||
|
||||
18
src/auth/auth.controller.spec.ts
Normal file
18
src/auth/auth.controller.spec.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { AuthController } from './auth.controller';
|
||||
|
||||
describe('AuthController', () => {
|
||||
let controller: AuthController;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [AuthController],
|
||||
}).compile();
|
||||
|
||||
controller = module.get<AuthController>(AuthController);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(controller).toBeDefined();
|
||||
});
|
||||
});
|
||||
78
src/auth/auth.controller.ts
Normal file
78
src/auth/auth.controller.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import { HttpService } from '@nestjs/axios';
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
Query,
|
||||
} from '@nestjs/common';
|
||||
import { ApiQuery } from '@nestjs/swagger';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { catchError, lastValueFrom, map } from 'rxjs';
|
||||
|
||||
@Controller('auth')
|
||||
export class AuthController {
|
||||
constructor(
|
||||
private config: ConfigService,
|
||||
private httpService: HttpService,
|
||||
) {}
|
||||
|
||||
@Get('/vatsim')
|
||||
@ApiQuery({
|
||||
name: 'code',
|
||||
description: 'The authorization code',
|
||||
type: String,
|
||||
})
|
||||
async vatsim(@Query('code') code): Promise<void> {
|
||||
if (!code) {
|
||||
throw new HttpException(
|
||||
'Code not found in the query',
|
||||
HttpStatus.NOT_ACCEPTABLE,
|
||||
);
|
||||
}
|
||||
|
||||
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 user = 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);
|
||||
}),
|
||||
),
|
||||
);
|
||||
console.log(user);
|
||||
}
|
||||
}
|
||||
9
src/auth/auth.module.ts
Normal file
9
src/auth/auth.module.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { HttpModule } from '@nestjs/axios';
|
||||
import { Module } from '@nestjs/common';
|
||||
import { AuthController } from './auth.controller';
|
||||
|
||||
@Module({
|
||||
imports: [HttpModule],
|
||||
controllers: [AuthController],
|
||||
})
|
||||
export class AuthModule {}
|
||||
Reference in New Issue
Block a user