@sanvika/auth 2.9.1 → 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/index.js +22 -1
- package/dist/server.js +40 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -20,6 +20,7 @@ var DEFAULT_AVATAR_SVG = `data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/200
|
|
|
20
20
|
// authFlow.js
|
|
21
21
|
var DEFAULT_AUTH_URL = "https://auth.sanvikaproduction.com";
|
|
22
22
|
var DEVICE_ID_STORAGE_KEY = "sanvika_deviceId";
|
|
23
|
+
var MOBILE_DEVICE_ID_STORAGE_KEY = "deviceId";
|
|
23
24
|
function randomDeviceId() {
|
|
24
25
|
try {
|
|
25
26
|
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
|
|
@@ -44,6 +45,22 @@ function getOrCreateWebDeviceId() {
|
|
|
44
45
|
}
|
|
45
46
|
return deviceId;
|
|
46
47
|
}
|
|
48
|
+
async function resolveLogoutDeviceId(persistence) {
|
|
49
|
+
if (persistence == null ? void 0 : persistence.getItem) {
|
|
50
|
+
try {
|
|
51
|
+
const mobileId = await persistence.getItem(MOBILE_DEVICE_ID_STORAGE_KEY);
|
|
52
|
+
if (mobileId) {
|
|
53
|
+
return mobileId;
|
|
54
|
+
}
|
|
55
|
+
const webId = await persistence.getItem(DEVICE_ID_STORAGE_KEY);
|
|
56
|
+
if (webId) {
|
|
57
|
+
return webId;
|
|
58
|
+
}
|
|
59
|
+
} catch {
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return getOrCreateWebDeviceId();
|
|
63
|
+
}
|
|
47
64
|
async function checkMobile({
|
|
48
65
|
authBaseUrl = DEFAULT_AUTH_URL,
|
|
49
66
|
mobile,
|
|
@@ -270,12 +287,14 @@ function SanvikaAuthProvider({
|
|
|
270
287
|
);
|
|
271
288
|
const logout = async () => {
|
|
272
289
|
try {
|
|
290
|
+
const deviceId = await resolveLogoutDeviceId(persistence);
|
|
273
291
|
await fetch(`${authBaseUrl}/api/auth/logout`, {
|
|
274
292
|
method: "POST",
|
|
275
293
|
headers: {
|
|
276
294
|
"Content-Type": "application/json",
|
|
277
295
|
...accessToken ? { Authorization: `Bearer ${accessToken}` } : {}
|
|
278
|
-
}
|
|
296
|
+
},
|
|
297
|
+
body: JSON.stringify(deviceId ? { deviceId } : {})
|
|
279
298
|
});
|
|
280
299
|
} catch (e) {
|
|
281
300
|
console.error("[SanvikaAuth] Logout API error:", e);
|
|
@@ -847,6 +866,7 @@ export {
|
|
|
847
866
|
DEFAULT_AUTH_URL,
|
|
848
867
|
DEFAULT_AVATAR_SVG,
|
|
849
868
|
DEVICE_ID_STORAGE_KEY,
|
|
869
|
+
MOBILE_DEVICE_ID_STORAGE_KEY,
|
|
850
870
|
STORAGE_KEYS,
|
|
851
871
|
SanvikaAccountButton,
|
|
852
872
|
SanvikaAdminLogin,
|
|
@@ -857,5 +877,6 @@ export {
|
|
|
857
877
|
getOrCreateWebDeviceId,
|
|
858
878
|
postLogin,
|
|
859
879
|
randomDeviceId,
|
|
880
|
+
resolveLogoutDeviceId,
|
|
860
881
|
useSanvikaAuth
|
|
861
882
|
};
|
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,
|