@sanvika/auth 2.5.2 → 2.5.4

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
@@ -17,6 +17,7 @@ var SanvikaAuthContext = createContext(null);
17
17
  function SanvikaAuthProvider({
18
18
  children,
19
19
  clientId,
20
+ redirectUri,
20
21
  dashboardPath
21
22
  }) {
22
23
  const [user, setUser] = useState(null);
@@ -102,6 +103,7 @@ function SanvikaAuthProvider({
102
103
  setAuth,
103
104
  authFetch,
104
105
  clientId,
106
+ redirectUri,
105
107
  dashboardPath
106
108
  };
107
109
  return /* @__PURE__ */ jsx(SanvikaAuthContext.Provider, { value, children });
@@ -273,7 +275,7 @@ function SanvikaAccountButtonContent({
273
275
  }
274
276
  if (!isAuthenticated || loading) {
275
277
  const { clientId } = auth;
276
- const redirectUri = typeof window !== "undefined" ? window.location.origin + "/auth/callback" : "";
278
+ const redirectUri = auth.redirectUri || (typeof window !== "undefined" && window.location ? window.location.origin + "/auth/callback" : "");
277
279
  const authorizeUrl = clientId && redirectUri ? `${S_AUTH_URL2}/authorize?client_id=${encodeURIComponent(clientId)}&redirect_uri=${encodeURIComponent(redirectUri)}` : `${S_AUTH_URL2}/authorize`;
278
280
  return /* @__PURE__ */ jsxs(
279
281
  "button",
package/dist/server.js CHANGED
@@ -1,19 +1,25 @@
1
- // server.js
2
- var _authUrl = () => {
1
+ const _authUrl = () => {
3
2
  var _a;
4
3
  return typeof process !== "undefined" && ((_a = process.env) == null ? void 0 : _a.AUTH_URL) || "https://accounts.sanvikaproduction.com";
5
4
  };
6
- var _serviceKey = () => {
5
+ const _serviceKey = () => {
7
6
  var _a;
8
7
  return typeof process !== "undefined" && ((_a = process.env) == null ? void 0 : _a.AUTH_SERVICE_KEY) || "";
9
8
  };
10
- var _s2sHeaders = () => ({
9
+ const _s2sHeaders = () => ({
11
10
  "Content-Type": "application/json",
12
11
  ..._serviceKey() ? { "x-service-key": _serviceKey() } : {}
13
12
  });
13
+ function _resolveAuthHeader(requestOrObj) {
14
+ var _a, _b;
15
+ if (typeof ((_a = requestOrObj == null ? void 0 : requestOrObj.headers) == null ? void 0 : _a.get) === "function") {
16
+ return requestOrObj.headers.get("authorization") || requestOrObj.headers.get("Authorization") || null;
17
+ }
18
+ return ((_b = requestOrObj == null ? void 0 : requestOrObj.headers) == null ? void 0 : _b.authorization) || null;
19
+ }
14
20
  async function verifyAuthToken(request) {
15
21
  try {
16
- const auth = request.headers.get("authorization");
22
+ const auth = _resolveAuthHeader(request);
17
23
  if (!(auth == null ? void 0 : auth.startsWith("Bearer "))) return null;
18
24
  const res = await fetch(`${_authUrl()}/api/auth/verify-token`, {
19
25
  method: "GET",
@@ -30,6 +36,28 @@ async function extractAuthUid(request) {
30
36
  const payload = await verifyAuthToken(request);
31
37
  return (payload == null ? void 0 : payload.sub) || null;
32
38
  }
39
+ async function extractUserFromToken(request) {
40
+ const auth = _resolveAuthHeader(request);
41
+ if (!auth || !auth.startsWith("Bearer ")) {
42
+ throw new Error("Authorization header missing or invalid");
43
+ }
44
+ const payload = await verifyAuthToken(request);
45
+ if (!payload) throw new Error("Invalid or expired token");
46
+ const uid = payload.sub || payload.uid;
47
+ if (!uid) throw new Error("Invalid token payload - missing uid");
48
+ return {
49
+ uid,
50
+ mobile: payload.mobile || null,
51
+ role: payload.role || "user",
52
+ roles: [],
53
+ deviceId: payload.deviceId || null,
54
+ image: payload.image || null,
55
+ firstName: payload.firstName || null,
56
+ lastName: payload.lastName || null,
57
+ exp: payload.exp,
58
+ iat: payload.iat
59
+ };
60
+ }
33
61
  async function getUserFromAuth(uid) {
34
62
  if (!uid) return null;
35
63
  try {
@@ -58,6 +86,22 @@ async function getUserLocationFromAuth(uid) {
58
86
  return null;
59
87
  }
60
88
  }
89
+ async function updateUserLocation(uid, locationData) {
90
+ if (!uid || !locationData) return null;
91
+ try {
92
+ const res = await fetch(`${_authUrl()}/api/location`, {
93
+ method: "POST",
94
+ headers: _s2sHeaders(),
95
+ body: JSON.stringify({ uid, ...locationData }),
96
+ signal: AbortSignal.timeout(5e3)
97
+ });
98
+ if (!res.ok) return null;
99
+ const data = await res.json();
100
+ return data.success ? data.data : null;
101
+ } catch {
102
+ return null;
103
+ }
104
+ }
61
105
  async function getUserByMobileFromAuth(mobile, authToken = "") {
62
106
  var _a;
63
107
  if (!mobile) return null;
@@ -114,9 +158,11 @@ async function listUsersFromAuth({ page = 1, limit = 20, search = "", authToken
114
158
  export {
115
159
  batchGetUsersFromAuth,
116
160
  extractAuthUid,
161
+ extractUserFromToken,
117
162
  getUserByMobileFromAuth,
118
163
  getUserFromAuth,
119
164
  getUserLocationFromAuth,
120
165
  listUsersFromAuth,
166
+ updateUserLocation,
121
167
  verifyAuthToken
122
168
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanvika/auth",
3
- "version": "2.5.2",
3
+ "version": "2.5.4",
4
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",