@rovela-ai/sdk 0.1.0 → 0.1.2
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/admin/hooks/useAdminAuth.d.ts +36 -0
- package/dist/admin/hooks/useAdminAuth.d.ts.map +1 -0
- package/dist/admin/hooks/useAdminAuth.js +118 -0
- package/dist/admin/hooks/useAdminAuth.js.map +1 -0
- package/dist/auth/hooks/useAuth.d.ts +30 -0
- package/dist/auth/hooks/useAuth.d.ts.map +1 -0
- package/dist/auth/hooks/useAuth.js +254 -0
- package/dist/auth/hooks/useAuth.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { UseAdminAuthReturn } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Admin authentication hook for auth state and actions.
|
|
4
|
+
*
|
|
5
|
+
* @returns Admin auth state and methods
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* function AdminDashboard() {
|
|
10
|
+
* const {
|
|
11
|
+
* admin,
|
|
12
|
+
* isAuthenticated,
|
|
13
|
+
* isLoading,
|
|
14
|
+
* isOwner,
|
|
15
|
+
* signIn,
|
|
16
|
+
* signOut,
|
|
17
|
+
* } = useAdminAuth()
|
|
18
|
+
*
|
|
19
|
+
* if (isLoading) return <div>Loading...</div>
|
|
20
|
+
*
|
|
21
|
+
* if (!isAuthenticated) {
|
|
22
|
+
* return <AdminLoginForm />
|
|
23
|
+
* }
|
|
24
|
+
*
|
|
25
|
+
* return (
|
|
26
|
+
* <div>
|
|
27
|
+
* Welcome, {admin?.name}
|
|
28
|
+
* {isOwner && <span>(Owner)</span>}
|
|
29
|
+
* <button onClick={signOut}>Sign Out</button>
|
|
30
|
+
* </div>
|
|
31
|
+
* )
|
|
32
|
+
* }
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export declare function useAdminAuth(): UseAdminAuthReturn;
|
|
36
|
+
//# sourceMappingURL=useAdminAuth.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useAdminAuth.d.ts","sourceRoot":"","sources":["../../../src/admin/hooks/useAdminAuth.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAIV,kBAAkB,EACnB,MAAM,UAAU,CAAA;AAMjB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,wBAAgB,YAAY,IAAI,kBAAkB,CA0EjD"}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
'use client';
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.useAdminAuth = useAdminAuth;
|
|
5
|
+
/**
|
|
6
|
+
* @rovela/sdk/admin/hooks/useAdminAuth
|
|
7
|
+
*
|
|
8
|
+
* Client-side admin authentication hook.
|
|
9
|
+
* Separate from customer auth - uses 'admin-credentials' provider.
|
|
10
|
+
*/
|
|
11
|
+
const react_1 = require("react");
|
|
12
|
+
const react_2 = require("next-auth/react");
|
|
13
|
+
// =============================================================================
|
|
14
|
+
// Hook Implementation
|
|
15
|
+
// =============================================================================
|
|
16
|
+
/**
|
|
17
|
+
* Admin authentication hook for auth state and actions.
|
|
18
|
+
*
|
|
19
|
+
* @returns Admin auth state and methods
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* function AdminDashboard() {
|
|
24
|
+
* const {
|
|
25
|
+
* admin,
|
|
26
|
+
* isAuthenticated,
|
|
27
|
+
* isLoading,
|
|
28
|
+
* isOwner,
|
|
29
|
+
* signIn,
|
|
30
|
+
* signOut,
|
|
31
|
+
* } = useAdminAuth()
|
|
32
|
+
*
|
|
33
|
+
* if (isLoading) return <div>Loading...</div>
|
|
34
|
+
*
|
|
35
|
+
* if (!isAuthenticated) {
|
|
36
|
+
* return <AdminLoginForm />
|
|
37
|
+
* }
|
|
38
|
+
*
|
|
39
|
+
* return (
|
|
40
|
+
* <div>
|
|
41
|
+
* Welcome, {admin?.name}
|
|
42
|
+
* {isOwner && <span>(Owner)</span>}
|
|
43
|
+
* <button onClick={signOut}>Sign Out</button>
|
|
44
|
+
* </div>
|
|
45
|
+
* )
|
|
46
|
+
* }
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
function useAdminAuth() {
|
|
50
|
+
// Defensive handling: useSession() returns undefined if no SessionProvider
|
|
51
|
+
const sessionResult = (0, react_2.useSession)();
|
|
52
|
+
const session = sessionResult?.data ?? null;
|
|
53
|
+
const status = sessionResult?.status ?? 'loading';
|
|
54
|
+
// Memoized admin data
|
|
55
|
+
const admin = (0, react_1.useMemo)(() => {
|
|
56
|
+
if (!session?.user)
|
|
57
|
+
return null;
|
|
58
|
+
// Cast to admin session type
|
|
59
|
+
const user = session.user;
|
|
60
|
+
// Verify this is an admin session (has role field)
|
|
61
|
+
if (!user.role)
|
|
62
|
+
return null;
|
|
63
|
+
return user;
|
|
64
|
+
}, [session?.user]);
|
|
65
|
+
// Derived state
|
|
66
|
+
const isLoading = status === 'loading';
|
|
67
|
+
const isAuthenticated = status === 'authenticated' && !!admin;
|
|
68
|
+
const isOwner = admin?.role === 'owner';
|
|
69
|
+
/**
|
|
70
|
+
* Sign in with email and password.
|
|
71
|
+
* Uses 'admin-credentials' provider (separate from customer auth).
|
|
72
|
+
*/
|
|
73
|
+
const signIn = (0, react_1.useCallback)(async (options) => {
|
|
74
|
+
const { email, password, redirectTo } = options;
|
|
75
|
+
try {
|
|
76
|
+
const result = await (0, react_2.signIn)('admin-credentials', {
|
|
77
|
+
email,
|
|
78
|
+
password,
|
|
79
|
+
redirect: false,
|
|
80
|
+
callbackUrl: redirectTo || '/admin',
|
|
81
|
+
});
|
|
82
|
+
if (result?.error) {
|
|
83
|
+
// Parse error from NextAuth
|
|
84
|
+
return {
|
|
85
|
+
success: false,
|
|
86
|
+
error: result.error.includes('Invalid')
|
|
87
|
+
? 'Invalid email or password'
|
|
88
|
+
: result.error,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
return { success: true };
|
|
92
|
+
}
|
|
93
|
+
catch (error) {
|
|
94
|
+
console.error('[useAdminAuth] Sign in error:', error);
|
|
95
|
+
return {
|
|
96
|
+
success: false,
|
|
97
|
+
error: 'An error occurred during sign in. Please try again.',
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
}, []);
|
|
101
|
+
/**
|
|
102
|
+
* Sign out the current admin.
|
|
103
|
+
*/
|
|
104
|
+
const signOut = (0, react_1.useCallback)(async () => {
|
|
105
|
+
await (0, react_2.signOut)({ redirect: false });
|
|
106
|
+
}, []);
|
|
107
|
+
return {
|
|
108
|
+
// State
|
|
109
|
+
admin,
|
|
110
|
+
isAuthenticated,
|
|
111
|
+
isLoading,
|
|
112
|
+
isOwner,
|
|
113
|
+
// Actions
|
|
114
|
+
signIn,
|
|
115
|
+
signOut,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
//# sourceMappingURL=useAdminAuth.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useAdminAuth.js","sourceRoot":"","sources":["../../../src/admin/hooks/useAdminAuth.ts"],"names":[],"mappings":";AAAA,YAAY,CAAA;;AAuDZ,oCA0EC;AA/HD;;;;;GAKG;AAEH,iCAA4C;AAC5C,2CAAkG;AAQlG,gFAAgF;AAChF,sBAAsB;AACtB,gFAAgF;AAEhF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,SAAgB,YAAY;IAC1B,2EAA2E;IAC3E,MAAM,aAAa,GAAG,IAAA,kBAAU,GAAE,CAAA;IAClC,MAAM,OAAO,GAAG,aAAa,EAAE,IAAI,IAAI,IAAI,CAAA;IAC3C,MAAM,MAAM,GAAG,aAAa,EAAE,MAAM,IAAI,SAAS,CAAA;IAEjD,sBAAsB;IACtB,MAAM,KAAK,GAAG,IAAA,eAAO,EAAC,GAAwB,EAAE;QAC9C,IAAI,CAAC,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O;gBACL,OAAO,EAAE,KAAK;gBACd,KAAK,EAAE,qDAAqD;aAC7D,CAAA;QACH,CAAC;IACH,CAAC,EAAE,EAAE,CAAC,CAAA;IAEN;;OAEG;IACH,MAAM,OAAO,GAAG,IAAA,mBAAW,EAAC,KAAK,IAAmB,EAAE;QACpD,MAAM,IAAA,eAAe,EAAC,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC,CAAA;IAC5C,CAAC,EAAE,EAAE,CAAC,CAAA;IAEN,OAAO;QACL,QAAQ;QACR,KAAK;QACL,eAAe;QACf,SAAS;QACT,OAAO;QAEP,UAAU;QACV,MAAM;QACN,OAAO;KACR,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { UseAuthReturn } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Authentication hook for customer auth state and actions.
|
|
4
|
+
*
|
|
5
|
+
* @returns Auth state and methods
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* function MyComponent() {
|
|
10
|
+
* const {
|
|
11
|
+
* customer,
|
|
12
|
+
* isAuthenticated,
|
|
13
|
+
* isLoading,
|
|
14
|
+
* signIn,
|
|
15
|
+
* signUp,
|
|
16
|
+
* signOut,
|
|
17
|
+
* } = useAuth()
|
|
18
|
+
*
|
|
19
|
+
* if (isLoading) return <div>Loading...</div>
|
|
20
|
+
*
|
|
21
|
+
* if (isAuthenticated) {
|
|
22
|
+
* return <div>Welcome, {customer?.name || customer?.email}</div>
|
|
23
|
+
* }
|
|
24
|
+
*
|
|
25
|
+
* return <button onClick={() => signIn({ email, password })}>Sign In</button>
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare function useAuth(): UseAuthReturn;
|
|
30
|
+
//# sourceMappingURL=useAuth.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useAuth.d.ts","sourceRoot":"","sources":["../../../src/auth/hooks/useAuth.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EACV,aAAa,EAUd,MAAM,UAAU,CAAA;AAMjB;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,OAAO,IAAI,aAAa,CAyOvC"}
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
'use client';
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.useAuth = useAuth;
|
|
5
|
+
/**
|
|
6
|
+
* @rovela/sdk/auth/hooks/useAuth
|
|
7
|
+
*
|
|
8
|
+
* Client-side authentication hook.
|
|
9
|
+
* Wraps NextAuth session with customer-specific functionality.
|
|
10
|
+
*/
|
|
11
|
+
const react_1 = require("react");
|
|
12
|
+
const react_2 = require("next-auth/react");
|
|
13
|
+
// =============================================================================
|
|
14
|
+
// Hook Implementation
|
|
15
|
+
// =============================================================================
|
|
16
|
+
/**
|
|
17
|
+
* Authentication hook for customer auth state and actions.
|
|
18
|
+
*
|
|
19
|
+
* @returns Auth state and methods
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* function MyComponent() {
|
|
24
|
+
* const {
|
|
25
|
+
* customer,
|
|
26
|
+
* isAuthenticated,
|
|
27
|
+
* isLoading,
|
|
28
|
+
* signIn,
|
|
29
|
+
* signUp,
|
|
30
|
+
* signOut,
|
|
31
|
+
* } = useAuth()
|
|
32
|
+
*
|
|
33
|
+
* if (isLoading) return <div>Loading...</div>
|
|
34
|
+
*
|
|
35
|
+
* if (isAuthenticated) {
|
|
36
|
+
* return <div>Welcome, {customer?.name || customer?.email}</div>
|
|
37
|
+
* }
|
|
38
|
+
*
|
|
39
|
+
* return <button onClick={() => signIn({ email, password })}>Sign In</button>
|
|
40
|
+
* }
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
function useAuth() {
|
|
44
|
+
// Defensive handling: useSession() returns undefined if no SessionProvider
|
|
45
|
+
const sessionResult = (0, react_2.useSession)();
|
|
46
|
+
const session = sessionResult?.data ?? null;
|
|
47
|
+
const status = sessionResult?.status ?? 'loading';
|
|
48
|
+
const update = sessionResult?.update;
|
|
49
|
+
// Memoized customer data
|
|
50
|
+
// Use double cast to avoid type conflicts with parent project's type declarations
|
|
51
|
+
const customer = (0, react_1.useMemo)(() => {
|
|
52
|
+
if (!session?.user)
|
|
53
|
+
return null;
|
|
54
|
+
return session.user;
|
|
55
|
+
}, [session?.user]);
|
|
56
|
+
// Derived state
|
|
57
|
+
const isLoading = status === 'loading';
|
|
58
|
+
const isAuthenticated = status === 'authenticated' && !!customer;
|
|
59
|
+
const isEmailVerified = customer?.emailVerified ?? false;
|
|
60
|
+
/**
|
|
61
|
+
* Sign in with email and password.
|
|
62
|
+
*/
|
|
63
|
+
const signIn = (0, react_1.useCallback)(async (options) => {
|
|
64
|
+
const { email, password, rememberMe = false, redirectTo } = options;
|
|
65
|
+
try {
|
|
66
|
+
const result = await (0, react_2.signIn)('credentials', {
|
|
67
|
+
email,
|
|
68
|
+
password,
|
|
69
|
+
rememberMe: rememberMe.toString(),
|
|
70
|
+
redirect: false,
|
|
71
|
+
callbackUrl: redirectTo,
|
|
72
|
+
});
|
|
73
|
+
if (result?.error) {
|
|
74
|
+
// Parse error from NextAuth
|
|
75
|
+
const error = result.error;
|
|
76
|
+
// Check for specific error codes
|
|
77
|
+
if (error.includes('EMAIL_NOT_VERIFIED') || error.includes('verify your email')) {
|
|
78
|
+
return {
|
|
79
|
+
success: false,
|
|
80
|
+
error: 'Please verify your email before signing in',
|
|
81
|
+
requiresVerification: true,
|
|
82
|
+
email,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
return {
|
|
86
|
+
success: false,
|
|
87
|
+
error: error.includes('Invalid') ? 'Invalid email or password' : error,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
// Trigger session update
|
|
91
|
+
if (update) {
|
|
92
|
+
await update();
|
|
93
|
+
}
|
|
94
|
+
return { success: true };
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
console.error('[useAuth] Sign in error:', error);
|
|
98
|
+
return {
|
|
99
|
+
success: false,
|
|
100
|
+
error: 'An error occurred during sign in. Please try again.',
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
}, [update]);
|
|
104
|
+
/**
|
|
105
|
+
* Sign up with email and password.
|
|
106
|
+
*/
|
|
107
|
+
const signUp = (0, react_1.useCallback)(async (data) => {
|
|
108
|
+
const { email, password, name } = data;
|
|
109
|
+
try {
|
|
110
|
+
const response = await fetch('/api/auth/register', {
|
|
111
|
+
method: 'POST',
|
|
112
|
+
headers: { 'Content-Type': 'application/json' },
|
|
113
|
+
body: JSON.stringify({ email, password, name }),
|
|
114
|
+
});
|
|
115
|
+
const result = await response.json();
|
|
116
|
+
if (!response.ok) {
|
|
117
|
+
return {
|
|
118
|
+
success: false,
|
|
119
|
+
error: result.error || 'Failed to create account',
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
return {
|
|
123
|
+
success: true,
|
|
124
|
+
customerId: result.customerId,
|
|
125
|
+
verificationEmailSent: true,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
catch (error) {
|
|
129
|
+
console.error('[useAuth] Sign up error:', error);
|
|
130
|
+
return {
|
|
131
|
+
success: false,
|
|
132
|
+
error: 'An error occurred during sign up. Please try again.',
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
}, []);
|
|
136
|
+
/**
|
|
137
|
+
* Sign out the current customer.
|
|
138
|
+
*/
|
|
139
|
+
const signOut = (0, react_1.useCallback)(async () => {
|
|
140
|
+
await (0, react_2.signOut)({ redirect: false });
|
|
141
|
+
}, []);
|
|
142
|
+
/**
|
|
143
|
+
* Resend verification email.
|
|
144
|
+
*/
|
|
145
|
+
const resendVerification = (0, react_1.useCallback)(async (email) => {
|
|
146
|
+
try {
|
|
147
|
+
const response = await fetch('/api/auth/resend-verification', {
|
|
148
|
+
method: 'POST',
|
|
149
|
+
headers: { 'Content-Type': 'application/json' },
|
|
150
|
+
body: JSON.stringify({ email }),
|
|
151
|
+
});
|
|
152
|
+
const result = await response.json();
|
|
153
|
+
if (!response.ok) {
|
|
154
|
+
return {
|
|
155
|
+
success: false,
|
|
156
|
+
error: result.error || 'Failed to send verification email',
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
return { success: true };
|
|
160
|
+
}
|
|
161
|
+
catch (error) {
|
|
162
|
+
console.error('[useAuth] Resend verification error:', error);
|
|
163
|
+
return {
|
|
164
|
+
success: false,
|
|
165
|
+
error: 'An error occurred. Please try again.',
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
}, []);
|
|
169
|
+
/**
|
|
170
|
+
* Request password reset.
|
|
171
|
+
*/
|
|
172
|
+
const requestPasswordReset = (0, react_1.useCallback)(async (email) => {
|
|
173
|
+
try {
|
|
174
|
+
const response = await fetch('/api/auth/forgot-password', {
|
|
175
|
+
method: 'POST',
|
|
176
|
+
headers: { 'Content-Type': 'application/json' },
|
|
177
|
+
body: JSON.stringify({ email }),
|
|
178
|
+
});
|
|
179
|
+
// Always return success to prevent email enumeration
|
|
180
|
+
return { success: true };
|
|
181
|
+
}
|
|
182
|
+
catch (error) {
|
|
183
|
+
console.error('[useAuth] Password reset request error:', error);
|
|
184
|
+
// Still return success for security
|
|
185
|
+
return { success: true };
|
|
186
|
+
}
|
|
187
|
+
}, []);
|
|
188
|
+
/**
|
|
189
|
+
* Reset password with token.
|
|
190
|
+
*/
|
|
191
|
+
const resetPassword = (0, react_1.useCallback)(async (token, newPassword) => {
|
|
192
|
+
try {
|
|
193
|
+
const response = await fetch('/api/auth/reset-password', {
|
|
194
|
+
method: 'POST',
|
|
195
|
+
headers: { 'Content-Type': 'application/json' },
|
|
196
|
+
body: JSON.stringify({ token, password: newPassword }),
|
|
197
|
+
});
|
|
198
|
+
const result = await response.json();
|
|
199
|
+
if (!response.ok) {
|
|
200
|
+
return {
|
|
201
|
+
success: false,
|
|
202
|
+
error: result.error || 'Failed to reset password',
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
return { success: true };
|
|
206
|
+
}
|
|
207
|
+
catch (error) {
|
|
208
|
+
console.error('[useAuth] Reset password error:', error);
|
|
209
|
+
return {
|
|
210
|
+
success: false,
|
|
211
|
+
error: 'An error occurred. Please try again.',
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
}, []);
|
|
215
|
+
/**
|
|
216
|
+
* Validate reset token.
|
|
217
|
+
*/
|
|
218
|
+
const validateResetToken = (0, react_1.useCallback)(async (token) => {
|
|
219
|
+
try {
|
|
220
|
+
const response = await fetch(`/api/auth/reset-password?token=${encodeURIComponent(token)}`);
|
|
221
|
+
const result = await response.json();
|
|
222
|
+
if (!response.ok || !result.valid) {
|
|
223
|
+
return {
|
|
224
|
+
valid: false,
|
|
225
|
+
error: result.error || 'Invalid or expired reset link',
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
return { valid: true };
|
|
229
|
+
}
|
|
230
|
+
catch (error) {
|
|
231
|
+
console.error('[useAuth] Validate token error:', error);
|
|
232
|
+
return {
|
|
233
|
+
valid: false,
|
|
234
|
+
error: 'Failed to validate reset link. Please try again.',
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
}, []);
|
|
238
|
+
return {
|
|
239
|
+
// State
|
|
240
|
+
customer,
|
|
241
|
+
isAuthenticated,
|
|
242
|
+
isLoading,
|
|
243
|
+
isEmailVerified,
|
|
244
|
+
// Actions
|
|
245
|
+
signIn,
|
|
246
|
+
signUp,
|
|
247
|
+
signOut,
|
|
248
|
+
resendVerification,
|
|
249
|
+
requestPasswordReset,
|
|
250
|
+
resetPassword,
|
|
251
|
+
validateResetToken,
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
//# sourceMappingURL=useAuth.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useAuth.js","sourceRoot":"","sources":["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