@@ -9,8 +9,10 @@ import {
9
9
Request ,
10
10
SerializeOptions ,
11
11
Delete ,
12
+ InternalServerErrorException ,
13
+ NotFoundException ,
12
14
} from '@nestjs/common' ;
13
- import { LoginSocialDto } from 'src/core/dtos/auth' ;
15
+ import { LoginSocialDto , OauthDto } from 'src/core/dtos/auth' ;
14
16
import { UserFactoryService } from '../user/user-factory.service' ;
15
17
import { AuthUseCases } from 'src/features/auth/auth.usecase' ;
16
18
import { AuthGuard } from '@nestjs/passport' ;
@@ -23,6 +25,8 @@ import {
23
25
import { LoginResponseType } from 'src/features/auth/types/login-response.type' ;
24
26
import { AwsS3Service } from '../image/aws-s3/aws-s3.service' ;
25
27
import { ApiKeyGuard } from 'src/utils/strategies/api-key.strategy' ;
28
+ import axios from 'axios' ;
29
+ import { EnvironmentConfigService } from 'src/core/config/environment/environments' ;
26
30
27
31
@ApiTags ( 'auth' )
28
32
@ApiSecurity ( 'api_key' , [ 'api_key' ] )
@@ -33,11 +37,36 @@ import { ApiKeyGuard } from 'src/utils/strategies/api-key.strategy';
33
37
} )
34
38
export class AuthController {
35
39
constructor (
40
+ private readonly environment : EnvironmentConfigService ,
36
41
private authUseCases : AuthUseCases ,
37
42
private userFactoryService : UserFactoryService ,
38
43
private awsS3Service : AwsS3Service ,
39
44
) { }
40
45
46
+ @ApiOperation ( { summary : 'Get Oauth Token' , description : 'Get Oauth token from code' } )
47
+ @Post ( 'token' )
48
+ async getOauthToken ( @Body ( ) oauth : OauthDto ) {
49
+ try {
50
+ const res = await axios . post ( "https://oauth2.googleapis.com/token" , null , {
51
+ params : {
52
+ client_id : oauth . clientId ,
53
+ client_secret : this . environment . getOauthSecret ( ) ,
54
+ code : oauth . code ,
55
+ grant_type : 'authorization_code' ,
56
+ redirect_uri : oauth . redirectUri ,
57
+ }
58
+ } ) ;
59
+
60
+ if ( res . status == 200 ) {
61
+ return res . data ;
62
+ }
63
+
64
+ throw new NotFoundException ( )
65
+ } catch ( error ) {
66
+ throw new InternalServerErrorException ( ) ;
67
+ }
68
+ }
69
+
41
70
@ApiOperation ( { summary : 'Login' , description : 'Login with Social Media' } )
42
71
@Post ( )
43
72
loginWithSocial ( @Body ( ) loginWithSocial : LoginSocialDto ) {
0 commit comments