@sanvika/auth 2.9.2 → 2.9.3
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/dist/server.js +40 -0
- package/package.json +1 -1
package/dist/server.js
CHANGED
|
@@ -370,6 +370,45 @@ async function authenticateAdmin(request) {
|
|
|
370
370
|
};
|
|
371
371
|
}
|
|
372
372
|
}
|
|
373
|
+
function resolveAuthServiceUrl(authUrl) {
|
|
374
|
+
return String(
|
|
375
|
+
authUrl || process.env.AUTH_URL || process.env.NEXT_PUBLIC_SANVIKA_URL || "https://auth.sanvikaproduction.com"
|
|
376
|
+
).trim().replace(/\/+$/, "");
|
|
377
|
+
}
|
|
378
|
+
function extractBearerToken(request) {
|
|
379
|
+
const authHeader = request.headers.get("authorization") || request.headers.get("Authorization") || "";
|
|
380
|
+
if (!authHeader.startsWith("Bearer ")) return null;
|
|
381
|
+
return authHeader;
|
|
382
|
+
}
|
|
383
|
+
async function proxyAuthPasswordRequest(request, { path, method = "GET", body = null, authUrl } = {}) {
|
|
384
|
+
const bearer = extractBearerToken(request);
|
|
385
|
+
if (!bearer) {
|
|
386
|
+
return {
|
|
387
|
+
ok: false,
|
|
388
|
+
status: 401,
|
|
389
|
+
body: { success: false, message: "Authentication required." }
|
|
390
|
+
};
|
|
391
|
+
}
|
|
392
|
+
const url = `${resolveAuthServiceUrl(authUrl)}${path}`;
|
|
393
|
+
try {
|
|
394
|
+
const saRes = await fetch(url, {
|
|
395
|
+
method,
|
|
396
|
+
headers: {
|
|
397
|
+
Authorization: bearer,
|
|
398
|
+
"Content-Type": "application/json"
|
|
399
|
+
},
|
|
400
|
+
...body != null ? { body: JSON.stringify(body) } : {}
|
|
401
|
+
});
|
|
402
|
+
const data = await saRes.json().catch(() => ({}));
|
|
403
|
+
return { ok: saRes.ok, status: saRes.status, body: data };
|
|
404
|
+
} catch {
|
|
405
|
+
return {
|
|
406
|
+
ok: false,
|
|
407
|
+
status: 502,
|
|
408
|
+
body: { success: false, message: "Password service temporarily unavailable." }
|
|
409
|
+
};
|
|
410
|
+
}
|
|
411
|
+
}
|
|
373
412
|
export {
|
|
374
413
|
authenticateAdmin,
|
|
375
414
|
batchGetUsersFromAuth,
|
|
@@ -385,6 +424,7 @@ export {
|
|
|
385
424
|
getUserLocationFromAuth,
|
|
386
425
|
listAdminsFromAuth,
|
|
387
426
|
listUsersFromAuth,
|
|
427
|
+
proxyAuthPasswordRequest,
|
|
388
428
|
revokeDeviceFromAuth,
|
|
389
429
|
updateUserLocation,
|
|
390
430
|
updateUserProfile,
|