@upsnap/strapi 1.0.7 → 1.0.8

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.
@@ -36,7 +36,7 @@ const index = {
36
36
  defaultMessage: PLUGIN_ID.slice(0, 1).toUpperCase() + PLUGIN_ID.slice(1)
37
37
  },
38
38
  Component: async () => {
39
- const { App } = await import("./App-DQN0F1ZK.mjs");
39
+ const { App } = await import("./App-DJivreSE.mjs");
40
40
  return App;
41
41
  }
42
42
  });
@@ -32,7 +32,7 @@ const service = ({ strapi }) => ({
32
32
  const settings2 = await this.settingsStore.get();
33
33
  return settings2?.token || null;
34
34
  },
35
- async makeBackendRequest(endpoint, options, forValidation = false) {
35
+ async makeBackendRequest(endpoint, options, forValidation = false, sessionToken = "") {
36
36
  const token = await this.getToken();
37
37
  if (!token && !forValidation) {
38
38
  return { error: "No token found in settings" };
@@ -40,7 +40,7 @@ const service = ({ strapi }) => ({
40
40
  const response = await fetch(`${BACKEND_URL}${endpoint}`, {
41
41
  ...options,
42
42
  headers: {
43
- Authorization: `Bearer ${token}`,
43
+ Authorization: `Bearer ${sessionToken || token}`,
44
44
  "Content-Type": "application/json",
45
45
  ...options.headers || {}
46
46
  }
@@ -57,14 +57,16 @@ const settings = ({ strapi }) => ({
57
57
  };
58
58
  },
59
59
  async set(ctx) {
60
- const { token } = ctx.request.body;
61
- const isValidData = await service({ strapi }).makeBackendRequest("/tokens/validate", {
62
- method: "POST",
63
- body: JSON.stringify({ token })
64
- }, true);
65
- if (!isValidData?.data?.valid) {
66
- ctx.body = { ok: false, error: "Invalid token" };
67
- return;
60
+ const { token, logOut } = ctx.request.body;
61
+ if (!logOut) {
62
+ const isValidData = await service({ strapi }).makeBackendRequest("/tokens/validate", {
63
+ method: "POST",
64
+ body: JSON.stringify({ token })
65
+ }, true);
66
+ if (!isValidData?.data?.valid) {
67
+ ctx.body = { ok: false, error: "Invalid token" };
68
+ return;
69
+ }
68
70
  }
69
71
  const store = service({ strapi }).settingsStore;
70
72
  const current = await store.get() || {};
@@ -662,12 +664,212 @@ const regions = ({ strapi }) => ({
662
664
  ctx.body = { regionsData };
663
665
  }
664
666
  });
667
+ const userDetailsService = ({ strapi }) => ({
668
+ async createUserApiToken(sessionToken) {
669
+ const apiTokens = await service({ strapi }).makeBackendRequest(
670
+ `/tokens/generate`,
671
+ {
672
+ method: "POST",
673
+ body: JSON.stringify({
674
+ name: "For Strapi",
675
+ description: "Token for strapi plugin",
676
+ expires: 0
677
+ })
678
+ },
679
+ true,
680
+ sessionToken
681
+ );
682
+ if (apiTokens?.data?.token_hash) {
683
+ const token = apiTokens?.data?.token_hash;
684
+ return token;
685
+ }
686
+ return null;
687
+ },
688
+ async getUserApiToken(sessionToken) {
689
+ const apiTokens = await service({ strapi }).makeBackendRequest(
690
+ `/tokens`,
691
+ {
692
+ method: "GET"
693
+ },
694
+ true,
695
+ sessionToken
696
+ );
697
+ const tokens = apiTokens?.data?.tokens || [];
698
+ let apiToken = "";
699
+ if (apiTokens?.status === "success" && tokens.length === 0) {
700
+ apiToken = await this.createUserApiToken(sessionToken);
701
+ } else if (tokens.length > 0) {
702
+ apiToken = tokens?.[0]?.token_hash;
703
+ }
704
+ return apiToken;
705
+ },
706
+ async createInitialMonitor(site_url, apiToken) {
707
+ try {
708
+ const payload = {
709
+ name: "Default Monitor",
710
+ service_type: "website",
711
+ is_enabled: true,
712
+ channel_ids: [],
713
+ tag_ids: [],
714
+ regions: [
715
+ {
716
+ id: "default",
717
+ is_primary: true,
718
+ name: "Default (Server Region)"
719
+ }
720
+ ],
721
+ config: {
722
+ meta: {
723
+ follow_redirects: true,
724
+ timeout: 5,
725
+ url: site_url
726
+ },
727
+ services: {
728
+ broken_links: {
729
+ enabled: true,
730
+ monitor_interval: 86400
731
+ },
732
+ domain: {
733
+ enabled: true,
734
+ monitor_interval: 86400,
735
+ notify_days_before_expiry: 7
736
+ },
737
+ lighthouse: {
738
+ enabled: true,
739
+ strategy: "desktop",
740
+ monitor_interval: 604800
741
+ },
742
+ mixed_content: {
743
+ enabled: true,
744
+ monitor_interval: 86400
745
+ },
746
+ ssl: {
747
+ enabled: true,
748
+ monitor_interval: 86400,
749
+ notify_days_before_expiry: 7
750
+ },
751
+ uptime: {
752
+ enabled: true,
753
+ monitor_interval: 300
754
+ }
755
+ }
756
+ }
757
+ };
758
+ const monitorsData = await service({ strapi }).makeBackendRequest(`/user/monitors`, {
759
+ method: "POST",
760
+ body: JSON.stringify(payload)
761
+ }, true, apiToken);
762
+ if (monitorsData?.status === "success") {
763
+ const monitorId = monitorsData?.data?.monitor?.id;
764
+ return monitorId;
765
+ }
766
+ return null;
767
+ } catch (err) {
768
+ console.log("Error creating initial monitor ", err);
769
+ return null;
770
+ }
771
+ }
772
+ });
665
773
  const userDetails = ({ strapi }) => ({
666
774
  async getUserDetails(ctx) {
667
775
  const userDetailsData = await service({ strapi }).makeBackendRequest(`/user/details`, {
668
776
  method: "GET"
669
777
  });
670
778
  ctx.body = { userDetailsData };
779
+ },
780
+ async signUp(ctx) {
781
+ try {
782
+ const { email, password, source, site_url, fullName } = ctx.request.body;
783
+ const registerData = await service({ strapi }).makeBackendRequest(
784
+ `/user/register`,
785
+ {
786
+ method: "POST",
787
+ body: JSON.stringify({
788
+ email,
789
+ password,
790
+ source,
791
+ full_name: fullName
792
+ })
793
+ },
794
+ true
795
+ );
796
+ if (registerData?.data?.token) {
797
+ const apiToken = await userDetailsService({ strapi }).getUserApiToken(registerData?.data?.token);
798
+ const monitorId = await userDetailsService({ strapi }).createInitialMonitor(site_url, apiToken);
799
+ if (!apiToken || !monitorId) {
800
+ ctx.body = { ok: false, message: "Error creating API token or Monitor" };
801
+ return;
802
+ }
803
+ const store = service({ strapi }).settingsStore;
804
+ const current = await store.get() || {};
805
+ await store.set({
806
+ value: {
807
+ ...current,
808
+ token: apiToken,
809
+ primaryMonitorId: monitorId
810
+ }
811
+ });
812
+ return ctx.body = { ok: true, message: registerData?.data?.message };
813
+ }
814
+ ctx.body = { ok: false };
815
+ } catch (err) {
816
+ console.log("Error signing up ", err);
817
+ ctx.body = { ok: false };
818
+ }
819
+ },
820
+ async signIn(ctx) {
821
+ try {
822
+ const { email, password } = ctx.request.body;
823
+ const loginData = await service({ strapi }).makeBackendRequest(
824
+ `/user/login`,
825
+ {
826
+ method: "POST",
827
+ body: JSON.stringify({
828
+ email,
829
+ password
830
+ })
831
+ },
832
+ true
833
+ );
834
+ if (loginData?.data?.token) {
835
+ const apiToken = await userDetailsService({ strapi }).getUserApiToken(loginData?.data?.token);
836
+ const store = service({ strapi }).settingsStore;
837
+ const current = await store.get() || {};
838
+ await store.set({
839
+ value: {
840
+ ...current,
841
+ token: apiToken
842
+ }
843
+ });
844
+ return ctx.body = { ok: true };
845
+ }
846
+ ctx.body = { ok: false, message: loginData?.message };
847
+ } catch (err) {
848
+ console.log("Error signing in ", err);
849
+ ctx.body = { ok: false };
850
+ }
851
+ },
852
+ async forgotPassword(ctx) {
853
+ try {
854
+ const { email } = ctx.request.body;
855
+ const forgotPasswordData = await service({ strapi }).makeBackendRequest(
856
+ `/user/forgot-password`,
857
+ {
858
+ method: "POST",
859
+ body: JSON.stringify({
860
+ email
861
+ })
862
+ },
863
+ true
864
+ );
865
+ if (forgotPasswordData?.status === "success") {
866
+ return ctx.body = { ok: true, message: forgotPasswordData?.data?.message };
867
+ }
868
+ ctx.body = { ok: false };
869
+ } catch (err) {
870
+ console.log("Error for forgot password ", err);
871
+ ctx.body = { ok: false };
872
+ }
671
873
  }
672
874
  });
673
875
  const notificationChannels = ({ strapi }) => ({
@@ -1068,12 +1270,40 @@ const routes = {
1068
1270
  policies: [],
1069
1271
  auth: false
1070
1272
  }
1273
+ },
1274
+ {
1275
+ method: "POST",
1276
+ path: "/signup",
1277
+ handler: "userDetails.signUp",
1278
+ config: {
1279
+ policies: [],
1280
+ auth: false
1281
+ }
1282
+ },
1283
+ {
1284
+ method: "POST",
1285
+ path: "/login",
1286
+ handler: "userDetails.signIn",
1287
+ config: {
1288
+ policies: [],
1289
+ auth: false
1290
+ }
1291
+ },
1292
+ {
1293
+ method: "POST",
1294
+ path: "/forgot-password",
1295
+ handler: "userDetails.forgotPassword",
1296
+ config: {
1297
+ policies: [],
1298
+ auth: false
1299
+ }
1071
1300
  }
1072
1301
  ]
1073
1302
  }
1074
1303
  };
1075
1304
  const services = {
1076
- service
1305
+ service,
1306
+ userDetailsService
1077
1307
  };
1078
1308
  const index = {
1079
1309
  register,
@@ -30,7 +30,7 @@ const service = ({ strapi }) => ({
30
30
  const settings2 = await this.settingsStore.get();
31
31
  return settings2?.token || null;
32
32
  },
33
- async makeBackendRequest(endpoint, options, forValidation = false) {
33
+ async makeBackendRequest(endpoint, options, forValidation = false, sessionToken = "") {
34
34
  const token = await this.getToken();
35
35
  if (!token && !forValidation) {
36
36
  return { error: "No token found in settings" };
@@ -38,7 +38,7 @@ const service = ({ strapi }) => ({
38
38
  const response = await fetch(`${BACKEND_URL}${endpoint}`, {
39
39
  ...options,
40
40
  headers: {
41
- Authorization: `Bearer ${token}`,
41
+ Authorization: `Bearer ${sessionToken || token}`,
42
42
  "Content-Type": "application/json",
43
43
  ...options.headers || {}
44
44
  }
@@ -55,14 +55,16 @@ const settings = ({ strapi }) => ({
55
55
  };
56
56
  },
57
57
  async set(ctx) {
58
- const { token } = ctx.request.body;
59
- const isValidData = await service({ strapi }).makeBackendRequest("/tokens/validate", {
60
- method: "POST",
61
- body: JSON.stringify({ token })
62
- }, true);
63
- if (!isValidData?.data?.valid) {
64
- ctx.body = { ok: false, error: "Invalid token" };
65
- return;
58
+ const { token, logOut } = ctx.request.body;
59
+ if (!logOut) {
60
+ const isValidData = await service({ strapi }).makeBackendRequest("/tokens/validate", {
61
+ method: "POST",
62
+ body: JSON.stringify({ token })
63
+ }, true);
64
+ if (!isValidData?.data?.valid) {
65
+ ctx.body = { ok: false, error: "Invalid token" };
66
+ return;
67
+ }
66
68
  }
67
69
  const store = service({ strapi }).settingsStore;
68
70
  const current = await store.get() || {};
@@ -660,12 +662,212 @@ const regions = ({ strapi }) => ({
660
662
  ctx.body = { regionsData };
661
663
  }
662
664
  });
665
+ const userDetailsService = ({ strapi }) => ({
666
+ async createUserApiToken(sessionToken) {
667
+ const apiTokens = await service({ strapi }).makeBackendRequest(
668
+ `/tokens/generate`,
669
+ {
670
+ method: "POST",
671
+ body: JSON.stringify({
672
+ name: "For Strapi",
673
+ description: "Token for strapi plugin",
674
+ expires: 0
675
+ })
676
+ },
677
+ true,
678
+ sessionToken
679
+ );
680
+ if (apiTokens?.data?.token_hash) {
681
+ const token = apiTokens?.data?.token_hash;
682
+ return token;
683
+ }
684
+ return null;
685
+ },
686
+ async getUserApiToken(sessionToken) {
687
+ const apiTokens = await service({ strapi }).makeBackendRequest(
688
+ `/tokens`,
689
+ {
690
+ method: "GET"
691
+ },
692
+ true,
693
+ sessionToken
694
+ );
695
+ const tokens = apiTokens?.data?.tokens || [];
696
+ let apiToken = "";
697
+ if (apiTokens?.status === "success" && tokens.length === 0) {
698
+ apiToken = await this.createUserApiToken(sessionToken);
699
+ } else if (tokens.length > 0) {
700
+ apiToken = tokens?.[0]?.token_hash;
701
+ }
702
+ return apiToken;
703
+ },
704
+ async createInitialMonitor(site_url, apiToken) {
705
+ try {
706
+ const payload = {
707
+ name: "Default Monitor",
708
+ service_type: "website",
709
+ is_enabled: true,
710
+ channel_ids: [],
711
+ tag_ids: [],
712
+ regions: [
713
+ {
714
+ id: "default",
715
+ is_primary: true,
716
+ name: "Default (Server Region)"
717
+ }
718
+ ],
719
+ config: {
720
+ meta: {
721
+ follow_redirects: true,
722
+ timeout: 5,
723
+ url: site_url
724
+ },
725
+ services: {
726
+ broken_links: {
727
+ enabled: true,
728
+ monitor_interval: 86400
729
+ },
730
+ domain: {
731
+ enabled: true,
732
+ monitor_interval: 86400,
733
+ notify_days_before_expiry: 7
734
+ },
735
+ lighthouse: {
736
+ enabled: true,
737
+ strategy: "desktop",
738
+ monitor_interval: 604800
739
+ },
740
+ mixed_content: {
741
+ enabled: true,
742
+ monitor_interval: 86400
743
+ },
744
+ ssl: {
745
+ enabled: true,
746
+ monitor_interval: 86400,
747
+ notify_days_before_expiry: 7
748
+ },
749
+ uptime: {
750
+ enabled: true,
751
+ monitor_interval: 300
752
+ }
753
+ }
754
+ }
755
+ };
756
+ const monitorsData = await service({ strapi }).makeBackendRequest(`/user/monitors`, {
757
+ method: "POST",
758
+ body: JSON.stringify(payload)
759
+ }, true, apiToken);
760
+ if (monitorsData?.status === "success") {
761
+ const monitorId = monitorsData?.data?.monitor?.id;
762
+ return monitorId;
763
+ }
764
+ return null;
765
+ } catch (err) {
766
+ console.log("Error creating initial monitor ", err);
767
+ return null;
768
+ }
769
+ }
770
+ });
663
771
  const userDetails = ({ strapi }) => ({
664
772
  async getUserDetails(ctx) {
665
773
  const userDetailsData = await service({ strapi }).makeBackendRequest(`/user/details`, {
666
774
  method: "GET"
667
775
  });
668
776
  ctx.body = { userDetailsData };
777
+ },
778
+ async signUp(ctx) {
779
+ try {
780
+ const { email, password, source, site_url, fullName } = ctx.request.body;
781
+ const registerData = await service({ strapi }).makeBackendRequest(
782
+ `/user/register`,
783
+ {
784
+ method: "POST",
785
+ body: JSON.stringify({
786
+ email,
787
+ password,
788
+ source,
789
+ full_name: fullName
790
+ })
791
+ },
792
+ true
793
+ );
794
+ if (registerData?.data?.token) {
795
+ const apiToken = await userDetailsService({ strapi }).getUserApiToken(registerData?.data?.token);
796
+ const monitorId = await userDetailsService({ strapi }).createInitialMonitor(site_url, apiToken);
797
+ if (!apiToken || !monitorId) {
798
+ ctx.body = { ok: false, message: "Error creating API token or Monitor" };
799
+ return;
800
+ }
801
+ const store = service({ strapi }).settingsStore;
802
+ const current = await store.get() || {};
803
+ await store.set({
804
+ value: {
805
+ ...current,
806
+ token: apiToken,
807
+ primaryMonitorId: monitorId
808
+ }
809
+ });
810
+ return ctx.body = { ok: true, message: registerData?.data?.message };
811
+ }
812
+ ctx.body = { ok: false };
813
+ } catch (err) {
814
+ console.log("Error signing up ", err);
815
+ ctx.body = { ok: false };
816
+ }
817
+ },
818
+ async signIn(ctx) {
819
+ try {
820
+ const { email, password } = ctx.request.body;
821
+ const loginData = await service({ strapi }).makeBackendRequest(
822
+ `/user/login`,
823
+ {
824
+ method: "POST",
825
+ body: JSON.stringify({
826
+ email,
827
+ password
828
+ })
829
+ },
830
+ true
831
+ );
832
+ if (loginData?.data?.token) {
833
+ const apiToken = await userDetailsService({ strapi }).getUserApiToken(loginData?.data?.token);
834
+ const store = service({ strapi }).settingsStore;
835
+ const current = await store.get() || {};
836
+ await store.set({
837
+ value: {
838
+ ...current,
839
+ token: apiToken
840
+ }
841
+ });
842
+ return ctx.body = { ok: true };
843
+ }
844
+ ctx.body = { ok: false, message: loginData?.message };
845
+ } catch (err) {
846
+ console.log("Error signing in ", err);
847
+ ctx.body = { ok: false };
848
+ }
849
+ },
850
+ async forgotPassword(ctx) {
851
+ try {
852
+ const { email } = ctx.request.body;
853
+ const forgotPasswordData = await service({ strapi }).makeBackendRequest(
854
+ `/user/forgot-password`,
855
+ {
856
+ method: "POST",
857
+ body: JSON.stringify({
858
+ email
859
+ })
860
+ },
861
+ true
862
+ );
863
+ if (forgotPasswordData?.status === "success") {
864
+ return ctx.body = { ok: true, message: forgotPasswordData?.data?.message };
865
+ }
866
+ ctx.body = { ok: false };
867
+ } catch (err) {
868
+ console.log("Error for forgot password ", err);
869
+ ctx.body = { ok: false };
870
+ }
669
871
  }
670
872
  });
671
873
  const notificationChannels = ({ strapi }) => ({
@@ -1066,12 +1268,40 @@ const routes = {
1066
1268
  policies: [],
1067
1269
  auth: false
1068
1270
  }
1271
+ },
1272
+ {
1273
+ method: "POST",
1274
+ path: "/signup",
1275
+ handler: "userDetails.signUp",
1276
+ config: {
1277
+ policies: [],
1278
+ auth: false
1279
+ }
1280
+ },
1281
+ {
1282
+ method: "POST",
1283
+ path: "/login",
1284
+ handler: "userDetails.signIn",
1285
+ config: {
1286
+ policies: [],
1287
+ auth: false
1288
+ }
1289
+ },
1290
+ {
1291
+ method: "POST",
1292
+ path: "/forgot-password",
1293
+ handler: "userDetails.forgotPassword",
1294
+ config: {
1295
+ policies: [],
1296
+ auth: false
1297
+ }
1069
1298
  }
1070
1299
  ]
1071
1300
  }
1072
1301
  };
1073
1302
  const services = {
1074
- service
1303
+ service,
1304
+ userDetailsService
1075
1305
  };
1076
1306
  const index = {
1077
1307
  register,
@@ -67,6 +67,17 @@ declare const _default: {
67
67
  strapi: import('@strapi/types/dist/core').Strapi;
68
68
  }) => {
69
69
  getUserDetails(ctx: any): Promise<void>;
70
+ signUp(ctx: any): Promise<{
71
+ ok: boolean;
72
+ message: any;
73
+ }>;
74
+ signIn(ctx: any): Promise<{
75
+ ok: boolean;
76
+ }>;
77
+ forgotPassword(ctx: any): Promise<{
78
+ ok: boolean;
79
+ message: any;
80
+ }>;
70
81
  };
71
82
  notificationChannels: ({ strapi }: {
72
83
  strapi: import('@strapi/types/dist/core').Strapi;
@@ -123,7 +134,14 @@ declare const _default: {
123
134
  }>): Promise<void>;
124
135
  };
125
136
  getToken(): Promise<string>;
126
- makeBackendRequest(endpoint: string, options: RequestInit, forValidation?: boolean): Promise<unknown>;
137
+ makeBackendRequest(endpoint: string, options: RequestInit, forValidation?: boolean, sessionToken?: string): Promise<unknown>;
138
+ };
139
+ userDetailsService: ({ strapi }: {
140
+ strapi: import('@strapi/types/dist/core').Strapi;
141
+ }) => {
142
+ createUserApiToken(sessionToken: string): Promise<any>;
143
+ getUserApiToken(sessionToken: string): Promise<string>;
144
+ createInitialMonitor(site_url: string, apiToken: string): Promise<any>;
127
145
  };
128
146
  };
129
147
  contentTypes: {};
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.0.7",
2
+ "version": "1.0.8",
3
3
  "repository": {
4
4
  "type": "git",
5
5
  "url": "https://github.com/Appfoster/upsnap-strapi"