@resultcrafter/aimanager-instagram-connector 0.4.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -113,7 +113,7 @@ router.post('/install', async (req, res) => {
113
113
  "project_id": project_id,
114
114
  "app_id": app_id,
115
115
  "token": token,
116
- "oauth_url": BASE_URL + '/auth/instagram/start'
116
+ "oauth_url": BASE_URL + '/auth/instagram/start?project_id=' + project_id + '&token=' + token + '&app_id=' + app_id
117
117
  }
118
118
  }));
119
119
 
@@ -728,6 +728,25 @@ router.get('/auth/instagram/start', async (req, res) => {
728
728
  }
729
729
  });
730
730
 
731
+ function oauthCallbackResponse(res, success, data) {
732
+ var username = data && data.username ? data.username : '';
733
+ var error = data && data.error ? data.error : '';
734
+ var redirectUri = data && data.redirect_uri ? data.redirect_uri : '';
735
+ res.send('<!DOCTYPE html><html><body><script>' +
736
+ 'if (window.opener) {' +
737
+ ' window.opener.postMessage({' +
738
+ ' type: "oauth-complete",' +
739
+ ' success: ' + (success ? 'true' : 'false') + ',' +
740
+ (username ? ' username: "' + username.replace(/"/g, '\\"') + '",' : '') +
741
+ (error ? ' error: "' + error.replace(/"/g, '\\"') + '"' : '') +
742
+ ' }, "*");' +
743
+ ' window.close();' +
744
+ '} else {' +
745
+ ' window.location.href = "' + redirectUri.replace(/"/g, '') + '";' +
746
+ '}' +
747
+ '</script></body></html>');
748
+ }
749
+
731
750
  router.get('/oauth-callback', async (req, res) => {
732
751
  winston.verbose("(ig) /oauth-callback");
733
752
 
@@ -735,16 +754,17 @@ router.get('/oauth-callback', async (req, res) => {
735
754
  var stateStr = req.query.state;
736
755
  if (!code || !stateStr) {
737
756
  winston.error("(ig) Missing code or state parameter");
738
- return res.status(400).send("Missing authorization code or state");
757
+ return oauthCallbackResponse(res, false, { error: 'Missing authorization code or state' });
739
758
  }
740
759
 
741
760
  var state = instagramOAuth.verifyState(stateStr);
742
761
  if (!state) {
743
762
  winston.error("(ig) Invalid or expired state");
744
- return res.status(400).send("Invalid or expired state parameter. Please try connecting again.");
763
+ return oauthCallbackResponse(res, false, { error: 'Invalid or expired state parameter' });
745
764
  }
746
765
 
747
766
  var projectId = state.project_id;
767
+ var redirect_uri = DASHBOARD_BASE_URL + "/#/project/" + projectId + "/integrations?name=instagram";
748
768
 
749
769
  try {
750
770
  var tokenData = await instagramOAuth.exchangeCodeForToken(code);
@@ -758,11 +778,10 @@ router.get('/oauth-callback', async (req, res) => {
758
778
  settings.connected = true;
759
779
  await db.set(CONTENT_KEY, settings);
760
780
 
761
- var redirect_uri = DASHBOARD_BASE_URL + "/#/project/" + projectId + "/integrations?name=instagram";
762
- res.redirect(redirect_uri);
781
+ oauthCallbackResponse(res, true, { username: tokenData.username, redirect_uri: redirect_uri });
763
782
  } catch (err) {
764
783
  winston.error("(ig) OAuth callback error: ", err?.response?.data || err.message || err);
765
- res.status(500).send("Authentication failed: " + (err.message || 'Unknown error'));
784
+ oauthCallbackResponse(res, false, { error: err.message || 'Authentication failed', redirect_uri: redirect_uri });
766
785
  }
767
786
  })
768
787
 
@@ -821,7 +840,8 @@ router.post('/enablePage', async (req, res) => {
821
840
  brand_name: BRAND_NAME,
822
841
  show_info_message: settings.show_info_message !== undefined ? settings.show_info_message : true,
823
842
  instagram_username: settings.instagram_username || settings.user_info?.username || 'Instagram Account',
824
- subscription_id: settings.subscription_id
843
+ subscription_id: settings.subscription_id,
844
+ oauth_url: BASE_URL + '/auth/instagram/start?project_id=' + project_id + '&token=' + token + '&app_id=' + app_id
825
845
  }
826
846
  var html = template(replacements)
827
847
  return res.send(html);
@@ -846,7 +866,8 @@ router.post('/enablePage', async (req, res) => {
846
866
  show_alert_modal: true,
847
867
  show_info_message: settings.show_info_message !== undefined ? settings.show_info_message : true,
848
868
  instagram_username: settings.instagram_username || settings.user_info?.username || 'Instagram Account',
849
- subscription_id: settings.subscription_id
869
+ subscription_id: settings.subscription_id,
870
+ oauth_url: BASE_URL + '/auth/instagram/start?project_id=' + project_id + '&token=' + token + '&app_id=' + app_id
850
871
  }
851
872
  var html = template(replacements)
852
873
  return res.send(html);
@@ -894,7 +915,8 @@ router.post('/disablePage', async (req, res) => {
894
915
  brand_name: BRAND_NAME,
895
916
  show_info_message: settings.show_info_message !== undefined ? settings.show_info_message : true,
896
917
  instagram_username: settings.instagram_username || settings.user_info?.username || 'Instagram Account',
897
- subscription_id: settings.subscription_id
918
+ subscription_id: settings.subscription_id,
919
+ oauth_url: BASE_URL + '/auth/instagram/start?project_id=' + project_id + '&token=' + token + '&app_id=' + app_id
898
920
  }
899
921
  var html = template(replacements)
900
922
  return res.send(html);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@resultcrafter/aimanager-instagram-connector",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "description": "AI Manager Instagram DM connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -242,6 +242,18 @@
242
242
 
243
243
  <script>
244
244
 
245
+ var oauthPopup = null;
246
+
247
+ window.addEventListener('message', function(event) {
248
+ if (event.data && event.data.type === 'oauth-complete') {
249
+ if (event.data.success) {
250
+ window.location.reload();
251
+ } else {
252
+ alert('Instagram connection failed: ' + (event.data.error || 'Unknown error'));
253
+ }
254
+ }
255
+ });
256
+
245
257
  function connectInstagram() {
246
258
  var width = 600;
247
259
  var height = 700;
@@ -259,7 +271,7 @@
259
271
  } catch (e) {
260
272
 
261
273
  }
262
- }, 1000);
274
+ }, 2000);
263
275
  }
264
276
 
265
277
  function update_advanced() {