File size: 1,074 Bytes
3206347
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import getCurrentUser from '../common/get-current-user.js';
import qs from 'qs';

const verifyCredentials = async ($) => {
  const oauthRedirectUrlField = $.app.auth.fields.find(
    (field) => field.key == 'oAuthRedirectUrl'
  );
  const redirectUri = oauthRedirectUrlField.value;
  const searchParams = qs.stringify({
    code: $.auth.data.code,
    grant_type: 'authorization_code',
    client_id: $.auth.data.consumerKey,
    client_secret: $.auth.data.consumerSecret,
    redirect_uri: redirectUri,
  });
  const { data } = await $.http.post(
    `${$.auth.data.oauth2Url}/token?${searchParams}`
  );

  await $.auth.set({
    accessToken: data.access_token,
    refreshToken: data.refresh_token,
    tokenType: data.token_type,
    idToken: data.id_token,
    instanceUrl: data.instance_url,
    signature: data.signature,
    userId: data.id,
    screenName: data.instance_url,
  });

  const currentUser = await getCurrentUser($);

  await $.auth.set({
    screenName: `${currentUser.displayName} - ${data.instance_url}`,
  });
};

export default verifyCredentials;