@@ -26,15 +26,44 @@ export class VkProvider extends SocialAbstract implements SocialProvider {
26
26
'video' ,
27
27
] ;
28
28
29
- async refreshToken ( refresh_token : string ) : Promise < AuthTokenDetails > {
29
+ async refreshToken ( refresh : string ) : Promise < AuthTokenDetails > {
30
+ const [ oldRefreshToken , device_id ] = refresh . split ( '&&&&' ) ;
31
+ const formData = new FormData ( ) ;
32
+ formData . append ( 'grant_type' , 'refresh_token' ) ;
33
+ formData . append ( 'refresh_token' , oldRefreshToken ) ;
34
+ formData . append ( 'client_id' , process . env . VK_ID ! ) ;
35
+ formData . append ( 'device_id' , device_id ) ;
36
+ formData . append ( 'state' , makeId ( 32 ) ) ;
37
+ formData . append ( 'scope' , this . scopes . join ( ' ' ) ) ;
38
+
39
+ const { access_token, refresh_token, expires_in } = await (
40
+ await this . fetch ( 'https://id.vk.com/oauth2/auth' , {
41
+ method : 'POST' ,
42
+ body : formData ,
43
+ } )
44
+ ) . json ( ) ;
45
+
46
+ const newFormData = new FormData ( ) ;
47
+ newFormData . append ( 'client_id' , process . env . VK_ID ! ) ;
48
+ newFormData . append ( 'access_token' , access_token ) ;
49
+
50
+ const {
51
+ user : { user_id, first_name, last_name, avatar } ,
52
+ } = await (
53
+ await this . fetch ( 'https://id.vk.com/oauth2/user_info' , {
54
+ method : 'POST' ,
55
+ body : newFormData ,
56
+ } )
57
+ ) . json ( ) ;
58
+
30
59
return {
31
- refreshToken : '' ,
32
- expiresIn : 0 ,
33
- accessToken : '' ,
34
- id : '' ,
35
- name : '' ,
36
- picture : '' ,
37
- username : '' ,
60
+ id : user_id ,
61
+ name : first_name + ' ' + last_name ,
62
+ accessToken : access_token ,
63
+ refreshToken : refresh_token + '&&&&' + device_id ,
64
+ expiresIn : dayjs ( ) . add ( expires_in , 'seconds' ) . unix ( ) - dayjs ( ) . unix ( ) ,
65
+ picture : avatar ,
66
+ username : first_name . toLowerCase ( ) ,
38
67
} ;
39
68
}
40
69
@@ -92,7 +121,7 @@ export class VkProvider extends SocialAbstract implements SocialProvider {
92
121
} /integrations/social/vk`
93
122
) ;
94
123
95
- const { access_token, scope, refresh_token } = await (
124
+ const { access_token, scope, refresh_token, expires_in } = await (
96
125
await this . fetch ( 'https://id.vk.com/oauth2/auth' , {
97
126
method : 'POST' ,
98
127
body : formData ,
@@ -116,8 +145,8 @@ export class VkProvider extends SocialAbstract implements SocialProvider {
116
145
id : user_id ,
117
146
name : first_name + ' ' + last_name ,
118
147
accessToken : access_token ,
119
- refreshToken : access_token ,
120
- expiresIn : dayjs ( ) . add ( 59 , 'days ' ) . unix ( ) - dayjs ( ) . unix ( ) ,
148
+ refreshToken : refresh_token + '&&&&' + device_id ,
149
+ expiresIn : dayjs ( ) . add ( expires_in , 'seconds ' ) . unix ( ) - dayjs ( ) . unix ( ) ,
121
150
picture : avatar ,
122
151
username : first_name . toLowerCase ( ) ,
123
152
} ;
@@ -197,7 +226,7 @@ export class VkProvider extends SocialAbstract implements SocialProvider {
197
226
198
227
let i = 0 ;
199
228
for ( const post of postDetails ) {
200
- const list = ( uploading ?. [ i ] || [ ] ) ;
229
+ const list = uploading ?. [ i ] || [ ] ;
201
230
202
231
const body = new FormData ( ) ;
203
232
body . append ( 'message' , post . message ) ;
@@ -224,7 +253,6 @@ export class VkProvider extends SocialAbstract implements SocialProvider {
224
253
)
225
254
) . json ( ) ;
226
255
227
-
228
256
values . push ( {
229
257
id : post . id ,
230
258
postId : String ( response ?. post_id || response ?. comment_id ) ,
0 commit comments