@redacto.io/consent-sdk-react 3.5.0 → 4.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.
- package/.turbo/turbo-build.log +21 -0
- package/CHANGELOG.md +6 -0
- package/dist/index.d.mts +49 -2
- package/dist/index.d.ts +49 -2
- package/dist/index.js +1785 -243
- package/dist/index.mjs +1784 -243
- package/package.json +1 -1
- package/src/RedactoNoticeConsent/RedactoNoticeConsent.test.tsx +201 -118
- package/src/RedactoNoticeConsent/RedactoNoticeConsent.tsx +1192 -93
- package/src/RedactoNoticeConsent/api/index.ts +333 -206
- package/src/RedactoNoticeConsent/api/types.ts +138 -16
- package/src/RedactoNoticeConsent/components/DigiLockerCallback.tsx +536 -0
- package/src/RedactoNoticeConsent/components/index.ts +7 -0
- package/src/RedactoNoticeConsent/injectStyles.ts +11 -0
- package/src/RedactoNoticeConsent/services/storage.ts +122 -0
- package/src/RedactoNoticeConsent/types.ts +2 -0
- package/src/index.ts +10 -0
|
@@ -4,16 +4,17 @@ import type {
|
|
|
4
4
|
ConsentEventPayload,
|
|
5
5
|
FetchConsentContentParams,
|
|
6
6
|
FetchTTSAudioUrlsParams,
|
|
7
|
-
|
|
7
|
+
InitiateGuardianVerificationParams,
|
|
8
|
+
InitiateGuardianVerificationResponse,
|
|
8
9
|
RedactoJwtPayload,
|
|
9
10
|
SubmitConsentEventParams,
|
|
10
|
-
SubmitGuardianInfoParams,
|
|
11
11
|
TTSAudioUrlsResponse,
|
|
12
|
+
VerifyGuardianStatusParams,
|
|
13
|
+
VerifyGuardianStatusResponse,
|
|
12
14
|
} from "./types";
|
|
13
15
|
|
|
14
16
|
const BASE_URL = "https://api.redacto.io/consent";
|
|
15
17
|
|
|
16
|
-
|
|
17
18
|
// Simple in-memory cache for API responses to reduce calls
|
|
18
19
|
const apiCache = new Map<string, { data: any; timestamp: number; ttl: number }>();
|
|
19
20
|
const CACHE_TTL = 5 * 60 * 1000; // 5 minutes
|
|
@@ -58,90 +59,82 @@ export const fetchConsentContent = async ({
|
|
|
58
59
|
specific_uuid,
|
|
59
60
|
validate_against = "all",
|
|
60
61
|
include_fully_consented_data = false,
|
|
61
|
-
signal,
|
|
62
|
+
signal,
|
|
62
63
|
}: FetchConsentContentParams & { signal?: AbortSignal }): Promise<ConsentContent> => {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
throw new Error("noticeId and accessToken are required");
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const decodedToken = decodeTokenSafely(accessToken);
|
|
70
|
-
if (!decodedToken) {
|
|
71
|
-
throw new Error("Invalid access token");
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
const { organisation_uuid: ORGANISATION_UUID, workspace_uuid: WORKSPACE_UUID } = decodedToken;
|
|
75
|
-
if (!ORGANISATION_UUID || !WORKSPACE_UUID) {
|
|
76
|
-
throw new Error("Invalid token: missing organization or workspace UUID");
|
|
77
|
-
}
|
|
64
|
+
if (!noticeId || !accessToken || !validate_against) {
|
|
65
|
+
throw new Error("noticeId and accessToken are required");
|
|
66
|
+
}
|
|
78
67
|
|
|
79
|
-
|
|
68
|
+
const decodedToken = decodeTokenSafely(accessToken);
|
|
69
|
+
if (!decodedToken) {
|
|
70
|
+
throw new Error("Invalid access token");
|
|
71
|
+
}
|
|
80
72
|
|
|
81
|
-
|
|
82
|
-
|
|
73
|
+
const { organisation_uuid: ORGANISATION_UUID, workspace_uuid: WORKSPACE_UUID } = decodedToken;
|
|
74
|
+
if (!ORGANISATION_UUID || !WORKSPACE_UUID) {
|
|
75
|
+
throw new Error("Invalid token: missing organization or workspace UUID");
|
|
76
|
+
}
|
|
83
77
|
|
|
84
|
-
|
|
85
|
-
if (validate_against === "all" && !specific_uuid) {
|
|
86
|
-
const cachedData = getCachedData(cacheKey);
|
|
87
|
-
if (cachedData) {
|
|
88
|
-
return cachedData;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
78
|
+
const apiBaseUrl = baseUrl || BASE_URL;
|
|
91
79
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
);
|
|
80
|
+
// Create cache key for this request - include accessToken to ensure different users get different cache
|
|
81
|
+
const cacheKey = `${accessToken}-${noticeId}-${validate_against}-${language}-${specific_uuid || ''}-${include_fully_consented_data}`;
|
|
95
82
|
|
|
96
|
-
|
|
97
|
-
|
|
83
|
+
if (validate_against === "all" && !specific_uuid) {
|
|
84
|
+
const cachedData = getCachedData(cacheKey);
|
|
85
|
+
if (cachedData) {
|
|
86
|
+
return cachedData;
|
|
98
87
|
}
|
|
99
|
-
|
|
100
|
-
if (include_fully_consented_data) {
|
|
101
|
-
url.searchParams.append("include_fully_consented_data", "true");
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
const response = await fetch(url.toString(), {
|
|
105
|
-
method: "GET",
|
|
106
|
-
headers: {
|
|
107
|
-
Authorization: `Bearer ${accessToken}`,
|
|
108
|
-
"Accept-Language": language,
|
|
109
|
-
"Content-Type": "application/json",
|
|
110
|
-
},
|
|
111
|
-
signal, // Add abort signal for request cancellation
|
|
112
|
-
});
|
|
88
|
+
}
|
|
113
89
|
|
|
114
|
-
|
|
115
|
-
|
|
90
|
+
const url = new URL(
|
|
91
|
+
`${apiBaseUrl}/public/organisations/${ORGANISATION_UUID}/workspaces/${WORKSPACE_UUID}/notices/${noticeId}`
|
|
92
|
+
);
|
|
116
93
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
errorMessage = "User has already provided consent";
|
|
125
|
-
} else if (response.status >= 500) {
|
|
126
|
-
errorMessage = "Server error: Please try again later";
|
|
127
|
-
}
|
|
94
|
+
if (specific_uuid) {
|
|
95
|
+
url.searchParams.append("specific_uuid", specific_uuid);
|
|
96
|
+
}
|
|
97
|
+
url.searchParams.append("validate_against", validate_against);
|
|
98
|
+
if (include_fully_consented_data) {
|
|
99
|
+
url.searchParams.append("include_fully_consented_data", "true");
|
|
100
|
+
}
|
|
128
101
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
102
|
+
const response = await fetch(url.toString(), {
|
|
103
|
+
method: "GET",
|
|
104
|
+
headers: {
|
|
105
|
+
Authorization: `Bearer ${accessToken}`,
|
|
106
|
+
"Accept-Language": language,
|
|
107
|
+
},
|
|
108
|
+
signal,
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
if (!response.ok) {
|
|
112
|
+
let errorMessage = `Failed to fetch consent content: ${response.status} ${response.statusText}`;
|
|
113
|
+
|
|
114
|
+
if (response.status === 401) {
|
|
115
|
+
errorMessage = "Unauthorized: Invalid or expired token";
|
|
116
|
+
} else if (response.status === 403) {
|
|
117
|
+
errorMessage = "Forbidden: Access denied";
|
|
118
|
+
} else if (response.status === 404) {
|
|
119
|
+
errorMessage = "Notice not found";
|
|
120
|
+
} else if (response.status === 409) {
|
|
121
|
+
errorMessage = "User has already provided consent";
|
|
122
|
+
} else if (response.status >= 500) {
|
|
123
|
+
errorMessage = "Server error: Please try again later";
|
|
132
124
|
}
|
|
133
125
|
|
|
134
|
-
const
|
|
126
|
+
const error = new Error(errorMessage) as Error & { status?: number };
|
|
127
|
+
error.status = response.status;
|
|
128
|
+
throw error;
|
|
129
|
+
}
|
|
135
130
|
|
|
136
|
-
|
|
137
|
-
if (validate_against === "all" && !specific_uuid) {
|
|
138
|
-
setCachedData(cacheKey, data);
|
|
139
|
-
}
|
|
131
|
+
const data: ConsentContent = await response.json();
|
|
140
132
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
throw error;
|
|
133
|
+
if (validate_against === "all" && !specific_uuid) {
|
|
134
|
+
setCachedData(cacheKey, data);
|
|
144
135
|
}
|
|
136
|
+
|
|
137
|
+
return data;
|
|
145
138
|
};
|
|
146
139
|
|
|
147
140
|
export const submitConsentEvent = async ({
|
|
@@ -151,120 +144,190 @@ export const submitConsentEvent = async ({
|
|
|
151
144
|
purposes,
|
|
152
145
|
declined,
|
|
153
146
|
meta_data,
|
|
154
|
-
|
|
147
|
+
guardianVerificationReference,
|
|
148
|
+
selfDeclaredAdult,
|
|
149
|
+
signal,
|
|
155
150
|
}: SubmitConsentEventParams & { signal?: AbortSignal }): Promise<void> => {
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
throw new Error("noticeUuid, accessToken, and purposes array are required");
|
|
160
|
-
}
|
|
151
|
+
if (!noticeUuid || !accessToken || !Array.isArray(purposes)) {
|
|
152
|
+
throw new Error("noticeUuid, accessToken, and purposes array are required");
|
|
153
|
+
}
|
|
161
154
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
155
|
+
const decodedToken = decodeTokenSafely(accessToken);
|
|
156
|
+
if (!decodedToken) {
|
|
157
|
+
throw new Error("Invalid access token");
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const { organisation_uuid: ORGANISATION_UUID, workspace_uuid: WORKSPACE_UUID } = decodedToken;
|
|
161
|
+
if (!ORGANISATION_UUID || !WORKSPACE_UUID) {
|
|
162
|
+
throw new Error("Invalid token: missing organization or workspace UUID");
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const apiBaseUrl = baseUrl || BASE_URL;
|
|
166
|
+
|
|
167
|
+
const validatedPurposes = purposes.map((purpose) => {
|
|
168
|
+
if (!purpose.uuid) {
|
|
169
|
+
throw new Error(`Invalid purpose data: missing uuid`);
|
|
165
170
|
}
|
|
166
171
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
172
|
+
// Only include enabled data elements (disabled elements are not selectable)
|
|
173
|
+
const dataElements = (purpose.data_elements || [])
|
|
174
|
+
.filter((element) => element.enabled)
|
|
175
|
+
.map((element) => ({
|
|
176
|
+
uuid: element.uuid,
|
|
177
|
+
selected: element.required ? true : Boolean(element.selected),
|
|
178
|
+
}));
|
|
179
|
+
|
|
180
|
+
return {
|
|
181
|
+
purpose_uuid: purpose.uuid,
|
|
182
|
+
selected: Boolean(purpose.selected),
|
|
183
|
+
data_elements: dataElements.length > 0 ? dataElements : undefined,
|
|
184
|
+
};
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
const payload: ConsentEventPayload = {
|
|
188
|
+
notice_uuid: noticeUuid,
|
|
189
|
+
purposes: validatedPurposes,
|
|
190
|
+
select_all_mandatory: false,
|
|
191
|
+
source: "WEB",
|
|
192
|
+
declined: Boolean(declined),
|
|
193
|
+
meta_data: meta_data ? { specific_uuid: meta_data.specific_uuid, minor_age: meta_data.minor_age } : undefined,
|
|
194
|
+
guardian_verification_reference: guardianVerificationReference,
|
|
195
|
+
self_declared_adult: selfDeclaredAdult,
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
const response = await fetch(
|
|
199
|
+
`${apiBaseUrl}/public/organisations/${ORGANISATION_UUID}/workspaces/${WORKSPACE_UUID}/submit-consent`,
|
|
200
|
+
{
|
|
201
|
+
method: "POST",
|
|
202
|
+
headers: {
|
|
203
|
+
Authorization: `Bearer ${accessToken}`,
|
|
204
|
+
"Content-Type": "application/json",
|
|
205
|
+
},
|
|
206
|
+
body: JSON.stringify(payload),
|
|
207
|
+
signal,
|
|
170
208
|
}
|
|
209
|
+
);
|
|
171
210
|
|
|
172
|
-
|
|
211
|
+
if (!response.ok) {
|
|
212
|
+
let errorMessage = `Failed to submit consent: ${response.status} ${response.statusText}`;
|
|
173
213
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
214
|
+
if (response.status === 400) {
|
|
215
|
+
try {
|
|
216
|
+
const errorData = await response.json();
|
|
217
|
+
errorMessage = errorData.message || "Invalid request data";
|
|
218
|
+
} catch {
|
|
219
|
+
errorMessage = "Invalid request data";
|
|
177
220
|
}
|
|
221
|
+
} else if (response.status === 401) {
|
|
222
|
+
errorMessage = "Unauthorized: Invalid or expired token";
|
|
223
|
+
} else if (response.status === 403) {
|
|
224
|
+
errorMessage = "Forbidden: Access denied";
|
|
225
|
+
} else if (response.status === 404) {
|
|
226
|
+
errorMessage = "Notice not found";
|
|
227
|
+
} else if (response.status >= 500) {
|
|
228
|
+
errorMessage = "Server error: Please try again later";
|
|
229
|
+
}
|
|
178
230
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
uuid: element.uuid,
|
|
184
|
-
selected: element.required ? true : Boolean(element.selected),
|
|
185
|
-
}));
|
|
231
|
+
const error = new Error(errorMessage) as Error & { status?: number };
|
|
232
|
+
error.status = response.status;
|
|
233
|
+
throw error;
|
|
234
|
+
}
|
|
186
235
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
data_elements: dataElements.length > 0 ? dataElements : undefined,
|
|
191
|
-
};
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
const payload: ConsentEventPayload = {
|
|
195
|
-
notice_uuid: noticeUuid,
|
|
196
|
-
purposes: validatedPurposes,
|
|
197
|
-
select_all_mandatory: false,
|
|
198
|
-
source: "WEB",
|
|
199
|
-
declined: Boolean(declined),
|
|
200
|
-
meta_data: meta_data ? { specific_uuid: meta_data.specific_uuid } : undefined,
|
|
201
|
-
};
|
|
236
|
+
// Clear any cached consent content since user preferences have changed
|
|
237
|
+
apiCache.clear();
|
|
238
|
+
};
|
|
202
239
|
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
}
|
|
214
|
-
);
|
|
240
|
+
export const fetchTTSAudioUrls = async ({
|
|
241
|
+
accessToken,
|
|
242
|
+
baseUrl,
|
|
243
|
+
noticeUuid,
|
|
244
|
+
language,
|
|
245
|
+
signal,
|
|
246
|
+
}: FetchTTSAudioUrlsParams & { signal?: AbortSignal }): Promise<TTSAudioUrlsResponse> => {
|
|
247
|
+
if (!accessToken || !noticeUuid || !language) {
|
|
248
|
+
throw new Error("accessToken, noticeUuid, and language are required");
|
|
249
|
+
}
|
|
215
250
|
|
|
216
|
-
|
|
217
|
-
|
|
251
|
+
const decodedToken = decodeTokenSafely(accessToken);
|
|
252
|
+
if (!decodedToken) {
|
|
253
|
+
throw new Error("Invalid access token");
|
|
254
|
+
}
|
|
218
255
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
} else if (response.status === 403) {
|
|
224
|
-
errorMessage = "Forbidden: Access denied";
|
|
225
|
-
} else if (response.status === 404) {
|
|
226
|
-
errorMessage = "Notice not found";
|
|
227
|
-
} else if (response.status >= 500) {
|
|
228
|
-
errorMessage = "Server error: Please try again later";
|
|
229
|
-
}
|
|
256
|
+
const { organisation_uuid: ORGANISATION_UUID, workspace_uuid: WORKSPACE_UUID } = decodedToken;
|
|
257
|
+
if (!ORGANISATION_UUID || !WORKSPACE_UUID) {
|
|
258
|
+
throw new Error("Invalid token: missing organization or workspace UUID");
|
|
259
|
+
}
|
|
230
260
|
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
261
|
+
const apiBaseUrl = baseUrl || BASE_URL;
|
|
262
|
+
|
|
263
|
+
const url = `${apiBaseUrl}/public/organisations/${ORGANISATION_UUID}/workspaces/${WORKSPACE_UUID}/notices/${noticeUuid}/audio/${language}`;
|
|
264
|
+
|
|
265
|
+
const response = await fetch(url, {
|
|
266
|
+
method: "GET",
|
|
267
|
+
headers: {
|
|
268
|
+
Authorization: `Bearer ${accessToken}`,
|
|
269
|
+
Accept: "application/json",
|
|
270
|
+
},
|
|
271
|
+
signal,
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
if (!response.ok) {
|
|
275
|
+
let errorMessage = `Failed to fetch TTS audio URLs: ${response.status} ${response.statusText}`;
|
|
276
|
+
|
|
277
|
+
if (response.status === 401) {
|
|
278
|
+
errorMessage = "Unauthorized: Invalid or expired token";
|
|
279
|
+
} else if (response.status === 403) {
|
|
280
|
+
errorMessage = "Forbidden: Access denied";
|
|
281
|
+
} else if (response.status === 404) {
|
|
282
|
+
errorMessage = "Notice or audio not found";
|
|
283
|
+
} else if (response.status >= 500) {
|
|
284
|
+
errorMessage = "Server error: Please try again later";
|
|
234
285
|
}
|
|
235
286
|
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
} catch (error) {
|
|
287
|
+
const error = new Error(errorMessage) as Error & { status?: number };
|
|
288
|
+
error.status = response.status;
|
|
240
289
|
throw error;
|
|
241
290
|
}
|
|
291
|
+
|
|
292
|
+
const data: TTSAudioUrlsResponse = await response.json();
|
|
293
|
+
return data;
|
|
242
294
|
};
|
|
243
295
|
|
|
244
|
-
|
|
296
|
+
// =============================================================================
|
|
297
|
+
// Guardian Verification API Functions
|
|
298
|
+
// =============================================================================
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Initiate guardian verification via DigiLocker
|
|
302
|
+
* POST /guardian/initiate-verification
|
|
303
|
+
*
|
|
304
|
+
* This combines guardian details submission and DigiLocker initiation in one call.
|
|
305
|
+
*/
|
|
306
|
+
export const initiateGuardianVerification = async ({
|
|
245
307
|
accessToken,
|
|
246
308
|
baseUrl,
|
|
247
309
|
guardianName,
|
|
248
310
|
guardianContact,
|
|
249
311
|
guardianRelationship,
|
|
250
|
-
|
|
251
|
-
|
|
312
|
+
frontendCallbackUrl,
|
|
313
|
+
metadata,
|
|
314
|
+
signal,
|
|
315
|
+
}: InitiateGuardianVerificationParams): Promise<InitiateGuardianVerificationResponse> => {
|
|
252
316
|
try {
|
|
253
|
-
// Validate required parameters
|
|
254
317
|
if (!accessToken) {
|
|
255
318
|
throw new Error("accessToken is required");
|
|
256
319
|
}
|
|
257
320
|
|
|
258
321
|
if (!guardianName?.trim()) {
|
|
259
|
-
throw new Error("
|
|
322
|
+
throw new Error("Guardian name is required");
|
|
260
323
|
}
|
|
261
324
|
|
|
262
325
|
if (!guardianContact?.trim()) {
|
|
263
|
-
throw new Error("
|
|
326
|
+
throw new Error("Guardian contact is required");
|
|
264
327
|
}
|
|
265
328
|
|
|
266
329
|
if (!guardianRelationship?.trim()) {
|
|
267
|
-
throw new Error("
|
|
330
|
+
throw new Error("Guardian relationship is required");
|
|
268
331
|
}
|
|
269
332
|
|
|
270
333
|
const decodedToken = decodeTokenSafely(accessToken);
|
|
@@ -277,16 +340,30 @@ export const submitGuardianInfo = async ({
|
|
|
277
340
|
throw new Error("Invalid token: missing organization or workspace UUID");
|
|
278
341
|
}
|
|
279
342
|
|
|
280
|
-
const
|
|
343
|
+
const apiBaseUrl = baseUrl || BASE_URL;
|
|
344
|
+
|
|
345
|
+
const payload: {
|
|
346
|
+
guardian_name: string;
|
|
347
|
+
guardian_contact: string;
|
|
348
|
+
guardian_relationship: string;
|
|
349
|
+
frontend_callback_url?: string;
|
|
350
|
+
metadata?: Record<string, unknown>;
|
|
351
|
+
} = {
|
|
281
352
|
guardian_name: guardianName.trim(),
|
|
282
353
|
guardian_contact: guardianContact.trim(),
|
|
283
354
|
guardian_relationship: guardianRelationship.trim(),
|
|
284
355
|
};
|
|
285
356
|
|
|
286
|
-
|
|
357
|
+
if (frontendCallbackUrl) {
|
|
358
|
+
payload.frontend_callback_url = frontendCallbackUrl;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
if (metadata) {
|
|
362
|
+
payload.metadata = metadata;
|
|
363
|
+
}
|
|
287
364
|
|
|
288
365
|
const response = await fetch(
|
|
289
|
-
`${apiBaseUrl}/public/organisations/${ORGANISATION_UUID}/workspaces/${WORKSPACE_UUID}/guardian-
|
|
366
|
+
`${apiBaseUrl}/public/organisations/${ORGANISATION_UUID}/workspaces/${WORKSPACE_UUID}/guardian/initiate-verification`,
|
|
290
367
|
{
|
|
291
368
|
method: "POST",
|
|
292
369
|
headers: {
|
|
@@ -294,27 +371,29 @@ export const submitGuardianInfo = async ({
|
|
|
294
371
|
"Content-Type": "application/json",
|
|
295
372
|
},
|
|
296
373
|
body: JSON.stringify(payload),
|
|
297
|
-
signal,
|
|
374
|
+
signal,
|
|
298
375
|
}
|
|
299
376
|
);
|
|
300
377
|
|
|
301
378
|
if (!response.ok) {
|
|
302
|
-
let errorMessage = `Failed to
|
|
379
|
+
let errorMessage = `Failed to initiate verification: ${response.status} ${response.statusText}`;
|
|
303
380
|
|
|
304
381
|
if (response.status === 400) {
|
|
305
|
-
|
|
382
|
+
try {
|
|
383
|
+
const errorData = await response.json();
|
|
384
|
+
errorMessage = errorData.message || "Invalid request data";
|
|
385
|
+
} catch {
|
|
386
|
+
errorMessage = "Invalid request data";
|
|
387
|
+
}
|
|
306
388
|
} else if (response.status === 401) {
|
|
307
389
|
errorMessage = "Unauthorized: Invalid or expired token";
|
|
308
|
-
} else if (response.status === 403) {
|
|
309
|
-
errorMessage = "Forbidden: Access denied";
|
|
310
390
|
} else if (response.status === 404) {
|
|
311
391
|
errorMessage = "Organization or workspace not found";
|
|
312
392
|
} else if (response.status === 422) {
|
|
313
|
-
// Handle validation errors from the server
|
|
314
393
|
try {
|
|
315
394
|
const errorData = await response.json();
|
|
316
|
-
errorMessage = errorData.message || "Validation error";
|
|
317
|
-
} catch
|
|
395
|
+
errorMessage = errorData.message || "Validation error: Please check your input";
|
|
396
|
+
} catch {
|
|
318
397
|
errorMessage = "Validation error: Please check your input";
|
|
319
398
|
}
|
|
320
399
|
} else if (response.status >= 500) {
|
|
@@ -326,72 +405,120 @@ export const submitGuardianInfo = async ({
|
|
|
326
405
|
throw error;
|
|
327
406
|
}
|
|
328
407
|
|
|
408
|
+
const responseData = await response.json();
|
|
409
|
+
|
|
410
|
+
// Handle response with detail wrapper (backend format: { code, status, detail: {...} })
|
|
411
|
+
const detail = responseData.detail || responseData;
|
|
412
|
+
|
|
413
|
+
// Already verified — skip DigiLocker entirely
|
|
414
|
+
if (detail.already_verified) {
|
|
415
|
+
return {
|
|
416
|
+
success: true,
|
|
417
|
+
guardian_info_uuid: detail.guardian_info_uuid,
|
|
418
|
+
already_verified: true,
|
|
419
|
+
verification_reference: detail.verification_reference,
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
// New verification flow — needs DigiLocker
|
|
424
|
+
if (!detail.session_token || !detail.digilocker_redirect_url) {
|
|
425
|
+
throw new Error("Invalid response: missing session_token or digilocker_redirect_url");
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
return {
|
|
429
|
+
success: detail.success ?? true,
|
|
430
|
+
guardian_info_uuid: detail.guardian_info_uuid,
|
|
431
|
+
already_verified: false,
|
|
432
|
+
session_token: detail.session_token,
|
|
433
|
+
digilocker_redirect_url: detail.digilocker_redirect_url,
|
|
434
|
+
expires_at: detail.expires_at,
|
|
435
|
+
};
|
|
329
436
|
} catch (error) {
|
|
330
|
-
|
|
331
|
-
console.error("Error submitting guardian info:", error instanceof Error ? error.message : error);
|
|
437
|
+
console.error("Error initiating guardian verification:", error instanceof Error ? error.message : error);
|
|
332
438
|
throw error;
|
|
333
439
|
}
|
|
334
440
|
};
|
|
335
441
|
|
|
336
|
-
|
|
442
|
+
/**
|
|
443
|
+
* Check guardian verification status
|
|
444
|
+
* POST /guardian/verify-status
|
|
445
|
+
*
|
|
446
|
+
* Poll this endpoint to check if DigiLocker verification is complete.
|
|
447
|
+
* Returns verification_reference when status is "verified".
|
|
448
|
+
*/
|
|
449
|
+
export const verifyGuardianStatus = async ({
|
|
337
450
|
accessToken,
|
|
338
451
|
baseUrl,
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
if (!accessToken || !noticeUuid || !language) {
|
|
346
|
-
throw new Error("accessToken, noticeUuid, and language are required");
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
const decodedToken = decodeTokenSafely(accessToken);
|
|
350
|
-
if (!decodedToken) {
|
|
351
|
-
throw new Error("Invalid access token");
|
|
352
|
-
}
|
|
452
|
+
sessionToken,
|
|
453
|
+
signal,
|
|
454
|
+
}: VerifyGuardianStatusParams): Promise<VerifyGuardianStatusResponse> => {
|
|
455
|
+
if (!accessToken || !sessionToken) {
|
|
456
|
+
throw new Error("accessToken and sessionToken are required");
|
|
457
|
+
}
|
|
353
458
|
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
459
|
+
const decodedToken = decodeTokenSafely(accessToken);
|
|
460
|
+
if (!decodedToken) {
|
|
461
|
+
throw new Error("Invalid access token");
|
|
462
|
+
}
|
|
358
463
|
|
|
359
|
-
|
|
464
|
+
const { organisation_uuid: ORGANISATION_UUID, workspace_uuid: WORKSPACE_UUID } = decodedToken;
|
|
465
|
+
if (!ORGANISATION_UUID || !WORKSPACE_UUID) {
|
|
466
|
+
throw new Error("Invalid token: missing organization or workspace UUID");
|
|
467
|
+
}
|
|
360
468
|
|
|
361
|
-
|
|
469
|
+
const apiBaseUrl = baseUrl || BASE_URL;
|
|
362
470
|
|
|
363
|
-
|
|
364
|
-
|
|
471
|
+
const response = await fetch(
|
|
472
|
+
`${apiBaseUrl}/public/organisations/${ORGANISATION_UUID}/workspaces/${WORKSPACE_UUID}/guardian/verify-status`,
|
|
473
|
+
{
|
|
474
|
+
method: "POST",
|
|
365
475
|
headers: {
|
|
366
476
|
Authorization: `Bearer ${accessToken}`,
|
|
367
|
-
Accept: "application/json",
|
|
368
477
|
"Content-Type": "application/json",
|
|
369
478
|
},
|
|
370
|
-
|
|
371
|
-
|
|
479
|
+
body: JSON.stringify({
|
|
480
|
+
session_token: sessionToken,
|
|
481
|
+
}),
|
|
482
|
+
signal,
|
|
483
|
+
}
|
|
484
|
+
);
|
|
372
485
|
|
|
373
|
-
|
|
374
|
-
|
|
486
|
+
if (!response.ok) {
|
|
487
|
+
let errorMessage = `Failed to check verification status: ${response.status} ${response.statusText}`;
|
|
375
488
|
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
errorMessage = "
|
|
380
|
-
}
|
|
381
|
-
errorMessage = "
|
|
382
|
-
} else if (response.status >= 500) {
|
|
383
|
-
errorMessage = "Server error: Please try again later";
|
|
489
|
+
if (response.status === 400) {
|
|
490
|
+
try {
|
|
491
|
+
const errorData = await response.json();
|
|
492
|
+
errorMessage = errorData.message || errorData.detail?.message || "Invalid session token";
|
|
493
|
+
} catch {
|
|
494
|
+
errorMessage = "Invalid session token";
|
|
384
495
|
}
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
496
|
+
} else if (response.status === 401) {
|
|
497
|
+
errorMessage = "Unauthorized: Invalid or expired token";
|
|
498
|
+
} else if (response.status === 404) {
|
|
499
|
+
errorMessage = "Verification session not found or expired. Please restart the verification process.";
|
|
500
|
+
} else if (response.status === 410) {
|
|
501
|
+
errorMessage = "Verification session has expired. Please restart the verification process.";
|
|
502
|
+
} else if (response.status >= 500) {
|
|
503
|
+
errorMessage = "Server error: Please try again later";
|
|
389
504
|
}
|
|
390
505
|
|
|
391
|
-
const
|
|
392
|
-
|
|
393
|
-
return data;
|
|
394
|
-
} catch (error) {
|
|
506
|
+
const error = new Error(errorMessage) as Error & { status?: number };
|
|
507
|
+
error.status = response.status;
|
|
395
508
|
throw error;
|
|
396
509
|
}
|
|
510
|
+
|
|
511
|
+
const responseData = await response.json();
|
|
512
|
+
const detail = responseData.detail || responseData;
|
|
513
|
+
|
|
514
|
+
return {
|
|
515
|
+
status: detail.status ?? "in_progress",
|
|
516
|
+
guardian_info_uuid: detail.guardian_info_uuid ?? null,
|
|
517
|
+
verification_reference: detail.verification_reference ?? null,
|
|
518
|
+
guardian_details: detail.guardian_details ?? null,
|
|
519
|
+
error: detail.error ?? null,
|
|
520
|
+
error_code: detail.error_code ?? null,
|
|
521
|
+
can_retry: detail.can_retry ?? true,
|
|
522
|
+
};
|
|
397
523
|
};
|
|
524
|
+
|