@mindful-web/marko-web-identity-x 1.0.0

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.
Files changed (138) hide show
  1. package/.eslintignore +1 -0
  2. package/LICENSE +21 -0
  3. package/README.md +200 -0
  4. package/api/fragment-types.json +1 -0
  5. package/api/fragments/active-user.js +108 -0
  6. package/api/mutations/add-external-user-id.js +24 -0
  7. package/api/mutations/create-app-user.js +11 -0
  8. package/api/mutations/delete-app-user-for-current-application.js +7 -0
  9. package/api/mutations/impersonate-app-user.js +12 -0
  10. package/api/mutations/logout-app-user.js +11 -0
  11. package/api/mutations/send-change-email-link.js +7 -0
  12. package/api/mutations/send-login-link.js +7 -0
  13. package/api/mutations/set-custom-attributes.js +15 -0
  14. package/api/queries/check-content-access.js +19 -0
  15. package/api/queries/get-active-context.js +45 -0
  16. package/api/queries/load-user-by-external-id.js +14 -0
  17. package/api/queries/load-user-by-id.js +11 -0
  18. package/api/queries/load-user.js +11 -0
  19. package/browser/.eslintrc.js +11 -0
  20. package/browser/access.vue +377 -0
  21. package/browser/authenticate.vue +278 -0
  22. package/browser/change-email-confirm.vue +120 -0
  23. package/browser/change-email-init.vue +179 -0
  24. package/browser/comments/create.vue +107 -0
  25. package/browser/comments/post.vue +151 -0
  26. package/browser/comments/stream.vue +319 -0
  27. package/browser/custom-column.vue +314 -0
  28. package/browser/dayjs.js +6 -0
  29. package/browser/download-related.vue +120 -0
  30. package/browser/download.vue +380 -0
  31. package/browser/errors/authentication.js +7 -0
  32. package/browser/errors/feature.js +7 -0
  33. package/browser/errors/form.js +7 -0
  34. package/browser/errors/logout.js +7 -0
  35. package/browser/form/address-block.vue +131 -0
  36. package/browser/form/common/checkbox-group.vue +140 -0
  37. package/browser/form/common/form-group.vue +24 -0
  38. package/browser/form/common/form-label.vue +17 -0
  39. package/browser/form/consent.vue +121 -0
  40. package/browser/form/fields/address-extra.vue +68 -0
  41. package/browser/form/fields/city.vue +68 -0
  42. package/browser/form/fields/comment-body.vue +63 -0
  43. package/browser/form/fields/country.vue +94 -0
  44. package/browser/form/fields/custom-boolean.vue +58 -0
  45. package/browser/form/fields/custom-select-group.vue +25 -0
  46. package/browser/form/fields/custom-select-multiple.vue +97 -0
  47. package/browser/form/fields/custom-select-write-in-inline.vue +39 -0
  48. package/browser/form/fields/custom-select-write-in.vue +66 -0
  49. package/browser/form/fields/custom-select.vue +191 -0
  50. package/browser/form/fields/custom-text.vue +62 -0
  51. package/browser/form/fields/display-name.vue +64 -0
  52. package/browser/form/fields/email.vue +62 -0
  53. package/browser/form/fields/family-name.vue +68 -0
  54. package/browser/form/fields/given-name.vue +68 -0
  55. package/browser/form/fields/organization-title.vue +68 -0
  56. package/browser/form/fields/organization.vue +68 -0
  57. package/browser/form/fields/phone-number.vue +68 -0
  58. package/browser/form/fields/postal-code.vue +68 -0
  59. package/browser/form/fields/receive-email.vue +58 -0
  60. package/browser/form/fields/region.vue +106 -0
  61. package/browser/form/fields/regional-policy.vue +59 -0
  62. package/browser/form/fields/street.vue +68 -0
  63. package/browser/graphql/create-client.js +65 -0
  64. package/browser/graphql/parse.js +1 -0
  65. package/browser/graphql/queries/download-related-content.js +38 -0
  66. package/browser/index.js +110 -0
  67. package/browser/login-fields.vue +214 -0
  68. package/browser/login.vue +331 -0
  69. package/browser/logout.vue +68 -0
  70. package/browser/mixins/global-auto-signup-event-emitter.js +18 -0
  71. package/browser/mixins/global-event-emitter.js +28 -0
  72. package/browser/profile.vue +688 -0
  73. package/browser/service.js +32 -0
  74. package/browser/utils/clean-path.js +1 -0
  75. package/browser/utils/cookies-enabled.js +10 -0
  76. package/browser/utils/get-as-array.js +6 -0
  77. package/browser/utils/get-referring-page.js +7 -0
  78. package/browser/utils/get.js +8 -0
  79. package/browser/utils/post.js +10 -0
  80. package/browser/utils/redirect.js +7 -0
  81. package/browser/utils/region-country-codes.js +1 -0
  82. package/components/access.marko +24 -0
  83. package/components/access.marko.js +61 -0
  84. package/components/comment-stream.marko +31 -0
  85. package/components/comment-stream.marko.js +71 -0
  86. package/components/context.marko +15 -0
  87. package/components/context.marko.js +52 -0
  88. package/components/form-access.marko +60 -0
  89. package/components/form-access.marko.js +111 -0
  90. package/components/form-authenticate.marko +32 -0
  91. package/components/form-authenticate.marko.js +73 -0
  92. package/components/form-change-email.marko +27 -0
  93. package/components/form-change-email.marko.js +68 -0
  94. package/components/form-login.marko +32 -0
  95. package/components/form-login.marko.js +72 -0
  96. package/components/form-logout.marko +7 -0
  97. package/components/form-logout.marko.js +36 -0
  98. package/components/form-profile.marko +38 -0
  99. package/components/form-profile.marko.js +78 -0
  100. package/components/form-register.marko +2 -0
  101. package/components/form-register.marko.js +32 -0
  102. package/components/identify.marko +17 -0
  103. package/components/identify.marko.js +59 -0
  104. package/components/marko.json +90 -0
  105. package/components/non-auth-identify.marko +15 -0
  106. package/components/non-auth-identify.marko.js +53 -0
  107. package/config.js +161 -0
  108. package/hooks.js +20 -0
  109. package/index.js +30 -0
  110. package/marko.json +6 -0
  111. package/middleware/content-access-state.js +31 -0
  112. package/middleware/content-download-state.js +27 -0
  113. package/middleware.js +32 -0
  114. package/package.json +40 -0
  115. package/routes/access.js +29 -0
  116. package/routes/authenticate.js +58 -0
  117. package/routes/change-email-confirm.js +60 -0
  118. package/routes/change-email-init.js +16 -0
  119. package/routes/comment-count.js +19 -0
  120. package/routes/comments.js +43 -0
  121. package/routes/countries.js +19 -0
  122. package/routes/create-comment.js +25 -0
  123. package/routes/download.js +29 -0
  124. package/routes/flag-comment.js +21 -0
  125. package/routes/index.js +40 -0
  126. package/routes/login-fields.js +35 -0
  127. package/routes/login.js +95 -0
  128. package/routes/logout.js +19 -0
  129. package/routes/profile.js +183 -0
  130. package/routes/regions.js +22 -0
  131. package/service.js +483 -0
  132. package/utils/call-hooks-for.js +28 -0
  133. package/utils/context-cookie.js +30 -0
  134. package/utils/create-client.js +48 -0
  135. package/utils/create-identity-builder.js +33 -0
  136. package/utils/get-as-array.js +6 -0
  137. package/utils/json-error-handler.js +41 -0
  138. package/utils/token-cookie.js +30 -0
@@ -0,0 +1,377 @@
1
+ <template>
2
+ <div id="access-idx-form" class="content-page-gate p-block">
3
+ <template v-if="hasActiveUser">
4
+ <h5 class="content-page-gate__title">
5
+ {{ title }}
6
+ </h5>
7
+ <p v-if="!didSubmit" v-html="callToAction" />
8
+ <form v-if="!didSubmit" @submit.prevent="handleSubmit">
9
+ <fieldset :disabled="isLoading">
10
+ <div v-for="(row, ridx) in fieldRows" :key="ridx" class="row">
11
+ <custom-column
12
+ v-for="(col, cidx) in row"
13
+ :key="`${ridx}_${cidx}`"
14
+ :label="col.label || defaultFieldLabels[col.key]"
15
+ :field-key="col.key"
16
+ :field-id="col.id"
17
+ :type="col.type"
18
+ :required="col.required"
19
+ :width="col.width || 1"
20
+ :user="user"
21
+ :endpoints="endpoints"
22
+ :enable-change-email="enableChangeEmail"
23
+ />
24
+ </div>
25
+
26
+ <form-consent
27
+ :user="user"
28
+ :consent-policy="consentPolicy"
29
+ :consent-policy-enabled="consentPolicyEnabled"
30
+ :email-consent-request="emailConsentRequest"
31
+ :email-consent-request-enabled="emailConsentRequestEnabled"
32
+ :regional-consent-policies="regionalConsentPolicies"
33
+ :country-code="countryCode"
34
+ />
35
+
36
+ <div class="d-flex align-items-center">
37
+ <button type="submit" class="btn btn-primary">
38
+ {{ buttonLabel }}
39
+ </button>
40
+ </div>
41
+ </fieldset>
42
+ <p v-if="error" class="mt-3 text-danger">
43
+ An error occurred: {{ error }}
44
+ </p>
45
+ </form>
46
+
47
+ <template v-else>
48
+ <div class="success-message">
49
+ <p class="success-message__title">
50
+ Your responses have been saved, and this page will automatically reload.
51
+ </p>
52
+ <p class="success-message__title d-flex justify-content-between">
53
+ If not, click the reload button
54
+ <a
55
+ class="btn btn-primary mr-3"
56
+ :href="handleReload()"
57
+ >Reload</a>
58
+ </p>
59
+ </div>
60
+ </template>
61
+ </template>
62
+
63
+ <div v-else>
64
+ <p v-html="callToActionLoggedOut" />
65
+ <login
66
+ :additional-event-data="additionalEventData"
67
+ :source="loginSource"
68
+ :redirect="redirect"
69
+ :endpoints="endpoints"
70
+ :app-context-id="appContextId"
71
+ :consent-policy="consentPolicy"
72
+ :consent-policy-enabled="consentPolicyEnabled"
73
+ :email-consent-request="emailConsentRequest"
74
+ :email-consent-request-enabled="emailConsentRequestEnabled"
75
+ :regional-consent-policies="regionalConsentPolicies"
76
+ :required-create-fields="requiredCreateFields"
77
+ :default-field-labels="defaultFieldLabels"
78
+ />
79
+ </div>
80
+ </div>
81
+ </template>
82
+
83
+ <script>
84
+ import post from './utils/post';
85
+ import cookiesEnabled from './utils/cookies-enabled';
86
+
87
+ import FormConsent from './form/consent.vue';
88
+ import CustomColumn from './custom-column.vue';
89
+ import Login from './login.vue';
90
+
91
+ import FeatureError from './errors/feature';
92
+ import FormError from './errors/form';
93
+ import EventEmitter from './mixins/global-event-emitter';
94
+ // import DownloadRelated from './download-related.vue';
95
+
96
+ export default {
97
+ components: {
98
+ CustomColumn,
99
+ // DownloadRelated,
100
+ FormConsent,
101
+ Login,
102
+ },
103
+
104
+ /**
105
+ *
106
+ */
107
+ mixins: [EventEmitter],
108
+
109
+ /**
110
+ *
111
+ */
112
+ props: {
113
+ /**
114
+ * custom form props
115
+ */
116
+ content: {
117
+ type: Object,
118
+ required: true,
119
+ },
120
+ fieldRows: {
121
+ type: Array,
122
+ required: true,
123
+ },
124
+ title: {
125
+ type: String,
126
+ default: 'Complete the form to access this content',
127
+ },
128
+ callToAction: {
129
+ type: String,
130
+ default: 'To access this content, please fill out the form below.',
131
+ },
132
+ callToActionLoggedOut: {
133
+ type: String,
134
+ default: 'To access this content, please enter your email address below.',
135
+ },
136
+
137
+ /**
138
+ * profile/login props
139
+ */
140
+ loginSource: {
141
+ type: String,
142
+ default: 'contentAccess',
143
+ },
144
+ endpoints: {
145
+ type: Object,
146
+ required: true,
147
+ },
148
+ activeUser: {
149
+ type: Object,
150
+ default: () => ({}),
151
+ },
152
+ requiredCreateFields: {
153
+ type: Array,
154
+ default: () => [],
155
+ },
156
+ cookie: {
157
+ type: Object,
158
+ required: true,
159
+ },
160
+ defaultFieldLabels: {
161
+ type: Object,
162
+ default: () => {},
163
+ },
164
+ consentPolicy: {
165
+ type: String,
166
+ default: null,
167
+ },
168
+ consentPolicyEnabled: {
169
+ type: Boolean,
170
+ default: false,
171
+ },
172
+ emailConsentRequest: {
173
+ type: String,
174
+ default: null,
175
+ },
176
+ emailConsentRequestEnabled: {
177
+ type: Boolean,
178
+ default: false,
179
+ },
180
+ appContextId: {
181
+ type: String,
182
+ default: null,
183
+ },
184
+ buttonLabel: {
185
+ type: String,
186
+ default: 'Submit & Access',
187
+ },
188
+ regionalConsentPolicies: {
189
+ type: Array,
190
+ default: () => [],
191
+ },
192
+ defaultCountryCode: {
193
+ type: String,
194
+ default: null,
195
+ },
196
+ enableChangeEmail: {
197
+ type: Boolean,
198
+ default: false,
199
+ },
200
+ updateProfileOnSubmit: {
201
+ type: Boolean,
202
+ default: true,
203
+ },
204
+ },
205
+
206
+ /**
207
+ *
208
+ */
209
+ data() {
210
+ return {
211
+ error: null,
212
+ isLoading: false,
213
+ isReloadingPage: false,
214
+ isRedirectingPage: false,
215
+ didSubmit: false,
216
+ user: {
217
+ ...this.activeUser,
218
+ ...(this.defaultCountryCode
219
+ && !this.activeUser.countryCode
220
+ && { countryCode: this.defaultCountryCode }
221
+ ),
222
+ },
223
+ };
224
+ },
225
+
226
+ /**
227
+ *
228
+ */
229
+ computed: {
230
+ /**
231
+ *
232
+ */
233
+ hasActiveUser() {
234
+ return this.user && this.user.email;
235
+ },
236
+
237
+ /**
238
+ *
239
+ */
240
+ redirect() {
241
+ if (this.content.siteContext && this.content.siteContext.url) {
242
+ return this.content.siteContext.url;
243
+ }
244
+ if (this.content) {
245
+ return `/${this.content.id}`;
246
+ }
247
+ return '/';
248
+ },
249
+
250
+ /**
251
+ *
252
+ */
253
+ canUpdateProfile() {
254
+ return this.hasActiveUser && this.updateProfileOnSubmit;
255
+ },
256
+
257
+ /**
258
+ *
259
+ */
260
+ countryCode() {
261
+ if (this.user && this.user.countryCode) return this.user.countryCode;
262
+ return this.defaultCountryCode;
263
+ },
264
+ },
265
+
266
+ /**
267
+ *
268
+ */
269
+ mounted() {
270
+ if (cookiesEnabled()) {
271
+ this.emit('access-mounted');
272
+ } else {
273
+ const error = new FeatureError('Your browser does not support cookies. Please enable cookies to use this feature.');
274
+ this.error = error.message;
275
+ this.emit('access-errored', { message: error.message });
276
+ }
277
+ },
278
+
279
+ /**
280
+ *
281
+ */
282
+ methods: {
283
+ async handleReload() {
284
+ this.isReloadingPage = true;
285
+ window.location.reload(true);
286
+ },
287
+
288
+ /**
289
+ *
290
+ */
291
+ async handleSubmit({ withReload = true }) {
292
+ this.error = null;
293
+ this.isLoading = true;
294
+ this.didSubmit = false;
295
+ try {
296
+ const additionalEventData = { ...this.additionalEventData, actionSource: this.loginSource };
297
+ let data = {};
298
+ if (this.canUpdateProfile) {
299
+ const res = await post('/profile', { ...this.user, additionalEventData });
300
+ data = await res.json();
301
+ if (!res.ok) throw new FormError(data.message, res.status);
302
+ this.user = data.user;
303
+ }
304
+
305
+ // Perform and notify about the download
306
+ const eventData = { ...additionalEventData, ...(data.additionalEventData || {}) };
307
+ await this.access(this.content, eventData);
308
+
309
+ this.didSubmit = true;
310
+
311
+ if (withReload) {
312
+ this.handleReload();
313
+ }
314
+ } catch (e) {
315
+ this.error = e;
316
+ this.emit('access-errored', { message: e.message });
317
+ } finally {
318
+ this.isLoading = false;
319
+ }
320
+ },
321
+ /**
322
+ *
323
+ */
324
+ async access(content, additionalEventData) {
325
+ try {
326
+ const res = await post('/access', {
327
+ contentId: content.id,
328
+ contentType: content.type,
329
+ cookie: this.cookie,
330
+ // companyId: company.id,
331
+ userId: this.user.id,
332
+ additionalEventData,
333
+ // Flatten the payload for storage
334
+ payload: this.fieldRows.reduce((objs, row) => {
335
+ const { fields, selects, booleans } = objs;
336
+ row.forEach((col) => {
337
+ const { id, type, label } = col;
338
+ if (type === 'custom-select') {
339
+ const value = this.user.customSelectFieldAnswers.find((ans) => ans.id === id);
340
+ const values = (value.answers || []).map((answer) => {
341
+ const opts = value.field.options.reduce((arr, opt) => ([
342
+ ...arr, opt, ...(opt.options ? opt.options : []),
343
+ ]), []);
344
+ const ans = opts.find((opt) => opt.id === answer.id);
345
+ return { id: answer.id, label: ans.label, writeIn: answer.writeInValue };
346
+ });
347
+ selects[id] = { label, values };
348
+ } else if (type === 'custom-boolean') {
349
+ const value = this.user.customBooleanFieldAnswers.find((ans) => ans.id === id);
350
+ booleans[id] = { label, value };
351
+ } else {
352
+ const value = this.user[col.key];
353
+ fields[col.key] = { label, value };
354
+ }
355
+ });
356
+ return { fields, selects, booleans };
357
+ }, { fields: {}, selects: {}, booleans: {} }),
358
+ });
359
+ const data = await res.json();
360
+ if (!res.ok) throw new FormError(data.message, res.status);
361
+
362
+ this.emit('access-submitted', {
363
+ contentId: content.id,
364
+ contentType: content.type,
365
+ userId: this.user.id,
366
+ additionalEventData,
367
+ }, data.entity);
368
+ } catch (e) {
369
+ this.error = e;
370
+ this.emit('access-errored', { message: e.message });
371
+ } finally {
372
+ this.isLoading = false;
373
+ }
374
+ },
375
+ },
376
+ };
377
+ </script>
@@ -0,0 +1,278 @@
1
+ <template>
2
+ <div v-if="isRedirecting">
3
+ <p>You've successfully logged in. Redirecting you...</p>
4
+ </div>
5
+ <div v-else-if="isLoading">
6
+ <p>Logging in...</p>
7
+ </div>
8
+ <div v-else-if="showProfileForm">
9
+ <profile-form
10
+ :additional-event-data="additionalEventData"
11
+ :endpoints="endpoints"
12
+ :active-user="activeUser"
13
+ :required-server-fields="requiredServerFields"
14
+ :required-client-fields="requiredClientFields"
15
+ :active-custom-field-ids="activeCustomFieldIds"
16
+ :hidden-fields="hiddenFields"
17
+ :default-country-code="defaultCountryCode"
18
+ :boolean-questions-label="booleanQuestionsLabel"
19
+ :consent-policy="consentPolicy"
20
+ :consent-policy-enabled="consentPolicyEnabled"
21
+ :email-consent-request="emailConsentRequest"
22
+ :email-consent-request-enabled="emailConsentRequestEnabled"
23
+ :regional-consent-policies="regionalConsentPolicies"
24
+ :button-label="buttonLabel"
25
+ :call-to-action="callToAction"
26
+ :default-field-labels="defaultFieldLabels"
27
+ :enable-change-email="enableChangeEmail"
28
+ @profile-updated="redirect"
29
+ />
30
+ </div>
31
+ <div v-else-if="error" class="alert alert-danger" role="alert">
32
+ <h5 class="alert-heading">
33
+ Unable to Login
34
+ </h5>
35
+ <p>{{ error.message }}</p>
36
+ <hr>
37
+ <p class="mb-0">
38
+ Please try <a :href="endpoints.login" class="alert-link">logging in</a> again.
39
+ </p>
40
+ </div>
41
+ </template>
42
+
43
+ <script>
44
+ import redirect from './utils/redirect';
45
+ import cookiesEnabled from './utils/cookies-enabled';
46
+ import post from './utils/post';
47
+ import ProfileForm from './profile.vue';
48
+ import AuthenticationError from './errors/authentication';
49
+ import FeatureError from './errors/feature';
50
+ import AutoSignupEventEmitter from './mixins/global-auto-signup-event-emitter';
51
+ import EventEmitter from './mixins/global-event-emitter';
52
+
53
+ const isEmpty = (v) => v == null || v === '';
54
+
55
+ export default {
56
+ /**
57
+ *
58
+ */
59
+ components: { ProfileForm },
60
+
61
+ /**
62
+ *
63
+ */
64
+ mixins: [EventEmitter, AutoSignupEventEmitter],
65
+
66
+ /**
67
+ *
68
+ */
69
+ props: {
70
+ token: {
71
+ type: String,
72
+ required: true,
73
+ },
74
+ endpoints: {
75
+ type: Object,
76
+ required: true,
77
+ },
78
+ redirectTo: {
79
+ type: String,
80
+ default: '/',
81
+ },
82
+ hiddenFields: {
83
+ type: Array,
84
+ default: () => [],
85
+ },
86
+ requiredServerFields: {
87
+ type: Array,
88
+ default: () => [],
89
+ },
90
+ requiredClientFields: {
91
+ type: Array,
92
+ default: () => [],
93
+ },
94
+ activeCustomFieldIds: {
95
+ type: Array,
96
+ default: () => [],
97
+ },
98
+ consentPolicy: {
99
+ type: String,
100
+ default: null,
101
+ },
102
+ consentPolicyEnabled: {
103
+ type: Boolean,
104
+ default: false,
105
+ },
106
+ emailConsentRequest: {
107
+ type: String,
108
+ default: null,
109
+ },
110
+ emailConsentRequestEnabled: {
111
+ type: Boolean,
112
+ default: false,
113
+ },
114
+ regionalConsentPolicies: {
115
+ type: Array,
116
+ default: () => [],
117
+ },
118
+ callToAction: {
119
+ type: String,
120
+ default: null,
121
+ },
122
+ buttonLabel: {
123
+ type: String,
124
+ default: 'Submit',
125
+ },
126
+ defaultCountryCode: {
127
+ type: String,
128
+ default: null,
129
+ },
130
+ defaultFieldLabels: {
131
+ type: Object,
132
+ default: () => ({}),
133
+ },
134
+ booleanQuestionsLabel: {
135
+ type: String,
136
+ default: null,
137
+ },
138
+ enableChangeEmail: {
139
+ type: Boolean,
140
+ default: false,
141
+ },
142
+ },
143
+
144
+ /**
145
+ *
146
+ */
147
+ data: () => ({
148
+ bypassProfileForm: false,
149
+ error: null,
150
+ isLoading: false,
151
+ isRedirecting: false,
152
+ isProfileComplete: true,
153
+ activeUser: null,
154
+ requiresCustomFieldAnswers: false,
155
+ mustReVerifyProfile: false,
156
+ }),
157
+
158
+ /**
159
+ *
160
+ */
161
+ computed: {
162
+ /**
163
+ *
164
+ */
165
+ requiredFields() {
166
+ return [...this.requiredServerFields, ...this.requiredClientFields];
167
+ },
168
+
169
+ /**
170
+ *
171
+ */
172
+ formHasRequiredFields() {
173
+ return Boolean(this.requiredFields.length);
174
+ },
175
+
176
+ isUserRedirect() {
177
+ const { redirectTo } = this;
178
+ const { login, register } = this.endpoints;
179
+ if (redirectTo.indexOf(login) === 0) return true;
180
+ if (redirectTo.indexOf(register) === 0) return true;
181
+ return false;
182
+ },
183
+
184
+ /**
185
+ *
186
+ */
187
+ showProfileForm() {
188
+ if (this.bypassProfileForm) return false;
189
+ if (this.mustReVerifyProfile) return true;
190
+ if (this.requiresCustomFieldAnswers) return true;
191
+ return !this.isProfileComplete;
192
+ },
193
+ },
194
+
195
+ /**
196
+ *
197
+ */
198
+ mounted() {
199
+ if (cookiesEnabled()) {
200
+ this.emit('authenticate-mounted');
201
+ this.authenticate();
202
+ } else {
203
+ this.error = new FeatureError('Your browser does not support cookies. Please enable cookies to use this feature.');
204
+ this.emit('authenticate-errored', { message: this.error.message });
205
+ }
206
+ },
207
+
208
+ /**
209
+ *
210
+ */
211
+ methods: {
212
+ /**
213
+ *
214
+ */
215
+ async authenticate() {
216
+ this.isLoading = true;
217
+ try {
218
+ const { token, additionalEventData, activeCustomFieldIds: ids } = this;
219
+ if (!token) throw new Error('No login token was provided.');
220
+
221
+ const res = await post('/authenticate', { token, additionalEventData });
222
+ const data = await res.json();
223
+ if (!res.ok) throw new AuthenticationError(data.message, res.status);
224
+
225
+ this.activeUser = data.user;
226
+ this.mustReVerifyProfile = data.user.mustReVerifyProfile;
227
+ const customAnswerIds = [
228
+ ...data.user.customBooleanFieldAnswers,
229
+ ...data.user.customSelectFieldAnswers,
230
+ ].filter((field) => field.hasAnswered).map((field) => field.id);
231
+
232
+ this.isProfileComplete = this.requiredFields
233
+ .every((key) => customAnswerIds.includes(key) || !isEmpty(this.activeUser[key]));
234
+
235
+ this.requiresCustomFieldAnswers = this.activeUser.customSelectFieldAnswers
236
+ .filter(!ids.length ? ({ field }) => ids.includes(field.id) : () => true)
237
+ .some(({ hasAnswered, field }) => field.required && !hasAnswered);
238
+ this.bypassProfileForm = (data.loginSource === 'contentDownload' || data.loginSource === 'contentAccess');
239
+
240
+ this.emitAutoSignup(data);
241
+ this.emit('authenticated', {
242
+ ...data,
243
+ id: this.activeUser.id,
244
+ email: this.activeUser.email,
245
+ verifiedCount: this.activeUser.verifiedCount,
246
+ mustReVerifyProfile: this.mustReVerifyProfile,
247
+ isProfileComplete: this.isProfileComplete,
248
+ requiresCustomFieldAnswers: this.requiresCustomFieldAnswers,
249
+ actionSource: data.loginSource,
250
+ additionalEventData: {
251
+ ...(this.additionalEventData || {}),
252
+ ...(data.additionalEventData || {}),
253
+ },
254
+ }, data.entity);
255
+
256
+ if (!this.showProfileForm) this.redirect();
257
+ } catch (e) {
258
+ if (/no token was found/i.test(e.message)) {
259
+ e.message = 'This login link has either expired or was already used.';
260
+ }
261
+ this.error = e;
262
+ this.emit('authenticate-errored', { message: e.message });
263
+ } finally {
264
+ this.isLoading = false;
265
+ }
266
+ },
267
+
268
+ /**
269
+ *
270
+ */
271
+ redirect() {
272
+ this.isRedirecting = true;
273
+ const redirectTo = this.isUserRedirect ? '/' : this.redirectTo;
274
+ redirect(redirectTo);
275
+ },
276
+ },
277
+ };
278
+ </script>