@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.
@@ -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
- async (isInitial) => {
81
- try {
82
- const user = await clientRef.current.getMe();
83
- syncedDispatch({
84
- type: "SET_USER",
85
- payload: {
86
- user,
87
- timestamp: /* @__PURE__ */ new Date()
88
- }
89
- });
90
- } catch (error) {
91
- const wasAuthenticated = statusRef.current === "authenticated";
92
- if (isSessionExpiredError(error)) {
93
- if (!isInitial && wasAuthenticated) {
94
- try {
95
- onSessionExpiredRef.current?.();
96
- } catch (callbackError) {
97
- console.warn("[nest-auth] onSessionExpired callback threw:", callbackError);
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: "SET_ERROR"
104
+ type: "CLEAR_SESSION"
107
105
  });
106
+ return;
108
107
  }
109
- },
110
- // Stryker disable next-line ArrayDeclaration: deps hold only the stable `syncedDispatch`, so emptying the array does not change effect/memo behavior
111
- [
112
- syncedDispatch
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
- async (email, password, options) => {
136
- const tenantId = options?.tenantId ?? DEFAULT_TENANT_ID;
137
- syncedDispatch({
138
- type: "SET_LOADING"
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
- try {
141
- const result = await clientRef.current.login({
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: "SET_USER",
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: "SET_LOADING"
154
+ type: "SET_USER",
155
+ payload: {
156
+ user: result.user,
157
+ timestamp: /* @__PURE__ */ new Date()
158
+ }
176
159
  });
177
- try {
178
- const result = await clientRef.current.register(data);
179
- syncedDispatch({
180
- type: "SET_USER",
181
- payload: {
182
- user: result.user,
183
- timestamp: /* @__PURE__ */ new Date()
184
- }
185
- });
186
- return result;
187
- } catch (error) {
188
- syncedDispatch({
189
- type: "SET_ERROR"
190
- });
191
- throw error;
192
- }
193
- },
194
- // Stryker disable next-line ArrayDeclaration: deps hold only the stable `syncedDispatch`, so emptying the array does not change memoization
195
- [
196
- syncedDispatch
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
  });