@bymax-one/nest-auth 1.0.11 → 1.1.1
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/CHANGELOG.md +443 -1
- package/README.md +275 -108
- package/dist/client/index.cjs +49 -5
- package/dist/client/index.mjs +49 -5
- package/dist/nextjs/index.cjs +152 -51
- package/dist/nextjs/index.d.cts +164 -58
- package/dist/nextjs/index.d.ts +164 -58
- package/dist/nextjs/index.mjs +152 -51
- package/dist/react/index.cjs +85 -89
- package/dist/react/index.mjs +85 -89
- package/dist/server/index.cjs +6083 -1574
- package/dist/server/index.d.cts +2880 -180
- package/dist/server/index.d.ts +2880 -180
- package/dist/server/index.mjs +6062 -1560
- package/dist/shared/index.cjs +60 -9
- package/dist/shared/index.d.cts +126 -18
- package/dist/shared/index.d.ts +126 -18
- package/dist/shared/index.mjs +60 -9
- package/package.json +112 -43
package/dist/react/index.mjs
CHANGED
|
@@ -50,6 +50,7 @@ function AuthProvider({ children, client, onSessionExpired, revalidateInterval =
|
|
|
50
50
|
const onSessionExpiredRef = useRef(onSessionExpired);
|
|
51
51
|
const clientRef = useRef(client);
|
|
52
52
|
const statusRef = useRef(INITIAL_STATE.status);
|
|
53
|
+
const sessionGenerationRef = useRef({});
|
|
53
54
|
useEffect(() => {
|
|
54
55
|
onSessionExpiredRef.current = onSessionExpired;
|
|
55
56
|
}, [
|
|
@@ -76,42 +77,41 @@ function AuthProvider({ children, client, onSessionExpired, revalidateInterval =
|
|
|
76
77
|
return;
|
|
77
78
|
}
|
|
78
79
|
}, []);
|
|
79
|
-
const revalidate = useCallback(
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
80
|
+
const revalidate = useCallback(async (isInitial) => {
|
|
81
|
+
const generation = sessionGenerationRef.current;
|
|
82
|
+
try {
|
|
83
|
+
const user = await clientRef.current.getMe();
|
|
84
|
+
if (sessionGenerationRef.current !== generation) return;
|
|
85
|
+
syncedDispatch({
|
|
86
|
+
type: "SET_USER",
|
|
87
|
+
payload: {
|
|
88
|
+
user,
|
|
89
|
+
timestamp: /* @__PURE__ */ new Date()
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
} catch (error) {
|
|
93
|
+
if (sessionGenerationRef.current !== generation) return;
|
|
94
|
+
const wasAuthenticated = statusRef.current === "authenticated";
|
|
95
|
+
if (isSessionExpiredError(error)) {
|
|
96
|
+
if (!isInitial && wasAuthenticated) {
|
|
97
|
+
try {
|
|
98
|
+
onSessionExpiredRef.current?.();
|
|
99
|
+
} catch (callbackError) {
|
|
100
|
+
console.warn("[nest-auth] onSessionExpired callback threw:", callbackError);
|
|
99
101
|
}
|
|
100
|
-
syncedDispatch({
|
|
101
|
-
type: "CLEAR_SESSION"
|
|
102
|
-
});
|
|
103
|
-
return;
|
|
104
102
|
}
|
|
105
103
|
syncedDispatch({
|
|
106
|
-
type: "
|
|
104
|
+
type: "CLEAR_SESSION"
|
|
107
105
|
});
|
|
106
|
+
return;
|
|
108
107
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
108
|
+
syncedDispatch({
|
|
109
|
+
type: "SET_ERROR"
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
}, [
|
|
113
|
+
syncedDispatch
|
|
114
|
+
]);
|
|
115
115
|
useEffect(() => {
|
|
116
116
|
void revalidate(true);
|
|
117
117
|
}, [
|
|
@@ -131,75 +131,71 @@ function AuthProvider({ children, client, onSessionExpired, revalidateInterval =
|
|
|
131
131
|
revalidate,
|
|
132
132
|
revalidateInterval
|
|
133
133
|
]);
|
|
134
|
-
const login = useCallback(
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
134
|
+
const login = useCallback(async (email, password, options) => {
|
|
135
|
+
const tenantId = options?.tenantId ?? DEFAULT_TENANT_ID;
|
|
136
|
+
syncedDispatch({
|
|
137
|
+
type: "SET_LOADING"
|
|
138
|
+
});
|
|
139
|
+
try {
|
|
140
|
+
const result = await clientRef.current.login({
|
|
141
|
+
email,
|
|
142
|
+
password,
|
|
143
|
+
tenantId
|
|
139
144
|
});
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
email,
|
|
143
|
-
password,
|
|
144
|
-
tenantId
|
|
145
|
-
});
|
|
146
|
-
if ("mfaRequired" in result) {
|
|
147
|
-
syncedDispatch({
|
|
148
|
-
type: "CLEAR_SESSION"
|
|
149
|
-
});
|
|
150
|
-
return result;
|
|
151
|
-
}
|
|
145
|
+
if ("mfaRequired" in result) {
|
|
146
|
+
sessionGenerationRef.current = {};
|
|
152
147
|
syncedDispatch({
|
|
153
|
-
type: "
|
|
154
|
-
payload: {
|
|
155
|
-
user: result.user,
|
|
156
|
-
timestamp: /* @__PURE__ */ new Date()
|
|
157
|
-
}
|
|
148
|
+
type: "CLEAR_SESSION"
|
|
158
149
|
});
|
|
159
150
|
return result;
|
|
160
|
-
} catch (error) {
|
|
161
|
-
syncedDispatch({
|
|
162
|
-
type: "SET_ERROR"
|
|
163
|
-
});
|
|
164
|
-
throw error;
|
|
165
151
|
}
|
|
166
|
-
|
|
167
|
-
// Stryker disable next-line ArrayDeclaration: deps hold only the stable `syncedDispatch`, so emptying the array does not change memoization
|
|
168
|
-
[
|
|
169
|
-
syncedDispatch
|
|
170
|
-
]
|
|
171
|
-
);
|
|
172
|
-
const register = useCallback(
|
|
173
|
-
async (data) => {
|
|
152
|
+
sessionGenerationRef.current = {};
|
|
174
153
|
syncedDispatch({
|
|
175
|
-
type: "
|
|
154
|
+
type: "SET_USER",
|
|
155
|
+
payload: {
|
|
156
|
+
user: result.user,
|
|
157
|
+
timestamp: /* @__PURE__ */ new Date()
|
|
158
|
+
}
|
|
176
159
|
});
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
160
|
+
return result;
|
|
161
|
+
} catch (error) {
|
|
162
|
+
syncedDispatch({
|
|
163
|
+
type: "SET_ERROR"
|
|
164
|
+
});
|
|
165
|
+
throw error;
|
|
166
|
+
}
|
|
167
|
+
}, [
|
|
168
|
+
syncedDispatch
|
|
169
|
+
]);
|
|
170
|
+
const register = useCallback(async (data) => {
|
|
171
|
+
syncedDispatch({
|
|
172
|
+
type: "SET_LOADING"
|
|
173
|
+
});
|
|
174
|
+
try {
|
|
175
|
+
const result = await clientRef.current.register(data);
|
|
176
|
+
sessionGenerationRef.current = {};
|
|
177
|
+
syncedDispatch({
|
|
178
|
+
type: "SET_USER",
|
|
179
|
+
payload: {
|
|
180
|
+
user: result.user,
|
|
181
|
+
timestamp: /* @__PURE__ */ new Date()
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
return result;
|
|
185
|
+
} catch (error) {
|
|
186
|
+
syncedDispatch({
|
|
187
|
+
type: "SET_ERROR"
|
|
188
|
+
});
|
|
189
|
+
throw error;
|
|
190
|
+
}
|
|
191
|
+
}, [
|
|
192
|
+
syncedDispatch
|
|
193
|
+
]);
|
|
199
194
|
const logout = useCallback(async () => {
|
|
200
195
|
try {
|
|
201
196
|
await clientRef.current.logout();
|
|
202
197
|
} finally {
|
|
198
|
+
sessionGenerationRef.current = {};
|
|
203
199
|
syncedDispatch({
|
|
204
200
|
type: "CLEAR_SESSION"
|
|
205
201
|
});
|