@sanvika/auth 2.0.0 → 2.2.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.
Files changed (2) hide show
  1. package/dist/index.js +35 -10
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -14,7 +14,14 @@ var DEFAULT_AVATAR_SVG = `data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/200
14
14
  import { jsx } from "react/jsx-runtime";
15
15
  var SA_URL = "https://accounts.sanvikaproduction.com";
16
16
  var SanvikaAuthContext = createContext(null);
17
- function SanvikaAuthProvider({ children }) {
17
+ function SanvikaAuthProvider({
18
+ children,
19
+ clientId,
20
+ redirectUri,
21
+ saUrl: saUrlProp,
22
+ dashboardPath
23
+ }) {
24
+ const saBaseUrl = saUrlProp || SA_URL;
18
25
  const [user, setUser] = useState(null);
19
26
  const [accessToken, setToken] = useState(null);
20
27
  const [loading, setLoading] = useState(true);
@@ -51,6 +58,12 @@ function SanvikaAuthProvider({ children }) {
51
58
  setUser(data.user);
52
59
  return data;
53
60
  };
61
+ const setAuth = (token, userData) => {
62
+ localStorage.setItem(STORAGE_KEYS.ACCESS_TOKEN, token);
63
+ localStorage.setItem(STORAGE_KEYS.USER, JSON.stringify(userData));
64
+ setToken(token);
65
+ setUser(userData);
66
+ };
54
67
  const logout = async () => {
55
68
  try {
56
69
  await fetch(`${SA_URL}/api/auth/logout`, {
@@ -75,7 +88,12 @@ function SanvikaAuthProvider({ children }) {
75
88
  loading,
76
89
  isAuthenticated: !!user,
77
90
  login,
78
- logout
91
+ logout,
92
+ setAuth,
93
+ clientId,
94
+ redirectUri,
95
+ saBaseUrl,
96
+ dashboardPath
79
97
  };
80
98
  return /* @__PURE__ */ jsx(SanvikaAuthContext.Provider, { value, children });
81
99
  }
@@ -232,12 +250,10 @@ function SanvikaAccountButtonContent({
232
250
  console.log("[SanvikaAccountButton] Calling custom onLoginClick");
233
251
  onLoginClick();
234
252
  } else {
235
- console.log("[SanvikaAccountButton] Redirecting to /authorize...");
236
253
  console.log(
237
- "[SanvikaAccountButton] Target URL:",
238
- window.location.origin + "/authorize"
254
+ "[SanvikaAccountButton] Redirecting to SA authorize..."
239
255
  );
240
- window.location.href = "/authorize";
256
+ window.location.href = "https://accounts.sanvikaproduction.com/authorize";
241
257
  }
242
258
  },
243
259
  "aria-label": "Login or Sign Up",
@@ -260,15 +276,24 @@ function SanvikaAccountButtonContent({
260
276
  setImgError(false);
261
277
  }
262
278
  if (!isAuthenticated || loading) {
279
+ const { clientId, redirectUri, saBaseUrl } = auth;
280
+ const authorizeUrl = clientId && redirectUri ? `${saBaseUrl}/authorize?client_id=${encodeURIComponent(clientId)}&redirect_uri=${encodeURIComponent(redirectUri)}` : `${saBaseUrl}/authorize`;
263
281
  return /* @__PURE__ */ jsxs(
264
282
  "button",
265
283
  {
266
284
  className: `snvk-guestBtn ${className}`,
267
- onClick: () => {
285
+ onClick: (e) => {
286
+ console.log("[SanvikaAccountButton] \u2705\u2705\u2705 BUTTON CLICKED!");
287
+ console.log("[SanvikaAccountButton] Event target:", e.target);
268
288
  if (onLoginClick) {
289
+ console.log("[SanvikaAccountButton] Calling custom onLoginClick");
269
290
  onLoginClick();
270
291
  } else {
271
- window.location.href = "https://accounts.sanvikaproduction.com";
292
+ console.log(
293
+ "[SanvikaAccountButton] Redirecting to SA authorize...",
294
+ authorizeUrl
295
+ );
296
+ window.location.href = authorizeUrl;
272
297
  }
273
298
  },
274
299
  "aria-label": "Login or Sign Up",
@@ -289,7 +314,7 @@ function SanvikaAccountButtonContent({
289
314
  const imageSrc = !imgError && user.image ? user.image : DEFAULT_AVATAR_SVG;
290
315
  const handleProfileClick = () => {
291
316
  if (onProfileClick) return onProfileClick();
292
- window.location.href = "https://accounts.sanvikaproduction.com/dashboard";
317
+ window.location.href = (auth == null ? void 0 : auth.dashboardPath) || "https://accounts.sanvikaproduction.com/dashboard";
293
318
  };
294
319
  const handleLogout = async () => {
295
320
  await logout(false);
@@ -348,7 +373,7 @@ function SanvikaAccountButtonContent({
348
373
  /* @__PURE__ */ jsxs(
349
374
  "a",
350
375
  {
351
- href: "/dashboard",
376
+ href: (auth == null ? void 0 : auth.dashboardPath) || "/dashboard",
352
377
  className: "snvk-dropdownItem",
353
378
  role: "menuitem",
354
379
  onClick: () => setDropdownOpen(false),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanvika/auth",
3
- "version": "2.0.0",
3
+ "version": "2.2.0",
4
4
  "description": "Sanvika Auth SDK — React components and hooks for Sanvika SSO integration",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",