1
1
import { AppsRepository } from "@/modules/apps/apps.repository" ;
2
+ import { GcalService } from "@/modules/apps/services/gcal.service" ;
2
3
import { GetUser } from "@/modules/auth/decorators/get-user/get-user.decorator" ;
3
4
import { AccessTokenGuard } from "@/modules/auth/guards/access-token/access-token.guard" ;
4
5
import { CredentialsRepository } from "@/modules/credentials/credentials.repository" ;
@@ -11,12 +12,12 @@ import {
11
12
HttpCode ,
12
13
HttpStatus ,
13
14
Logger ,
14
- NotFoundException ,
15
15
Query ,
16
16
Redirect ,
17
17
Req ,
18
18
UnauthorizedException ,
19
19
UseGuards ,
20
+ Headers ,
20
21
} from "@nestjs/common" ;
21
22
import { ConfigService } from "@nestjs/config" ;
22
23
import { Request } from "express" ;
@@ -43,27 +44,21 @@ export class GcalController {
43
44
private readonly credentialRepository : CredentialsRepository ,
44
45
private readonly tokensRepository : TokensRepository ,
45
46
private readonly selectedCalendarsRepository : SelectedCalendarsRepository ,
46
- private readonly config : ConfigService
47
+ private readonly config : ConfigService ,
48
+ private readonly gcalService : GcalService
47
49
) { }
48
50
49
51
private redirectUri = `${ this . config . get ( "api.url" ) } /platform/gcal/oauth/save` ;
50
52
51
53
@Get ( "/oauth/auth-url" )
52
54
@HttpCode ( HttpStatus . OK )
53
55
@UseGuards ( AccessTokenGuard )
54
- async redirect ( @Req ( ) req : Request ) : Promise < ApiResponse < { authUrl : string } > > {
55
- const app = await this . appRepository . getAppBySlug ( "google-calendar" ) ;
56
-
57
- if ( ! app ) {
58
- throw new NotFoundException ( ) ;
59
- }
60
-
61
- const { client_id, client_secret } = z
62
- . object ( { client_id : z . string ( ) , client_secret : z . string ( ) } )
63
- . parse ( app . keys ) ;
64
-
65
- const oAuth2Client = new google . auth . OAuth2 ( client_id , client_secret , this . redirectUri ) ;
66
- const accessToken = req . get ( "Authorization" ) ?. replace ( "Bearer " , "" ) ;
56
+ async redirect (
57
+ @Headers ( "Authorization" ) authorization : string ,
58
+ @Req ( ) req : Request
59
+ ) : Promise < ApiResponse < { authUrl : string } > > {
60
+ const oAuth2Client = await this . gcalService . getOAuthClient ( this . redirectUri ) ;
61
+ const accessToken = authorization . replace ( "Bearer " , "" ) ;
67
62
const origin = req . get ( "origin" ) ?? req . get ( "host" ) ;
68
63
const authUrl = oAuth2Client . generateAuthUrl ( {
69
64
access_type : "offline" ,
@@ -97,16 +92,7 @@ export class GcalController {
97
92
throw new UnauthorizedException ( "Invalid Access token." ) ;
98
93
}
99
94
100
- const app = await this . appRepository . getAppBySlug ( "google-calendar" ) ;
101
-
102
- if ( ! app ) {
103
- throw new NotFoundException ( ) ;
104
- }
105
-
106
- const { client_id, client_secret } = z
107
- . object ( { client_id : z . string ( ) , client_secret : z . string ( ) } )
108
- . parse ( app . keys ) ;
109
- const oAuth2Client = new google . auth . OAuth2 ( client_id , client_secret , this . redirectUri ) ;
95
+ const oAuth2Client = await this . gcalService . getOAuthClient ( this . redirectUri ) ;
110
96
const token = await oAuth2Client . getToken ( parsedCode ) ;
111
97
const key = token . res ?. data ;
112
98
const credential = await this . credentialRepository . createAppCredential (
0 commit comments