introduce the vatsim oauth logic
This commit is contained in:
		
							
								
								
									
										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