@sanvika/auth 2.2.0 → 2.3.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/dist/index.js CHANGED
@@ -12,7 +12,7 @@ var DEFAULT_AVATAR_SVG = `data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/200
12
12
 
13
13
  // SanvikaAuthProvider.jsx
14
14
  import { jsx } from "react/jsx-runtime";
15
- var SA_URL = "https://accounts.sanvikaproduction.com";
15
+ var S_AUTH_URL = "https://accounts.sanvikaproduction.com";
16
16
  var SanvikaAuthContext = createContext(null);
17
17
  function SanvikaAuthProvider({
18
18
  children,
@@ -21,7 +21,7 @@ function SanvikaAuthProvider({
21
21
  saUrl: saUrlProp,
22
22
  dashboardPath
23
23
  }) {
24
- const saBaseUrl = saUrlProp || SA_URL;
24
+ const saBaseUrl = saUrlProp || S_AUTH_URL;
25
25
  const [user, setUser] = useState(null);
26
26
  const [accessToken, setToken] = useState(null);
27
27
  const [loading, setLoading] = useState(true);
@@ -40,7 +40,7 @@ function SanvikaAuthProvider({
40
40
  }
41
41
  }, []);
42
42
  const login = async ({ mobile, password, deviceId, deviceName }) => {
43
- const response = await fetch(`${SA_URL}/api/auth/login`, {
43
+ const response = await fetch(`${S_AUTH_URL}/api/auth/login`, {
44
44
  method: "POST",
45
45
  headers: { "Content-Type": "application/json" },
46
46
  body: JSON.stringify({
@@ -66,7 +66,7 @@ function SanvikaAuthProvider({
66
66
  };
67
67
  const logout = async () => {
68
68
  try {
69
- await fetch(`${SA_URL}/api/auth/logout`, {
69
+ await fetch(`${S_AUTH_URL}/api/auth/logout`, {
70
70
  method: "POST",
71
71
  headers: {
72
72
  "Content-Type": "application/json",
@@ -216,9 +216,6 @@ function SanvikaAccountButtonContent({
216
216
  }) {
217
217
  var _a, _b;
218
218
  const auth = useSanvikaAuth();
219
- console.log("[SanvikaAccountButton] auth context:", auth);
220
- console.log("[SanvikaAccountButton] auth type:", typeof auth);
221
- console.log("[SanvikaAccountButton] auth is null:", auth === null);
222
219
  const [dropdownOpen, setDropdownOpen] = useState2(false);
223
220
  const [imgError, setImgError] = useState2(false);
224
221
  const [prevImage, setPrevImage] = useState2((_a = auth == null ? void 0 : auth.user) == null ? void 0 : _a.image);
@@ -234,25 +231,14 @@ function SanvikaAccountButtonContent({
234
231
  return () => document.removeEventListener("mousedown", handleOutside);
235
232
  }, [dropdownOpen]);
236
233
  if (!auth) {
237
- console.log("[SanvikaAccountButton] Auth is null, showing guest button");
238
234
  return /* @__PURE__ */ jsxs(
239
235
  "button",
240
236
  {
241
237
  className: `snvk-guestBtn ${className}`,
242
238
  onClick: (e) => {
243
- console.log("[SanvikaAccountButton] \u2705\u2705\u2705 BUTTON CLICKED!");
244
- console.log("[SanvikaAccountButton] Event target:", e.target);
245
- console.log(
246
- "[SanvikaAccountButton] Current URL:",
247
- window.location.href
248
- );
249
239
  if (onLoginClick) {
250
- console.log("[SanvikaAccountButton] Calling custom onLoginClick");
251
240
  onLoginClick();
252
241
  } else {
253
- console.log(
254
- "[SanvikaAccountButton] Redirecting to SA authorize..."
255
- );
256
242
  window.location.href = "https://accounts.sanvikaproduction.com/authorize";
257
243
  }
258
244
  },
@@ -283,16 +269,9 @@ function SanvikaAccountButtonContent({
283
269
  {
284
270
  className: `snvk-guestBtn ${className}`,
285
271
  onClick: (e) => {
286
- console.log("[SanvikaAccountButton] \u2705\u2705\u2705 BUTTON CLICKED!");
287
- console.log("[SanvikaAccountButton] Event target:", e.target);
288
272
  if (onLoginClick) {
289
- console.log("[SanvikaAccountButton] Calling custom onLoginClick");
290
273
  onLoginClick();
291
274
  } else {
292
- console.log(
293
- "[SanvikaAccountButton] Redirecting to SA authorize...",
294
- authorizeUrl
295
- );
296
275
  window.location.href = authorizeUrl;
297
276
  }
298
277
  },
package/dist/server.js ADDED
@@ -0,0 +1,122 @@
1
+ // server.js
2
+ var _saUrl = () => {
3
+ var _a;
4
+ return typeof process !== "undefined" && ((_a = process.env) == null ? void 0 : _a.SA_URL) || "https://accounts.sanvikaproduction.com";
5
+ };
6
+ var _serviceKey = () => {
7
+ var _a;
8
+ return typeof process !== "undefined" && ((_a = process.env) == null ? void 0 : _a.AUTH_SERVICE_KEY) || "";
9
+ };
10
+ var _s2sHeaders = () => ({
11
+ "Content-Type": "application/json",
12
+ ..._serviceKey() ? { "x-service-key": _serviceKey() } : {}
13
+ });
14
+ async function verifySAToken(request) {
15
+ try {
16
+ const auth = request.headers.get("authorization");
17
+ if (!(auth == null ? void 0 : auth.startsWith("Bearer "))) return null;
18
+ const res = await fetch(`${_saUrl()}/api/auth/verify-token`, {
19
+ method: "GET",
20
+ headers: { Authorization: auth }
21
+ });
22
+ if (!res.ok) return null;
23
+ const json = await res.json();
24
+ return (json == null ? void 0 : json.success) ? json.payload : null;
25
+ } catch {
26
+ return null;
27
+ }
28
+ }
29
+ async function extractSAUid(request) {
30
+ const payload = await verifySAToken(request);
31
+ return (payload == null ? void 0 : payload.sub) || null;
32
+ }
33
+ async function getUserFromSA(uid) {
34
+ if (!uid) return null;
35
+ try {
36
+ const res = await fetch(`${_saUrl()}/api/user/profile?uid=${uid}`, {
37
+ headers: _s2sHeaders(),
38
+ signal: AbortSignal.timeout(5e3)
39
+ });
40
+ if (!res.ok) return null;
41
+ const data = await res.json();
42
+ return data.success ? data.data : null;
43
+ } catch {
44
+ return null;
45
+ }
46
+ }
47
+ async function getUserLocationFromSA(uid) {
48
+ if (!uid) return null;
49
+ try {
50
+ const res = await fetch(`${_saUrl()}/api/location?uid=${uid}`, {
51
+ headers: _s2sHeaders(),
52
+ signal: AbortSignal.timeout(5e3)
53
+ });
54
+ if (!res.ok) return null;
55
+ const data = await res.json();
56
+ return data.success ? data.data : null;
57
+ } catch {
58
+ return null;
59
+ }
60
+ }
61
+ async function getUserByMobileFromSA(mobile, authToken = "") {
62
+ var _a;
63
+ if (!mobile) return null;
64
+ try {
65
+ const headers = {
66
+ ..._s2sHeaders(),
67
+ ...authToken ? { Authorization: `Bearer ${authToken}` } : {}
68
+ };
69
+ const res = await fetch(
70
+ `${_saUrl()}/api/admin/users?search=${encodeURIComponent(mobile)}&limit=1`,
71
+ { headers, signal: AbortSignal.timeout(5e3) }
72
+ );
73
+ if (!res.ok) return null;
74
+ const data = await res.json();
75
+ return data.success && ((_a = data.users) == null ? void 0 : _a.length) > 0 ? data.users[0] : null;
76
+ } catch {
77
+ return null;
78
+ }
79
+ }
80
+ async function batchGetUsersFromSA(uids) {
81
+ const results = /* @__PURE__ */ new Map();
82
+ const batchSize = 10;
83
+ for (let i = 0; i < uids.length; i += batchSize) {
84
+ const batch = uids.slice(i, i + batchSize);
85
+ await Promise.all(
86
+ batch.map(async (uid) => {
87
+ const user = await getUserFromSA(uid);
88
+ if (user) results.set(uid, user);
89
+ })
90
+ );
91
+ }
92
+ return results;
93
+ }
94
+ async function listUsersFromSA({ page = 1, limit = 20, search = "", authToken = "" } = {}) {
95
+ const empty = { users: [], pagination: { page, limit, total: 0, pages: 0 } };
96
+ try {
97
+ const params = new URLSearchParams({ page: String(page), limit: String(limit) });
98
+ if (search) params.set("search", search);
99
+ const headers = {
100
+ ..._s2sHeaders(),
101
+ ...authToken ? { Authorization: `Bearer ${authToken}` } : {}
102
+ };
103
+ const res = await fetch(`${_saUrl()}/api/admin/users?${params}`, {
104
+ headers,
105
+ signal: AbortSignal.timeout(8e3)
106
+ });
107
+ if (!res.ok) return empty;
108
+ const data = await res.json();
109
+ return data.success ? { users: data.users || [], pagination: data.pagination || empty.pagination } : empty;
110
+ } catch {
111
+ return empty;
112
+ }
113
+ }
114
+ export {
115
+ batchGetUsersFromSA,
116
+ extractSAUid,
117
+ getUserByMobileFromSA,
118
+ getUserFromSA,
119
+ getUserLocationFromSA,
120
+ listUsersFromSA,
121
+ verifySAToken
122
+ };
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@sanvika/auth",
3
- "version": "2.2.0",
4
- "description": "Sanvika Auth SDK — React components and hooks for Sanvika SSO integration",
3
+ "version": "2.3.0",
4
+ "description": "Sanvika Auth SDK — React components/hooks + server-side token verification and user proxy",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.js",
8
8
  "exports": {
9
- ".": "./dist/index.js"
9
+ ".": "./dist/index.js",
10
+ "./server": "./dist/server.js"
10
11
  },
11
12
  "files": [
12
13
  "dist"