@urbackend/react 0.1.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.mjs ADDED
@@ -0,0 +1,926 @@
1
+ // src/context.tsx
2
+ import { createContext, useContext, useEffect, useState, useMemo } from "react";
3
+ import { UrBackendClient, AuthModule, DatabaseModule, StorageModule } from "@urbackend/sdk";
4
+ import { jsx } from "react/jsx-runtime";
5
+ var UrContext = createContext(void 0);
6
+ var UrProvider = ({ apiKey, baseUrl, children }) => {
7
+ const [user, setUser] = useState(null);
8
+ const [isInitializing, setIsInitializing] = useState(true);
9
+ const [isLoading, setIsLoading] = useState(false);
10
+ const [error, setError] = useState(null);
11
+ const { client, auth, db, storage } = useMemo(() => {
12
+ const _client = new UrBackendClient({ apiKey, baseUrl });
13
+ return {
14
+ client: _client,
15
+ auth: new AuthModule(_client),
16
+ db: new DatabaseModule(_client),
17
+ storage: new StorageModule(_client)
18
+ };
19
+ }, [apiKey, baseUrl]);
20
+ useEffect(() => {
21
+ let mounted = true;
22
+ const initAuth = async () => {
23
+ try {
24
+ if (typeof window !== "undefined") {
25
+ const savedToken = localStorage.getItem("ur_auth_token");
26
+ if (savedToken) auth.setToken(savedToken);
27
+ }
28
+ const urlParams = new URLSearchParams(window.location.search);
29
+ const hashParams = new URLSearchParams(window.location.hash.substring(1));
30
+ const token = hashParams.get("token");
31
+ const rtCode = urlParams.get("rtCode");
32
+ const error2 = urlParams.get("error");
33
+ if (error2) {
34
+ console.error("Social Auth Error:", error2);
35
+ if (mounted) setError(error2);
36
+ window.history.replaceState({}, document.title, window.location.pathname);
37
+ } else if (token) {
38
+ auth.setToken(token);
39
+ if (typeof window !== "undefined") localStorage.setItem("ur_auth_token", token);
40
+ if (rtCode) {
41
+ try {
42
+ const exRes = await auth.socialExchange({ token, rtCode });
43
+ const exToken = exRes.accessToken || exRes.token;
44
+ if (exToken && typeof window !== "undefined") localStorage.setItem("ur_auth_token", exToken);
45
+ } catch (err) {
46
+ console.error("Failed to exchange refresh token", err);
47
+ if (mounted) setError(err.message || "Failed to complete social login");
48
+ throw err;
49
+ }
50
+ }
51
+ window.history.replaceState({}, document.title, window.location.pathname);
52
+ } else {
53
+ try {
54
+ const res = await auth.refreshToken();
55
+ const newToken = res.accessToken || res.token;
56
+ if (newToken && typeof window !== "undefined") localStorage.setItem("ur_auth_token", newToken);
57
+ } catch (e) {
58
+ }
59
+ }
60
+ const currentUser = await auth.me();
61
+ if (mounted) {
62
+ setUser(currentUser);
63
+ }
64
+ } catch (error2) {
65
+ if (mounted) {
66
+ setUser(null);
67
+ }
68
+ } finally {
69
+ if (mounted) {
70
+ setIsInitializing(false);
71
+ }
72
+ }
73
+ };
74
+ initAuth();
75
+ return () => {
76
+ mounted = false;
77
+ };
78
+ }, [auth]);
79
+ const value = {
80
+ client,
81
+ auth,
82
+ db,
83
+ storage,
84
+ user,
85
+ setUser,
86
+ isInitializing,
87
+ isLoading,
88
+ setIsLoading,
89
+ error,
90
+ setError
91
+ };
92
+ return /* @__PURE__ */ jsx(UrContext.Provider, { value, children });
93
+ };
94
+ var useUrContext = () => {
95
+ const context = useContext(UrContext);
96
+ if (!context) {
97
+ throw new Error("useUrContext must be used within an UrProvider");
98
+ }
99
+ return context;
100
+ };
101
+
102
+ // src/hooks.ts
103
+ import { useCallback } from "react";
104
+ var useAuth = () => {
105
+ const { auth, user, setUser, isInitializing, isLoading, setIsLoading, error, setError } = useUrContext();
106
+ if (!auth) {
107
+ throw new Error("Auth module not initialized. Make sure you are inside UrProvider.");
108
+ }
109
+ const login = useCallback(async (payload) => {
110
+ try {
111
+ setError(null);
112
+ setIsLoading(true);
113
+ const res = await auth.login(payload);
114
+ const token = res.accessToken || res.token;
115
+ if (token && typeof window !== "undefined") localStorage.setItem("ur_auth_token", token);
116
+ const currentUser = await auth.me();
117
+ setUser(currentUser);
118
+ } catch (err) {
119
+ setError(err.message || "Login failed");
120
+ throw err;
121
+ } finally {
122
+ setIsLoading(false);
123
+ }
124
+ }, [auth, setUser, setIsLoading, setError]);
125
+ const signUp = useCallback(async (payload) => {
126
+ try {
127
+ setError(null);
128
+ setIsLoading(true);
129
+ const newUser = await auth.signUp(payload);
130
+ return newUser;
131
+ } catch (err) {
132
+ setError(err.message || "Sign up failed");
133
+ throw err;
134
+ } finally {
135
+ setIsLoading(false);
136
+ }
137
+ }, [auth, setIsLoading, setError]);
138
+ const logout = useCallback(async () => {
139
+ try {
140
+ setError(null);
141
+ setIsLoading(true);
142
+ await auth.logout();
143
+ if (typeof window !== "undefined") localStorage.removeItem("ur_auth_token");
144
+ setUser(null);
145
+ } catch (err) {
146
+ setError(err.message || "Logout failed");
147
+ throw err;
148
+ } finally {
149
+ setIsLoading(false);
150
+ }
151
+ }, [auth, setUser, setIsLoading, setError]);
152
+ const socialLogin = useCallback((provider) => {
153
+ setError(null);
154
+ const url = auth.socialStart(provider);
155
+ window.location.href = url;
156
+ }, [auth, setError]);
157
+ const verifyEmail = useCallback(async (payload) => {
158
+ try {
159
+ setError(null);
160
+ return await auth.verifyEmail(payload);
161
+ } catch (err) {
162
+ setError(err.message || "Email verification failed");
163
+ throw err;
164
+ }
165
+ }, [auth, setError]);
166
+ const changePassword = useCallback(async (payload) => {
167
+ try {
168
+ setError(null);
169
+ return await auth.changePassword(payload);
170
+ } catch (err) {
171
+ setError(err.message || "Failed to change password");
172
+ throw err;
173
+ }
174
+ }, [auth, setError]);
175
+ const requestPasswordReset = useCallback(async (payload) => {
176
+ try {
177
+ setError(null);
178
+ setIsLoading(true);
179
+ return await auth.requestPasswordReset(payload);
180
+ } catch (err) {
181
+ setError(err.message || "Failed to request password reset");
182
+ throw err;
183
+ } finally {
184
+ setIsLoading(false);
185
+ }
186
+ }, [auth, setError, setIsLoading]);
187
+ const resetPassword = useCallback(async (payload) => {
188
+ try {
189
+ setError(null);
190
+ setIsLoading(true);
191
+ return await auth.resetPassword(payload);
192
+ } catch (err) {
193
+ setError(err.message || "Failed to reset password");
194
+ throw err;
195
+ } finally {
196
+ setIsLoading(false);
197
+ }
198
+ }, [auth, setError, setIsLoading]);
199
+ const clearError = useCallback(() => setError(null), [setError]);
200
+ return {
201
+ user,
202
+ isInitializing,
203
+ isLoading,
204
+ error,
205
+ isAuthenticated: !!user,
206
+ login,
207
+ signUp,
208
+ logout,
209
+ socialLogin,
210
+ verifyEmail,
211
+ changePassword,
212
+ requestPasswordReset,
213
+ resetPassword,
214
+ clearError,
215
+ authApi: auth
216
+ // Escape hatch to underlying SDK
217
+ };
218
+ };
219
+ var useUser = () => {
220
+ const { user, isInitializing, isLoading, error } = useUrContext();
221
+ return {
222
+ user,
223
+ isInitializing,
224
+ isLoading,
225
+ error,
226
+ isAuthenticated: !!user
227
+ };
228
+ };
229
+ var useDb = () => {
230
+ const { db } = useUrContext();
231
+ if (!db) {
232
+ throw new Error("Database module not initialized.");
233
+ }
234
+ return db;
235
+ };
236
+ var useStorage = () => {
237
+ const { storage } = useUrContext();
238
+ if (!storage) {
239
+ throw new Error("Storage module not initialized.");
240
+ }
241
+ return storage;
242
+ };
243
+
244
+ // src/components.tsx
245
+ import { useEffect as useEffect2 } from "react";
246
+ import { Fragment, jsx as jsx2 } from "react/jsx-runtime";
247
+ var ProtectedRoute = ({
248
+ children,
249
+ redirectTo = "/login",
250
+ fallback = null,
251
+ onRedirect
252
+ }) => {
253
+ const { isAuthenticated, isInitializing } = useUser();
254
+ useEffect2(() => {
255
+ if (!isInitializing && !isAuthenticated) {
256
+ if (onRedirect) {
257
+ onRedirect();
258
+ } else if (typeof window !== "undefined") {
259
+ window.location.href = redirectTo;
260
+ }
261
+ }
262
+ }, [isAuthenticated, isInitializing, redirectTo, onRedirect]);
263
+ if (isInitializing) {
264
+ return fallback;
265
+ }
266
+ if (!isAuthenticated) {
267
+ return fallback;
268
+ }
269
+ return /* @__PURE__ */ jsx2(Fragment, { children });
270
+ };
271
+ var GuestRoute = ({
272
+ children,
273
+ redirectTo = "/dashboard",
274
+ fallback = null,
275
+ onRedirect
276
+ }) => {
277
+ const { isAuthenticated, isInitializing } = useUser();
278
+ useEffect2(() => {
279
+ if (!isInitializing && isAuthenticated) {
280
+ if (onRedirect) {
281
+ onRedirect();
282
+ } else if (typeof window !== "undefined") {
283
+ window.location.href = redirectTo;
284
+ }
285
+ }
286
+ }, [isAuthenticated, isInitializing, redirectTo, onRedirect]);
287
+ if (isInitializing) {
288
+ return fallback;
289
+ }
290
+ if (isAuthenticated) {
291
+ return fallback;
292
+ }
293
+ return /* @__PURE__ */ jsx2(Fragment, { children });
294
+ };
295
+
296
+ // src/components/UrAuth.tsx
297
+ import { useState as useState3, useEffect as useEffect4 } from "react";
298
+
299
+ // src/components/Toast.tsx
300
+ import { useEffect as useEffect3, useState as useState2 } from "react";
301
+ import { Fragment as Fragment2, jsx as jsx3, jsxs } from "react/jsx-runtime";
302
+ var Toast = ({ message, type, onClose, isDark = false }) => {
303
+ const [isVisible, setIsVisible] = useState2(false);
304
+ const [isLeaving, setIsLeaving] = useState2(false);
305
+ useEffect3(() => {
306
+ requestAnimationFrame(() => {
307
+ setIsVisible(true);
308
+ });
309
+ let innerTimer;
310
+ const timer = setTimeout(() => {
311
+ setIsLeaving(true);
312
+ innerTimer = setTimeout(onClose, 300);
313
+ }, 4e3);
314
+ return () => {
315
+ clearTimeout(timer);
316
+ if (innerTimer) clearTimeout(innerTimer);
317
+ };
318
+ }, [onClose]);
319
+ const bgColor = isDark ? "rgba(30, 30, 30, 0.9)" : "rgba(255, 255, 255, 0.9)";
320
+ const borderColor = type === "success" ? "rgba(34, 197, 94, 0.5)" : "rgba(239, 68, 68, 0.5)";
321
+ const iconColor = type === "success" ? "#22c55e" : "#ef4444";
322
+ const textColor = isDark ? "#fff" : "#000";
323
+ return /* @__PURE__ */ jsxs(Fragment2, { children: [
324
+ /* @__PURE__ */ jsx3("style", { children: `
325
+ @keyframes slideIn {
326
+ from { transform: translate(-50%, -20px) scale(0.95); opacity: 0; }
327
+ to { transform: translate(-50%, 0) scale(1); opacity: 1; }
328
+ }
329
+ @keyframes slideOut {
330
+ from { transform: translate(-50%, 0) scale(1); opacity: 1; }
331
+ to { transform: translate(-50%, -20px) scale(0.95); opacity: 0; }
332
+ }
333
+ ` }),
334
+ /* @__PURE__ */ jsxs(
335
+ "div",
336
+ {
337
+ style: {
338
+ position: "fixed",
339
+ top: "24px",
340
+ left: "50%",
341
+ transform: "translateX(-50%)",
342
+ zIndex: 9999,
343
+ display: "flex",
344
+ alignItems: "center",
345
+ gap: "12px",
346
+ padding: "12px 20px",
347
+ borderRadius: "0",
348
+ background: bgColor,
349
+ backdropFilter: "blur(16px)",
350
+ WebkitBackdropFilter: "blur(16px)",
351
+ border: `1px solid ${borderColor}`,
352
+ boxShadow: "0 8px 32px rgba(0,0,0,0.12)",
353
+ color: textColor,
354
+ fontFamily: "system-ui, -apple-system, sans-serif",
355
+ fontSize: "14px",
356
+ fontWeight: 500,
357
+ animation: isLeaving ? "slideOut 0.3s cubic-bezier(0.4, 0, 0.2, 1) forwards" : "slideIn 0.4s cubic-bezier(0.16, 1, 0.3, 1) forwards"
358
+ },
359
+ children: [
360
+ type === "success" ? /* @__PURE__ */ jsxs("svg", { width: "20", height: "20", viewBox: "0 0 24 24", fill: "none", stroke: iconColor, strokeWidth: "2.5", strokeLinecap: "round", strokeLinejoin: "round", children: [
361
+ /* @__PURE__ */ jsx3("path", { d: "M22 11.08V12a10 10 0 1 1-5.93-9.14" }),
362
+ /* @__PURE__ */ jsx3("polyline", { points: "22 4 12 14.01 9 11.01" })
363
+ ] }) : /* @__PURE__ */ jsxs("svg", { width: "20", height: "20", viewBox: "0 0 24 24", fill: "none", stroke: iconColor, strokeWidth: "2.5", strokeLinecap: "round", strokeLinejoin: "round", children: [
364
+ /* @__PURE__ */ jsx3("circle", { cx: "12", cy: "12", r: "10" }),
365
+ /* @__PURE__ */ jsx3("line", { x1: "12", y1: "8", x2: "12", y2: "12" }),
366
+ /* @__PURE__ */ jsx3("line", { x1: "12", y1: "16", x2: "12.01", y2: "16" })
367
+ ] }),
368
+ message
369
+ ]
370
+ }
371
+ )
372
+ ] });
373
+ };
374
+
375
+ // src/components/UrAuth.tsx
376
+ import { Fragment as Fragment3, jsx as jsx4, jsxs as jsxs2 } from "react/jsx-runtime";
377
+ var UrAuth = ({
378
+ providers = ["google", "github"],
379
+ theme = "light",
380
+ onSuccess
381
+ }) => {
382
+ const { login, signUp, socialLogin, requestPasswordReset, resetPassword, isLoading, error, clearError } = useAuth();
383
+ const [mode, setMode] = useState3("signin");
384
+ const [email, setEmail] = useState3("");
385
+ const [password, setPassword] = useState3("");
386
+ const [otp, setOtp] = useState3("");
387
+ const [name, setName] = useState3("");
388
+ const [toast, setToast] = useState3(null);
389
+ useEffect4(() => {
390
+ if (error) {
391
+ setToast({ message: error, type: "error" });
392
+ }
393
+ }, [error]);
394
+ const handleSubmit = async (e) => {
395
+ e.preventDefault();
396
+ try {
397
+ if (mode === "signin") {
398
+ await login({ email, password });
399
+ setToast({ message: "Welcome back!", type: "success" });
400
+ if (onSuccess) onSuccess();
401
+ } else if (mode === "signup") {
402
+ await signUp({ email, password, name });
403
+ await login({ email, password });
404
+ setToast({ message: "Account created successfully!", type: "success" });
405
+ if (onSuccess) onSuccess();
406
+ } else if (mode === "forgot") {
407
+ await requestPasswordReset({ email });
408
+ setToast({ message: "Reset code sent to your email", type: "success" });
409
+ setMode("reset");
410
+ } else if (mode === "reset") {
411
+ await resetPassword({ email, otp, newPassword: password });
412
+ setToast({ message: "Password reset successfully", type: "success" });
413
+ setMode("signin");
414
+ setPassword("");
415
+ setOtp("");
416
+ }
417
+ } catch (err) {
418
+ }
419
+ };
420
+ const isDark = theme === "dark";
421
+ const bg = isDark ? "#1a1a1a" : "#ffffff";
422
+ const text = isDark ? "#ffffff" : "#0f172a";
423
+ const textMuted = isDark ? "#a1a1aa" : "#64748b";
424
+ const border = isDark ? "#333" : "#e2e8f0";
425
+ const inputBg = isDark ? "#2a2a2a" : "#ffffff";
426
+ const styles = {
427
+ wrapper: {
428
+ width: "100%",
429
+ maxWidth: "420px",
430
+ margin: "0 auto",
431
+ borderRadius: "0",
432
+ background: bg,
433
+ boxShadow: isDark ? "0 20px 40px rgba(0,0,0,0.5)" : "0 20px 40px rgba(0,0,0,0.06), 0 1px 3px rgba(0,0,0,0.05)",
434
+ border: `1px solid ${border}`,
435
+ overflow: "hidden",
436
+ fontFamily: 'system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif',
437
+ color: text
438
+ },
439
+ body: {
440
+ padding: "32px 32px 24px 32px"
441
+ },
442
+ switcherContainer: {
443
+ display: "flex",
444
+ alignItems: "center",
445
+ justifyContent: "center",
446
+ marginBottom: "32px"
447
+ },
448
+ switcher: {
449
+ display: "inline-flex",
450
+ background: isDark ? "#2a2a2a" : "#f1f5f9",
451
+ padding: "4px",
452
+ borderRadius: "0"
453
+ },
454
+ switchBtn: (active) => ({
455
+ display: "flex",
456
+ alignItems: "center",
457
+ gap: "6px",
458
+ padding: "8px 20px",
459
+ borderRadius: "0",
460
+ fontSize: "13px",
461
+ fontWeight: 600,
462
+ cursor: "pointer",
463
+ color: active ? text : textMuted,
464
+ background: active ? isDark ? "#444" : "#ffffff" : "transparent",
465
+ boxShadow: active ? isDark ? "0 2px 4px rgba(0,0,0,0.2)" : "0 2px 8px rgba(0,0,0,0.05)" : "none",
466
+ border: "none",
467
+ transition: "all 0.2s ease"
468
+ }),
469
+ field: {
470
+ marginBottom: "20px"
471
+ },
472
+ labelRow: {
473
+ display: "flex",
474
+ justifyContent: "space-between",
475
+ alignItems: "center",
476
+ marginBottom: "8px"
477
+ },
478
+ label: {
479
+ fontSize: "13px",
480
+ fontWeight: 600,
481
+ color: isDark ? "#ddd" : "#334155"
482
+ },
483
+ forgotLink: {
484
+ fontSize: "12px",
485
+ fontWeight: 600,
486
+ color: text,
487
+ cursor: "pointer",
488
+ textDecoration: "none",
489
+ background: "none",
490
+ border: "none",
491
+ padding: 0
492
+ },
493
+ input: {
494
+ width: "100%",
495
+ padding: "12px 16px",
496
+ borderRadius: "0",
497
+ border: `1px solid ${border}`,
498
+ background: inputBg,
499
+ color: text,
500
+ fontSize: "14px",
501
+ boxSizing: "border-box",
502
+ outline: "none",
503
+ transition: "border-color 0.2s ease"
504
+ },
505
+ primaryBtn: {
506
+ width: "100%",
507
+ padding: "14px",
508
+ borderRadius: "0",
509
+ background: "linear-gradient(180deg, #2a2a2a 0%, #111111 100%)",
510
+ color: "#ffffff",
511
+ fontSize: "15px",
512
+ fontWeight: 600,
513
+ border: "none",
514
+ boxShadow: "0 4px 12px rgba(0,0,0,0.15)",
515
+ cursor: "pointer",
516
+ marginTop: "8px",
517
+ transition: "transform 0.1s ease"
518
+ },
519
+ divider: {
520
+ display: "flex",
521
+ alignItems: "center",
522
+ margin: "24px 0",
523
+ color: "#94a3b8",
524
+ fontSize: "11px",
525
+ fontWeight: 600,
526
+ letterSpacing: "1px"
527
+ },
528
+ dividerLine: {
529
+ flex: 1,
530
+ height: "1px",
531
+ background: border
532
+ },
533
+ dividerText: {
534
+ padding: "0 12px"
535
+ },
536
+ socialBtn: {
537
+ width: "100%",
538
+ padding: "12px",
539
+ borderRadius: "0",
540
+ border: `1px solid ${border}`,
541
+ background: isDark ? "#2a2a2a" : "#ffffff",
542
+ color: text,
543
+ fontSize: "14px",
544
+ fontWeight: 600,
545
+ display: "flex",
546
+ alignItems: "center",
547
+ justifyContent: "center",
548
+ gap: "10px",
549
+ marginBottom: "12px",
550
+ cursor: "pointer",
551
+ boxShadow: isDark ? "none" : "0 1px 2px rgba(0,0,0,0.02)",
552
+ transition: "background 0.2s ease"
553
+ },
554
+ footer: {
555
+ background: isDark ? "#222" : "#f8fafc",
556
+ padding: "24px",
557
+ textAlign: "center",
558
+ borderTop: `1px solid ${border}`,
559
+ fontSize: "13px",
560
+ color: textMuted
561
+ },
562
+ footerLink: {
563
+ color: text,
564
+ fontWeight: 600,
565
+ textDecoration: "underline",
566
+ cursor: "pointer",
567
+ marginLeft: "4px",
568
+ background: "none",
569
+ border: "none",
570
+ padding: 0
571
+ }
572
+ };
573
+ const GoogleIcon = () => /* @__PURE__ */ jsxs2("svg", { width: "18", height: "18", viewBox: "0 0 24 24", fill: "none", children: [
574
+ /* @__PURE__ */ jsx4("path", { d: "M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92c-.26 1.37-1.04 2.53-2.21 3.31v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.09z", fill: "#4285F4" }),
575
+ /* @__PURE__ */ jsx4("path", { d: "M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z", fill: "#34A853" }),
576
+ /* @__PURE__ */ jsx4("path", { d: "M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z", fill: "#FBBC05" }),
577
+ /* @__PURE__ */ jsx4("path", { d: "M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z", fill: "#EA4335" })
578
+ ] });
579
+ const GithubIcon = () => /* @__PURE__ */ jsx4("svg", { width: "18", height: "18", viewBox: "0 0 24 24", fill: isDark ? "#fff" : "#000", children: /* @__PURE__ */ jsx4("path", { d: "M12 2C6.477 2 2 6.484 2 12.017c0 4.425 2.865 8.18 6.839 9.504.5.092.682-.217.682-.483 0-.237-.008-.868-.013-1.703-2.782.605-3.369-1.343-3.369-1.343-.454-1.158-1.11-1.466-1.11-1.466-.908-.62.069-.608.069-.608 1.003.07 1.531 1.032 1.531 1.032.892 1.53 2.341 1.088 2.91.832.092-.647.35-1.088.636-1.338-2.22-.253-4.555-1.113-4.555-4.951 0-1.093.39-1.988 1.029-2.688-.103-.253-.446-1.272.098-2.65 0 0 .84-.27 2.75 1.026A9.564 9.564 0 0112 6.844c.85.004 1.705.115 2.504.337 1.909-1.296 2.747-1.027 2.747-1.027.546 1.379.202 2.398.1 2.651.64.7 1.028 1.595 1.028 2.688 0 3.848-2.339 4.695-4.566 4.943.359.309.678.92.678 1.855 0 1.338-.012 2.419-.012 2.747 0 .268.18.58.688.482A10.019 10.019 0 0022 12.017C22 6.484 17.522 2 12 2z" }) });
580
+ return /* @__PURE__ */ jsxs2("div", { style: styles.wrapper, children: [
581
+ toast && /* @__PURE__ */ jsx4(
582
+ Toast,
583
+ {
584
+ message: toast.message,
585
+ type: toast.type,
586
+ isDark,
587
+ onClose: () => {
588
+ setToast(null);
589
+ if (toast.type === "error") clearError();
590
+ }
591
+ }
592
+ ),
593
+ /* @__PURE__ */ jsxs2("div", { style: styles.body, children: [
594
+ (mode === "signin" || mode === "signup") && /* @__PURE__ */ jsx4("div", { style: styles.switcherContainer, children: /* @__PURE__ */ jsxs2("div", { style: styles.switcher, children: [
595
+ /* @__PURE__ */ jsxs2(
596
+ "button",
597
+ {
598
+ type: "button",
599
+ style: styles.switchBtn(mode === "signin"),
600
+ onClick: () => {
601
+ setMode("signin");
602
+ clearError();
603
+ },
604
+ children: [
605
+ /* @__PURE__ */ jsxs2("svg", { width: "14", height: "14", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2.5", strokeLinecap: "round", strokeLinejoin: "round", children: [
606
+ /* @__PURE__ */ jsx4("path", { d: "M15 3h4a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2h-4" }),
607
+ /* @__PURE__ */ jsx4("polyline", { points: "10 17 15 12 10 7" }),
608
+ /* @__PURE__ */ jsx4("line", { x1: "15", y1: "12", x2: "3", y2: "12" })
609
+ ] }),
610
+ "Login"
611
+ ]
612
+ }
613
+ ),
614
+ /* @__PURE__ */ jsxs2(
615
+ "button",
616
+ {
617
+ type: "button",
618
+ style: styles.switchBtn(mode === "signup"),
619
+ onClick: () => {
620
+ setMode("signup");
621
+ clearError();
622
+ },
623
+ children: [
624
+ /* @__PURE__ */ jsxs2("svg", { width: "14", height: "14", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2.5", strokeLinecap: "round", strokeLinejoin: "round", children: [
625
+ /* @__PURE__ */ jsx4("path", { d: "M16 21v-2a4 4 0 0 0-4-4H6a4 4 0 0 0-4 4v2" }),
626
+ /* @__PURE__ */ jsx4("circle", { cx: "9", cy: "7", r: "4" }),
627
+ /* @__PURE__ */ jsx4("line", { x1: "19", y1: "8", x2: "19", y2: "14" }),
628
+ /* @__PURE__ */ jsx4("line", { x1: "22", y1: "11", x2: "16", y2: "11" })
629
+ ] }),
630
+ "Sign Up"
631
+ ]
632
+ }
633
+ )
634
+ ] }) }),
635
+ (mode === "forgot" || mode === "reset") && /* @__PURE__ */ jsxs2("div", { style: { marginBottom: "24px", textAlign: "center" }, children: [
636
+ /* @__PURE__ */ jsx4("h2", { style: { margin: "0 0 8px", fontSize: "20px", fontWeight: 700, color: text }, children: mode === "forgot" ? "Reset Password" : "Enter Reset Code" }),
637
+ /* @__PURE__ */ jsx4("p", { style: { margin: 0, fontSize: "14px", color: textMuted }, children: mode === "forgot" ? "Enter your email and we'll send a code" : `Enter the code sent to ${email}` })
638
+ ] }),
639
+ /* @__PURE__ */ jsxs2("form", { onSubmit: handleSubmit, children: [
640
+ mode === "signup" && /* @__PURE__ */ jsxs2("div", { style: styles.field, children: [
641
+ /* @__PURE__ */ jsx4("div", { style: styles.labelRow, children: /* @__PURE__ */ jsx4("label", { style: styles.label, children: "Full Name" }) }),
642
+ /* @__PURE__ */ jsx4(
643
+ "input",
644
+ {
645
+ style: styles.input,
646
+ type: "text",
647
+ placeholder: "Enter your name",
648
+ value: name,
649
+ onChange: (e) => setName(e.target.value),
650
+ required: true
651
+ }
652
+ )
653
+ ] }),
654
+ /* @__PURE__ */ jsxs2("div", { style: styles.field, children: [
655
+ /* @__PURE__ */ jsx4("div", { style: styles.labelRow, children: /* @__PURE__ */ jsx4("label", { style: styles.label, children: "Email address" }) }),
656
+ /* @__PURE__ */ jsx4(
657
+ "input",
658
+ {
659
+ style: styles.input,
660
+ type: "email",
661
+ placeholder: "Enter your email address",
662
+ value: email,
663
+ onChange: (e) => setEmail(e.target.value),
664
+ required: true,
665
+ readOnly: mode === "reset"
666
+ }
667
+ )
668
+ ] }),
669
+ mode === "reset" && /* @__PURE__ */ jsxs2("div", { style: styles.field, children: [
670
+ /* @__PURE__ */ jsx4("div", { style: styles.labelRow, children: /* @__PURE__ */ jsx4("label", { style: styles.label, children: "6-digit OTP Code" }) }),
671
+ /* @__PURE__ */ jsx4(
672
+ "input",
673
+ {
674
+ style: styles.input,
675
+ type: "text",
676
+ placeholder: "Enter reset code",
677
+ value: otp,
678
+ onChange: (e) => setOtp(e.target.value),
679
+ required: true
680
+ }
681
+ )
682
+ ] }),
683
+ (mode === "signin" || mode === "signup" || mode === "reset") && /* @__PURE__ */ jsxs2("div", { style: styles.field, children: [
684
+ /* @__PURE__ */ jsxs2("div", { style: styles.labelRow, children: [
685
+ /* @__PURE__ */ jsx4("label", { style: styles.label, children: mode === "reset" ? "New Password" : "Password" }),
686
+ mode === "signin" && /* @__PURE__ */ jsx4("button", { type: "button", style: styles.forgotLink, onClick: () => {
687
+ setMode("forgot");
688
+ clearError();
689
+ }, children: "Forgot password?" })
690
+ ] }),
691
+ /* @__PURE__ */ jsx4(
692
+ "input",
693
+ {
694
+ style: styles.input,
695
+ type: "password",
696
+ placeholder: mode === "reset" ? "Enter new password" : "Enter your password",
697
+ value: password,
698
+ onChange: (e) => setPassword(e.target.value),
699
+ required: true
700
+ }
701
+ )
702
+ ] }),
703
+ /* @__PURE__ */ jsx4(
704
+ "button",
705
+ {
706
+ style: styles.primaryBtn,
707
+ type: "submit",
708
+ disabled: isLoading,
709
+ onMouseDown: (e) => e.currentTarget.style.transform = "scale(0.98)",
710
+ onMouseUp: (e) => e.currentTarget.style.transform = "scale(1)",
711
+ onMouseLeave: (e) => e.currentTarget.style.transform = "scale(1)",
712
+ children: isLoading ? "Processing..." : mode === "signin" ? "Log In" : mode === "signup" ? "Create Account" : mode === "forgot" ? "Send Reset Code" : "Reset Password"
713
+ }
714
+ )
715
+ ] }),
716
+ (mode === "signin" || mode === "signup") && providers && providers.length > 0 && /* @__PURE__ */ jsxs2(Fragment3, { children: [
717
+ /* @__PURE__ */ jsxs2("div", { style: styles.divider, children: [
718
+ /* @__PURE__ */ jsx4("div", { style: styles.dividerLine }),
719
+ /* @__PURE__ */ jsx4("span", { style: styles.dividerText, children: "OR" }),
720
+ /* @__PURE__ */ jsx4("div", { style: styles.dividerLine })
721
+ ] }),
722
+ /* @__PURE__ */ jsxs2("div", { children: [
723
+ providers.includes("google") && /* @__PURE__ */ jsxs2("button", { style: styles.socialBtn, onClick: () => socialLogin("google"), type: "button", children: [
724
+ /* @__PURE__ */ jsx4(GoogleIcon, {}),
725
+ "Continue with Google"
726
+ ] }),
727
+ providers.includes("github") && /* @__PURE__ */ jsxs2("button", { style: styles.socialBtn, onClick: () => socialLogin("github"), type: "button", children: [
728
+ /* @__PURE__ */ jsx4(GithubIcon, {}),
729
+ "Continue with GitHub"
730
+ ] })
731
+ ] })
732
+ ] })
733
+ ] }),
734
+ /* @__PURE__ */ jsxs2("div", { style: styles.footer, children: [
735
+ mode === "signin" ? "Don't have an account yet?" : mode === "signup" ? "Already have an account?" : "Remember your password?",
736
+ /* @__PURE__ */ jsx4(
737
+ "button",
738
+ {
739
+ type: "button",
740
+ style: styles.footerLink,
741
+ onClick: () => {
742
+ setMode(mode === "signin" ? "signup" : "signin");
743
+ clearError();
744
+ },
745
+ children: mode === "signin" ? "Sign up" : "Log in"
746
+ }
747
+ )
748
+ ] })
749
+ ] });
750
+ };
751
+
752
+ // src/components/UrUserButton.tsx
753
+ import { useState as useState4, useRef, useEffect as useEffect5 } from "react";
754
+ import { jsx as jsx5, jsxs as jsxs3 } from "react/jsx-runtime";
755
+ var UrUserButton = ({
756
+ shape = "square",
757
+ position = "top-right",
758
+ onProfileClick,
759
+ onSettingsClick,
760
+ zIndex = 999
761
+ }) => {
762
+ const { user } = useUser();
763
+ const { logout } = useAuth();
764
+ const [isOpen, setIsOpen] = useState4(false);
765
+ const containerRef = useRef(null);
766
+ useEffect5(() => {
767
+ const handleClickOutside = (event) => {
768
+ if (containerRef.current && !containerRef.current.contains(event.target)) {
769
+ setIsOpen(false);
770
+ }
771
+ };
772
+ document.addEventListener("mousedown", handleClickOutside);
773
+ return () => document.removeEventListener("mousedown", handleClickOutside);
774
+ }, []);
775
+ if (!user) return null;
776
+ const borderRadius = shape === "circle" ? "50%" : "0px";
777
+ const isFixed = position !== "inline";
778
+ const positionStyles = isFixed ? {
779
+ position: "fixed",
780
+ zIndex,
781
+ top: position.includes("top") ? "24px" : "auto",
782
+ bottom: position.includes("bottom") ? "24px" : "auto",
783
+ right: position.includes("right") ? "24px" : "auto",
784
+ left: position.includes("left") ? "24px" : "auto"
785
+ } : { position: "relative" };
786
+ const dropdownStyles = {
787
+ position: "absolute",
788
+ top: position.includes("top") || position === "inline" ? "calc(100% + 8px)" : "auto",
789
+ bottom: position.includes("bottom") ? "calc(100% + 8px)" : "auto",
790
+ right: position.includes("right") || position === "inline" ? "0" : "auto",
791
+ left: position.includes("left") ? "0" : "auto",
792
+ background: "#ffffff",
793
+ border: "1px solid #e2e8f0",
794
+ borderRadius: "0px",
795
+ boxShadow: "0 10px 25px rgba(0,0,0,0.1)",
796
+ width: "220px",
797
+ display: isOpen ? "block" : "none",
798
+ overflow: "hidden",
799
+ fontFamily: "system-ui, -apple-system, sans-serif"
800
+ };
801
+ const getInitials = () => {
802
+ return user.name?.[0]?.toUpperCase() || user.email?.[0]?.toUpperCase() || "U";
803
+ };
804
+ return /* @__PURE__ */ jsxs3("div", { ref: containerRef, style: positionStyles, children: [
805
+ /* @__PURE__ */ jsx5(
806
+ "button",
807
+ {
808
+ onClick: () => setIsOpen(!isOpen),
809
+ style: {
810
+ width: "40px",
811
+ height: "40px",
812
+ padding: 0,
813
+ border: "1px solid #e2e8f0",
814
+ background: "#f8fafc",
815
+ borderRadius,
816
+ cursor: "pointer",
817
+ display: "flex",
818
+ alignItems: "center",
819
+ justifyContent: "center",
820
+ overflow: "hidden",
821
+ boxShadow: "0 2px 5px rgba(0,0,0,0.05)",
822
+ transition: "transform 0.1s ease"
823
+ },
824
+ children: user.avatarUrl ? /* @__PURE__ */ jsx5("img", { src: user.avatarUrl, alt: "User", style: { width: "100%", height: "100%", objectFit: "cover" } }) : /* @__PURE__ */ jsx5("span", { style: { fontSize: "16px", fontWeight: 600, color: "#475569" }, children: getInitials() })
825
+ }
826
+ ),
827
+ /* @__PURE__ */ jsxs3("div", { style: dropdownStyles, children: [
828
+ /* @__PURE__ */ jsxs3("div", { style: { padding: "16px", borderBottom: "1px solid #e2e8f0", background: "#f8fafc" }, children: [
829
+ /* @__PURE__ */ jsx5("div", { style: { fontSize: "14px", fontWeight: 600, color: "#0f172a", whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis" }, children: user.name || "User" }),
830
+ /* @__PURE__ */ jsx5("div", { style: { fontSize: "12px", color: "#64748b", whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis", marginTop: "2px" }, children: user.email })
831
+ ] }),
832
+ /* @__PURE__ */ jsxs3("div", { style: { padding: "8px" }, children: [
833
+ onProfileClick && /* @__PURE__ */ jsx5(
834
+ "button",
835
+ {
836
+ onClick: () => {
837
+ setIsOpen(false);
838
+ onProfileClick();
839
+ },
840
+ style: {
841
+ width: "100%",
842
+ textAlign: "left",
843
+ padding: "10px 12px",
844
+ background: "transparent",
845
+ border: "none",
846
+ fontSize: "14px",
847
+ color: "#334155",
848
+ cursor: "pointer",
849
+ borderRadius: "0px",
850
+ display: "block"
851
+ },
852
+ onMouseEnter: (e) => e.currentTarget.style.background = "#f1f5f9",
853
+ onMouseLeave: (e) => e.currentTarget.style.background = "transparent",
854
+ children: "Profile"
855
+ }
856
+ ),
857
+ onSettingsClick && /* @__PURE__ */ jsx5(
858
+ "button",
859
+ {
860
+ onClick: () => {
861
+ setIsOpen(false);
862
+ onSettingsClick();
863
+ },
864
+ style: {
865
+ width: "100%",
866
+ textAlign: "left",
867
+ padding: "10px 12px",
868
+ background: "transparent",
869
+ border: "none",
870
+ fontSize: "14px",
871
+ color: "#334155",
872
+ cursor: "pointer",
873
+ borderRadius: "0px",
874
+ display: "block"
875
+ },
876
+ onMouseEnter: (e) => e.currentTarget.style.background = "#f1f5f9",
877
+ onMouseLeave: (e) => e.currentTarget.style.background = "transparent",
878
+ children: "Settings"
879
+ }
880
+ ),
881
+ /* @__PURE__ */ jsx5("div", { style: { height: "1px", background: "#e2e8f0", margin: "4px 0" } }),
882
+ /* @__PURE__ */ jsx5(
883
+ "button",
884
+ {
885
+ onClick: () => {
886
+ setIsOpen(false);
887
+ logout();
888
+ },
889
+ style: {
890
+ width: "100%",
891
+ textAlign: "left",
892
+ padding: "10px 12px",
893
+ background: "transparent",
894
+ border: "none",
895
+ fontSize: "14px",
896
+ color: "#ef4444",
897
+ fontWeight: 500,
898
+ cursor: "pointer",
899
+ borderRadius: "0px",
900
+ display: "block"
901
+ },
902
+ onMouseEnter: (e) => e.currentTarget.style.background = "#fef2f2",
903
+ onMouseLeave: (e) => e.currentTarget.style.background = "transparent",
904
+ children: "Logout"
905
+ }
906
+ )
907
+ ] })
908
+ ] })
909
+ ] });
910
+ };
911
+
912
+ // src/index.ts
913
+ export * from "@urbackend/sdk";
914
+ export {
915
+ GuestRoute,
916
+ ProtectedRoute,
917
+ UrAuth,
918
+ UrProvider,
919
+ UrUserButton,
920
+ useAuth,
921
+ useDb,
922
+ useStorage,
923
+ useUrContext,
924
+ useUser
925
+ };
926
+ //# sourceMappingURL=index.mjs.map