@ttt-productions/auth-core 0.3.3 → 0.3.5
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/react/AuthProvider.d.ts +5 -5
- package/dist/react/AuthProvider.d.ts.map +1 -1
- package/dist/react/AuthProvider.js +4 -119
- package/dist/react/AuthProvider.js.map +1 -1
- package/dist/react/index.d.ts +3 -2
- package/dist/react/index.d.ts.map +1 -1
- package/dist/react/index.js +1 -2
- package/dist/react/index.js.map +1 -1
- package/dist/react/types.d.ts +3 -37
- package/dist/react/types.d.ts.map +1 -1
- package/dist/react/useAuth.d.ts +3 -3
- package/dist/react/useAuth.d.ts.map +1 -1
- package/dist/react/useAuth.js +1 -1
- package/dist/react/useAuth.js.map +1 -1
- package/dist/react/useAuthGuard.d.ts +0 -2
- package/dist/react/useAuthGuard.d.ts.map +1 -1
- package/dist/react/useAuthGuard.js +1 -7
- package/dist/react/useAuthGuard.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { type ReactNode } from "react";
|
|
2
|
-
import type { AuthProviderConfig, AuthContextValue
|
|
3
|
-
export declare const AuthContext: import("react").Context<AuthContextValue<any
|
|
4
|
-
interface AuthProviderProps<
|
|
5
|
-
config: AuthProviderConfig<
|
|
2
|
+
import type { AuthProviderConfig, AuthContextValue } from "./types.js";
|
|
3
|
+
export declare const AuthContext: import("react").Context<AuthContextValue<any> | null>;
|
|
4
|
+
interface AuthProviderProps<TClaims> {
|
|
5
|
+
config: AuthProviderConfig<TClaims>;
|
|
6
6
|
children: ReactNode;
|
|
7
7
|
}
|
|
8
|
-
export declare function AuthProvider<
|
|
8
|
+
export declare function AuthProvider<TClaims = Record<string, unknown>>({ config, children }: AuthProviderProps<TClaims>): import("react/jsx-runtime").JSX.Element;
|
|
9
9
|
export {};
|
|
10
10
|
//# sourceMappingURL=AuthProvider.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AuthProvider.d.ts","sourceRoot":"","sources":["../../src/react/AuthProvider.tsx"],"names":[],"mappings":"AAEA,OAAO,EAOL,KAAK,SAAS,EACf,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"AuthProvider.d.ts","sourceRoot":"","sources":["../../src/react/AuthProvider.tsx"],"names":[],"mappings":"AAEA,OAAO,EAOL,KAAK,SAAS,EACf,MAAM,OAAO,CAAC;AAGf,OAAO,KAAK,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAGvE,eAAO,MAAM,WAAW,uDAAoD,CAAC;AAE7E,UAAU,iBAAiB,CAAC,OAAO;IACjC,MAAM,EAAE,kBAAkB,CAAC,OAAO,CAAC,CAAC;IACpC,QAAQ,EAAE,SAAS,CAAC;CACrB;AAED,wBAAgB,YAAY,CAAC,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC5D,EAAE,MAAM,EAAE,QAAQ,EAAE,EAAE,iBAAiB,CAAC,OAAO,CAAC,2CA6EjD"}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
3
|
import { createContext, useEffect, useRef, useState, useCallback, useMemo, } from "react";
|
|
4
|
-
import { doc, onSnapshot } from "firebase/firestore";
|
|
5
4
|
import { useAuthState } from "./useAuthState.js";
|
|
6
5
|
import { getIdTokenClaims } from "../claims.js";
|
|
7
6
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -10,13 +9,6 @@ export function AuthProvider({ config, children }) {
|
|
|
10
9
|
const { user, loading: authLoading } = useAuthState(config.auth);
|
|
11
10
|
const [claims, setClaims] = useState(config.defaultClaims);
|
|
12
11
|
const [claimsLoading, setClaimsLoading] = useState(true);
|
|
13
|
-
const [userProfile, setUserProfile] = useState(null);
|
|
14
|
-
const [profileLoading, setProfileLoading] = useState(true);
|
|
15
|
-
const [profileError, setProfileError] = useState(null);
|
|
16
|
-
// Refs for cleanup
|
|
17
|
-
const profileUnsubRef = useRef(null);
|
|
18
|
-
const timeoutRef = useRef(null);
|
|
19
|
-
const prevUidRef = useRef(null);
|
|
20
12
|
// Stable config ref so effects don't re-fire on config object identity changes
|
|
21
13
|
const configRef = useRef(config);
|
|
22
14
|
configRef.current = config;
|
|
@@ -27,10 +19,12 @@ export function AuthProvider({ config, children }) {
|
|
|
27
19
|
if (!user) {
|
|
28
20
|
setClaims(configRef.current.defaultClaims);
|
|
29
21
|
setClaimsLoading(false);
|
|
22
|
+
configRef.current.onAuthStateChange?.(null);
|
|
30
23
|
return;
|
|
31
24
|
}
|
|
32
25
|
let cancelled = false;
|
|
33
26
|
setClaimsLoading(true);
|
|
27
|
+
configRef.current.onAuthStateChange?.(user);
|
|
34
28
|
getIdTokenClaims(user)
|
|
35
29
|
.then((raw) => {
|
|
36
30
|
if (cancelled)
|
|
@@ -49,102 +43,6 @@ export function AuthProvider({ config, children }) {
|
|
|
49
43
|
});
|
|
50
44
|
return () => { cancelled = true; };
|
|
51
45
|
}, [user, authLoading]);
|
|
52
|
-
// --- Profile listener ---
|
|
53
|
-
useEffect(() => {
|
|
54
|
-
if (authLoading)
|
|
55
|
-
return;
|
|
56
|
-
const uid = user?.uid ?? null;
|
|
57
|
-
const prevUid = prevUidRef.current;
|
|
58
|
-
prevUidRef.current = uid;
|
|
59
|
-
// Clean up previous listener + timeout if user changed
|
|
60
|
-
if (uid !== prevUid) {
|
|
61
|
-
if (profileUnsubRef.current) {
|
|
62
|
-
profileUnsubRef.current();
|
|
63
|
-
profileUnsubRef.current = null;
|
|
64
|
-
}
|
|
65
|
-
if (timeoutRef.current) {
|
|
66
|
-
clearTimeout(timeoutRef.current);
|
|
67
|
-
timeoutRef.current = null;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
// User signed out
|
|
71
|
-
if (!uid) {
|
|
72
|
-
setUserProfile(null);
|
|
73
|
-
setProfileError(null);
|
|
74
|
-
setProfileLoading(false);
|
|
75
|
-
configRef.current.onAuthStateChange?.(null);
|
|
76
|
-
configRef.current.onProfileChange?.(null, null);
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
// User signed in (or changed)
|
|
80
|
-
setProfileLoading(true);
|
|
81
|
-
setProfileError(null);
|
|
82
|
-
configRef.current.onAuthStateChange?.(user);
|
|
83
|
-
const timeout = (configRef.current.profileTimeout ?? 15) * 1000;
|
|
84
|
-
const [colPath, docId] = configRef.current.profilePath(uid);
|
|
85
|
-
const docRef = doc(configRef.current.db, colPath, docId);
|
|
86
|
-
// Start timeout timer
|
|
87
|
-
timeoutRef.current = setTimeout(() => {
|
|
88
|
-
// Only fire if we're still waiting (profileLoading is still true)
|
|
89
|
-
setProfileError({
|
|
90
|
-
code: "PROFILE_TIMEOUT",
|
|
91
|
-
message: `Profile document not found after ${configRef.current.profileTimeout ?? 15}s.`,
|
|
92
|
-
uid,
|
|
93
|
-
});
|
|
94
|
-
setProfileLoading(false);
|
|
95
|
-
}, timeout);
|
|
96
|
-
const unsub = onSnapshot(docRef, (snap) => {
|
|
97
|
-
// Clear timeout on any successful snapshot
|
|
98
|
-
if (timeoutRef.current) {
|
|
99
|
-
clearTimeout(timeoutRef.current);
|
|
100
|
-
timeoutRef.current = null;
|
|
101
|
-
}
|
|
102
|
-
if (snap.exists()) {
|
|
103
|
-
const data = snap.data();
|
|
104
|
-
const parsed = configRef.current.parseProfile
|
|
105
|
-
? configRef.current.parseProfile(data, uid)
|
|
106
|
-
: { uid, ...data };
|
|
107
|
-
setUserProfile(parsed);
|
|
108
|
-
setProfileError(null);
|
|
109
|
-
setProfileLoading(false);
|
|
110
|
-
configRef.current.onProfileChange?.(parsed, user);
|
|
111
|
-
}
|
|
112
|
-
else {
|
|
113
|
-
// Doc doesn't exist yet — restart timeout if not already timed out
|
|
114
|
-
if (!timeoutRef.current) {
|
|
115
|
-
timeoutRef.current = setTimeout(() => {
|
|
116
|
-
setProfileError({
|
|
117
|
-
code: "PROFILE_NOT_FOUND",
|
|
118
|
-
message: "Profile document does not exist.",
|
|
119
|
-
uid,
|
|
120
|
-
});
|
|
121
|
-
setProfileLoading(false);
|
|
122
|
-
}, timeout);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
}, (err) => {
|
|
126
|
-
if (timeoutRef.current) {
|
|
127
|
-
clearTimeout(timeoutRef.current);
|
|
128
|
-
timeoutRef.current = null;
|
|
129
|
-
}
|
|
130
|
-
configRef.current.onError?.(err, "profile onSnapshot");
|
|
131
|
-
setProfileError({
|
|
132
|
-
code: "PROFILE_LISTENER_ERROR",
|
|
133
|
-
message: err instanceof Error ? err.message : "Profile listener failed.",
|
|
134
|
-
uid,
|
|
135
|
-
});
|
|
136
|
-
setProfileLoading(false);
|
|
137
|
-
});
|
|
138
|
-
profileUnsubRef.current = unsub;
|
|
139
|
-
return () => {
|
|
140
|
-
unsub();
|
|
141
|
-
profileUnsubRef.current = null;
|
|
142
|
-
if (timeoutRef.current) {
|
|
143
|
-
clearTimeout(timeoutRef.current);
|
|
144
|
-
timeoutRef.current = null;
|
|
145
|
-
}
|
|
146
|
-
};
|
|
147
|
-
}, [user, authLoading]);
|
|
148
46
|
// --- refreshClaims ---
|
|
149
47
|
const refreshClaims = useCallback(async () => {
|
|
150
48
|
if (!user)
|
|
@@ -158,29 +56,16 @@ export function AuthProvider({ config, children }) {
|
|
|
158
56
|
}
|
|
159
57
|
}, [user]);
|
|
160
58
|
// --- Context value ---
|
|
161
|
-
const loading = authLoading || claimsLoading
|
|
59
|
+
const loading = authLoading || claimsLoading;
|
|
162
60
|
const value = useMemo(() => ({
|
|
163
61
|
user,
|
|
164
|
-
userProfile,
|
|
165
62
|
claims,
|
|
166
63
|
loading,
|
|
167
64
|
authLoading,
|
|
168
65
|
claimsLoading,
|
|
169
|
-
profileLoading,
|
|
170
|
-
profileError,
|
|
171
66
|
isAuthenticated: !!user,
|
|
172
67
|
refreshClaims,
|
|
173
|
-
}), [
|
|
174
|
-
user,
|
|
175
|
-
userProfile,
|
|
176
|
-
claims,
|
|
177
|
-
loading,
|
|
178
|
-
authLoading,
|
|
179
|
-
claimsLoading,
|
|
180
|
-
profileLoading,
|
|
181
|
-
profileError,
|
|
182
|
-
refreshClaims,
|
|
183
|
-
]);
|
|
68
|
+
}), [user, claims, loading, authLoading, claimsLoading, refreshClaims]);
|
|
184
69
|
return (_jsx(AuthContext.Provider, { value: value, children: children }));
|
|
185
70
|
}
|
|
186
71
|
//# sourceMappingURL=AuthProvider.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AuthProvider.js","sourceRoot":"","sources":["../../src/react/AuthProvider.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AAEb,OAAO,EACL,aAAa,EACb,SAAS,EACT,MAAM,EACN,QAAQ,EACR,WAAW,EACX,OAAO,GAER,MAAM,OAAO,CAAC;AACf,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"AuthProvider.js","sourceRoot":"","sources":["../../src/react/AuthProvider.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AAEb,OAAO,EACL,aAAa,EACb,SAAS,EACT,MAAM,EACN,QAAQ,EACR,WAAW,EACX,OAAO,GAER,MAAM,OAAO,CAAC;AACf,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAGhD,8DAA8D;AAC9D,MAAM,CAAC,MAAM,WAAW,GAAG,aAAa,CAA+B,IAAI,CAAC,CAAC;AAO7E,MAAM,UAAU,YAAY,CAC1B,EAAE,MAAM,EAAE,QAAQ,EAA8B;IAEhD,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,WAAW,EAAE,GAAG,YAAY,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAEjE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,GAAG,QAAQ,CAAU,MAAM,CAAC,aAAa,CAAC,CAAC;IACpE,MAAM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AAG,SAAS,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;IACrC,CAAC,EAAE,CAAC,IAAI,EAAE,WAAW,CAAC,CAAC,CAAC;IAExB,wBAAwB;IACxB,MAAM,aAAa,GAAG,WAAW,CAAC,KAAK,IAAI,EAAE;QAC3C,IAAI,CAAC,IAAI;YAAE,OAAO;QAClB,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAC;YACjD,SAAS,CACP,SAAS,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC,MAAM,CAAC,MAAM,IAAI,EAAE,CAA4B,CAAC,CAChF,CAAC;QACJ,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,SAAS,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC,GAAG,EAAE,eAAe,CAAC,CAAC;QACpD,CAAC;IACH,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC;IAEX,wBAAwB;IACxB,MAAM,OAAO,GAAG,WAAW,IAAI,aAAa,CAAC;IAE7C,MAAM,KAAK,GAAG,OAAO,CACnB,GAAG,EAAE,CAAC,CAAC;QACL,IAAI;QACJ,MAAM;QACN,OAAO;QACP,WAAW;QACX,aAAa;QACb,eAAe,EAAE,CAAC,CAAC,IAAI;QACvB,aAAa;KACd,CAAC,EACF,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,aAAa,CAAC,CACnE,CAAC;IAEF,OAAO,CACL,KAAC,WAAW,CAAC,QAAQ,IAAC,KAAK,EAAE,KAAK,YAC/B,QAAQ,GACY,CACxB,CAAC;AACJ,CAAC"}
|
package/dist/react/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
1
|
+
export type { AuthState } from "./useAuthState.js";
|
|
2
|
+
export { useAuthState } from "./useAuthState.js";
|
|
3
|
+
export type { AuthProviderConfig, AuthContextValue } from "./types.js";
|
|
3
4
|
export { AuthProvider } from "./AuthProvider.js";
|
|
4
5
|
export { AuthContext } from "./AuthProvider.js";
|
|
5
6
|
export { useAuth } from "./useAuth.js";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/react/index.ts"],"names":[],"mappings":"AAEA,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/react/index.ts"],"names":[],"mappings":"AAEA,YAAY,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AACnD,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,YAAY,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AACvE,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,YAAY,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC"}
|
package/dist/react/index.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
export
|
|
3
|
-
export * from "./types.js";
|
|
2
|
+
export { useAuthState } from "./useAuthState.js";
|
|
4
3
|
export { AuthProvider } from "./AuthProvider.js";
|
|
5
4
|
export { AuthContext } from "./AuthProvider.js";
|
|
6
5
|
export { useAuth } from "./useAuth.js";
|
package/dist/react/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/react/index.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/react/index.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAGb,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEjD,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC"}
|
package/dist/react/types.d.ts
CHANGED
|
@@ -1,62 +1,28 @@
|
|
|
1
1
|
import type { Auth, User } from "firebase/auth";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Base profile contract. Apps extend with their own fields.
|
|
5
|
-
* auth-core uses these for monitoring integration and generic display.
|
|
6
|
-
*/
|
|
7
|
-
export interface BaseProfile {
|
|
8
|
-
uid: string;
|
|
9
|
-
displayName: string;
|
|
10
|
-
photoURL?: string | null;
|
|
11
|
-
status?: string;
|
|
12
|
-
}
|
|
13
|
-
export interface AuthProviderConfig<TProfile extends BaseProfile = BaseProfile, TClaims = Record<string, unknown>> {
|
|
2
|
+
export interface AuthProviderConfig<TClaims = Record<string, unknown>> {
|
|
14
3
|
auth: Auth;
|
|
15
|
-
db: Firestore;
|
|
16
|
-
/** Firestore path segments for profile doc: (uid) => ['users', uid] */
|
|
17
|
-
profilePath: (uid: string) => [string, string];
|
|
18
|
-
/** Transform raw Firestore data into app profile type */
|
|
19
|
-
parseProfile?: (data: Record<string, unknown>, uid: string) => TProfile;
|
|
20
4
|
/** Transform raw ID token claims into app claims shape */
|
|
21
5
|
parseClaims: (raw: Record<string, unknown>) => TClaims;
|
|
22
6
|
/** Default claims when no user or claims unavailable */
|
|
23
7
|
defaultClaims: TClaims;
|
|
24
8
|
/** Called on Firebase auth state change (for monitoring, analytics) */
|
|
25
9
|
onAuthStateChange?: (user: User | null) => void;
|
|
26
|
-
/** Called when profile snapshot updates (for monitoring, RQ cache bridge) */
|
|
27
|
-
onProfileChange?: (profile: TProfile | null, user: User | null) => void;
|
|
28
10
|
/** Called on errors (for Sentry) */
|
|
29
11
|
onError?: (error: unknown, context: string) => void;
|
|
30
|
-
/**
|
|
31
|
-
* Max seconds to wait for profile doc after auth.
|
|
32
|
-
* If exceeded, profileError is set and loading becomes false.
|
|
33
|
-
* Default: 15
|
|
34
|
-
*/
|
|
35
|
-
profileTimeout?: number;
|
|
36
12
|
}
|
|
37
|
-
export interface AuthContextValue<
|
|
13
|
+
export interface AuthContextValue<TClaims = Record<string, unknown>> {
|
|
38
14
|
/** Firebase User */
|
|
39
15
|
user: User | null;
|
|
40
|
-
/** Firestore profile doc, parsed */
|
|
41
|
-
userProfile: TProfile | null;
|
|
42
16
|
/** Parsed custom claims */
|
|
43
17
|
claims: TClaims;
|
|
44
|
-
/** True until auth + claims
|
|
18
|
+
/** True until auth + claims resolved */
|
|
45
19
|
loading: boolean;
|
|
46
20
|
/** Individual loading states */
|
|
47
21
|
authLoading: boolean;
|
|
48
22
|
claimsLoading: boolean;
|
|
49
|
-
profileLoading: boolean;
|
|
50
|
-
/** Set when user is authenticated but profile doc not found within timeout */
|
|
51
|
-
profileError: ProfileError | null;
|
|
52
23
|
/** Convenience: !!user */
|
|
53
24
|
isAuthenticated: boolean;
|
|
54
25
|
/** Force refresh claims from Firebase */
|
|
55
26
|
refreshClaims: () => Promise<void>;
|
|
56
27
|
}
|
|
57
|
-
export type ProfileError = {
|
|
58
|
-
code: "PROFILE_NOT_FOUND" | "PROFILE_TIMEOUT" | "PROFILE_LISTENER_ERROR";
|
|
59
|
-
message: string;
|
|
60
|
-
uid: string;
|
|
61
|
-
};
|
|
62
28
|
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/react/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/react/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;AAEhD,MAAM,WAAW,kBAAkB,CAAC,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IACnE,IAAI,EAAE,IAAI,CAAC;IAEX,0DAA0D;IAC1D,WAAW,EAAE,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,OAAO,CAAC;IAEvD,wDAAwD;IACxD,aAAa,EAAE,OAAO,CAAC;IAEvB,uEAAuE;IACvE,iBAAiB,CAAC,EAAE,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI,KAAK,IAAI,CAAC;IAEhD,oCAAoC;IACpC,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;CACrD;AAED,MAAM,WAAW,gBAAgB,CAAC,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IACjE,oBAAoB;IACpB,IAAI,EAAE,IAAI,GAAG,IAAI,CAAC;IAElB,2BAA2B;IAC3B,MAAM,EAAE,OAAO,CAAC;IAEhB,wCAAwC;IACxC,OAAO,EAAE,OAAO,CAAC;IAEjB,gCAAgC;IAChC,WAAW,EAAE,OAAO,CAAC;IACrB,aAAa,EAAE,OAAO,CAAC;IAEvB,0BAA0B;IAC1B,eAAe,EAAE,OAAO,CAAC;IAEzB,yCAAyC;IACzC,aAAa,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CACpC"}
|
package/dist/react/useAuth.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { AuthContextValue } from "./types.js";
|
|
2
2
|
/**
|
|
3
3
|
* Access the auth context. Must be used within an AuthProvider.
|
|
4
|
-
* Generic
|
|
4
|
+
* Generic param lets each app specify its own claims type.
|
|
5
5
|
*/
|
|
6
|
-
export declare function useAuth<
|
|
6
|
+
export declare function useAuth<TClaims = Record<string, unknown>>(): AuthContextValue<TClaims>;
|
|
7
7
|
//# sourceMappingURL=useAuth.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useAuth.d.ts","sourceRoot":"","sources":["../../src/react/useAuth.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"useAuth.d.ts","sourceRoot":"","sources":["../../src/react/useAuth.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAEnD;;;GAGG;AACH,wBAAgB,OAAO,CAAC,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,gBAAgB,CAAC,OAAO,CAAC,CAMtF"}
|
package/dist/react/useAuth.js
CHANGED
|
@@ -3,7 +3,7 @@ import { useContext } from "react";
|
|
|
3
3
|
import { AuthContext } from "./AuthProvider.js";
|
|
4
4
|
/**
|
|
5
5
|
* Access the auth context. Must be used within an AuthProvider.
|
|
6
|
-
* Generic
|
|
6
|
+
* Generic param lets each app specify its own claims type.
|
|
7
7
|
*/
|
|
8
8
|
export function useAuth() {
|
|
9
9
|
const ctx = useContext(AuthContext);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useAuth.js","sourceRoot":"","sources":["../../src/react/useAuth.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AACnC,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAGhD;;;GAGG;AACH,MAAM,UAAU,OAAO;
|
|
1
|
+
{"version":3,"file":"useAuth.js","sourceRoot":"","sources":["../../src/react/useAuth.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AACnC,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAGhD;;;GAGG;AACH,MAAM,UAAU,OAAO;IACrB,MAAM,GAAG,GAAG,UAAU,CAAC,WAAW,CAAC,CAAC;IACpC,IAAI,CAAC,GAAG,EAAE,CAAC;QACT,MAAM,IAAI,KAAK,CAAC,6CAA6C,CAAC,CAAC;IACjE,CAAC;IACD,OAAO,GAAgC,CAAC;AAC1C,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useAuthGuard.d.ts","sourceRoot":"","sources":["../../src/react/useAuthGuard.ts"],"names":[],"mappings":"AAIA,MAAM,WAAW,eAAe;IAC9B,+CAA+C;IAC/C,YAAY,EAAE,MAAM,EAAE,CAAC;IACvB,sEAAsE;IACtE,kBAAkB,EAAE,MAAM,EAAE,CAAC;IAC7B,qCAAqC;IACrC,UAAU,EAAE,MAAM,CAAC;IACnB,2DAA2D;IAC3D,YAAY,EAAE,MAAM,CAAC;IACrB,iCAAiC;IACjC,YAAY,CAAC,EAAE;QAAE,aAAa,EAAE,MAAM,CAAC;QAAC,eAAe,EAAE,MAAM,CAAA;KAAE,CAAC;IAClE,+CAA+C;IAC/C,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,gDAAgD;IAChD,OAAO,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;IAChC,mBAAmB;IACnB,QAAQ,EAAE,MAAM,CAAC;IACjB,qBAAqB;IACrB,OAAO,EAAE,OAAO,CAAC;IACjB,qBAAqB;IACrB,eAAe,EAAE,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"useAuthGuard.d.ts","sourceRoot":"","sources":["../../src/react/useAuthGuard.ts"],"names":[],"mappings":"AAIA,MAAM,WAAW,eAAe;IAC9B,+CAA+C;IAC/C,YAAY,EAAE,MAAM,EAAE,CAAC;IACvB,sEAAsE;IACtE,kBAAkB,EAAE,MAAM,EAAE,CAAC;IAC7B,qCAAqC;IACrC,UAAU,EAAE,MAAM,CAAC;IACnB,2DAA2D;IAC3D,YAAY,EAAE,MAAM,CAAC;IACrB,iCAAiC;IACjC,YAAY,CAAC,EAAE;QAAE,aAAa,EAAE,MAAM,CAAC;QAAC,eAAe,EAAE,MAAM,CAAA;KAAE,CAAC;IAClE,+CAA+C;IAC/C,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,gDAAgD;IAChD,OAAO,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;IAChC,mBAAmB;IACnB,QAAQ,EAAE,MAAM,CAAC;IACjB,qBAAqB;IACrB,OAAO,EAAE,OAAO,CAAC;IACjB,qBAAqB;IACrB,eAAe,EAAE,OAAO,CAAC;CAC1B;AAID;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,eAAe,GAAG,IAAI,CA4D1D"}
|
|
@@ -10,7 +10,7 @@ const DEFAULT_REDIRECT_KEY = "auth_redirect_path";
|
|
|
10
10
|
* - Saves attempted path for post-login redirect.
|
|
11
11
|
*/
|
|
12
12
|
export function useAuthGuard(config) {
|
|
13
|
-
const { publicRoutes, authRedirectRoutes, loginRoute, defaultRoute, rootRedirect, redirectKey = DEFAULT_REDIRECT_KEY, replace, pathname, loading, isAuthenticated,
|
|
13
|
+
const { publicRoutes, authRedirectRoutes, loginRoute, defaultRoute, rootRedirect, redirectKey = DEFAULT_REDIRECT_KEY, replace, pathname, loading, isAuthenticated, } = config;
|
|
14
14
|
useEffect(() => {
|
|
15
15
|
if (loading)
|
|
16
16
|
return;
|
|
@@ -31,11 +31,6 @@ export function useAuthGuard(config) {
|
|
|
31
31
|
}
|
|
32
32
|
// Authenticated user on auth-only route (login, register, etc.)
|
|
33
33
|
if (isAuthenticated && isAuthRedirect) {
|
|
34
|
-
// Still registering -- profile not created yet, stay on page
|
|
35
|
-
if (!hasProfile && typeof window !== "undefined" && localStorage.getItem("isRegistering") === "true") {
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
// Redirect to saved path or default
|
|
39
34
|
let target = defaultRoute;
|
|
40
35
|
if (typeof window !== "undefined") {
|
|
41
36
|
const saved = localStorage.getItem(redirectKey);
|
|
@@ -49,7 +44,6 @@ export function useAuthGuard(config) {
|
|
|
49
44
|
}, [
|
|
50
45
|
loading,
|
|
51
46
|
isAuthenticated,
|
|
52
|
-
hasProfile,
|
|
53
47
|
pathname,
|
|
54
48
|
publicRoutes,
|
|
55
49
|
authRedirectRoutes,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useAuthGuard.js","sourceRoot":"","sources":["../../src/react/useAuthGuard.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"useAuthGuard.js","sourceRoot":"","sources":["../../src/react/useAuthGuard.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAyBlC,MAAM,oBAAoB,GAAG,oBAAoB,CAAC;AAElD;;;;;;;GAOG;AACH,MAAM,UAAU,YAAY,CAAC,MAAuB;IAClD,MAAM,EACJ,YAAY,EACZ,kBAAkB,EAClB,UAAU,EACV,YAAY,EACZ,YAAY,EACZ,WAAW,GAAG,oBAAoB,EAClC,OAAO,EACP,QAAQ,EACR,OAAO,EACP,eAAe,GAChB,GAAG,MAAM,CAAC;IAEX,SAAS,CAAC,GAAG,EAAE;QACb,IAAI,OAAO;YAAE,OAAO;QAEpB,6BAA6B;QAC7B,IAAI,YAAY,IAAI,QAAQ,KAAK,GAAG,EAAE,CAAC;YACrC,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,YAAY,CAAC,aAAa,CAAC,CAAC,CAAC,YAAY,CAAC,eAAe,CAAC,CAAC;YACrF,OAAO;QACT,CAAC;QAED,MAAM,QAAQ,GACZ,YAAY,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,IAAI,QAAQ,CAAC,UAAU,CAAC,KAAK,CAAC,CAA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