@@ -26,15 +26,44 @@ export class VkProvider extends SocialAbstract implements SocialProvider {
2626 'video' ,
2727 ] ;
2828
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+
3059 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 ( ) ,
3867 } ;
3968 }
4069
@@ -92,7 +121,7 @@ export class VkProvider extends SocialAbstract implements SocialProvider {
92121 } /integrations/social/vk`
93122 ) ;
94123
95- const { access_token, scope, refresh_token } = await (
124+ const { access_token, scope, refresh_token, expires_in } = await (
96125 await this . fetch ( 'https://id.vk.com/oauth2/auth' , {
97126 method : 'POST' ,
98127 body : formData ,
@@ -116,8 +145,8 @@ export class VkProvider extends SocialAbstract implements SocialProvider {
116145 id : user_id ,
117146 name : first_name + ' ' + last_name ,
118147 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 ( ) ,
121150 picture : avatar ,
122151 username : first_name . toLowerCase ( ) ,
123152 } ;
@@ -197,7 +226,7 @@ export class VkProvider extends SocialAbstract implements SocialProvider {
197226
198227 let i = 0 ;
199228 for ( const post of postDetails ) {
200- const list = ( uploading ?. [ i ] || [ ] ) ;
229+ const list = uploading ?. [ i ] || [ ] ;
201230
202231 const body = new FormData ( ) ;
203232 body . append ( 'message' , post . message ) ;
@@ -224,7 +253,6 @@ export class VkProvider extends SocialAbstract implements SocialProvider {
224253 )
225254 ) . json ( ) ;
226255
227-
228256 values . push ( {
229257 id : post . id ,
230258 postId : String ( response ?. post_id || response ?. comment_id ) ,
0 commit comments