Skip to content

Commit ada1fdf

Browse files
committed
fix(PayloadSessionProvider): Unset the local session then request failed
1 parent a3bd556 commit ada1fdf

File tree

1 file changed

+33
-28
lines changed

1 file changed

+33
-28
lines changed

packages/payload-authjs/src/payload/session/PayloadSessionProvider.tsx

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -94,22 +94,25 @@ export const PayloadSessionProvider = <TSlug extends CollectionSlug = "users">({
9494
// Set loading to false
9595
setIsLoading(false);
9696

97-
// If the response is not ok or the user is not present, return null
98-
if (!response.ok || !result.user) {
97+
// If the response is ok
98+
if (response.ok && result.user) {
99+
// Update the local session
100+
const localSession = {
101+
user: result.user,
102+
expires: new Date(result.exp * 1000).toISOString(),
103+
collection: result.collection,
104+
strategy: result.user._strategy ?? result.strategy, // Extract the strategy from user._strategy or for legacy support directly from the result
105+
};
106+
setLocalSession(localSession);
107+
108+
// Return the session
109+
return localSession;
110+
} else {
111+
// Reset the session
112+
setLocalSession(null);
113+
99114
return null;
100115
}
101-
102-
// Update the local session
103-
const localSession = {
104-
user: result.user,
105-
expires: new Date(result.exp * 1000).toISOString(),
106-
collection: result.collection,
107-
strategy: result.user._strategy ?? result.strategy, // Extract the strategy from user._strategy or for legacy support directly from the result
108-
};
109-
setLocalSession(localSession);
110-
111-
// Return the session
112-
return localSession;
113116
},
114117
[userCollectionSlug],
115118
);
@@ -157,22 +160,24 @@ export const PayloadSessionProvider = <TSlug extends CollectionSlug = "users">({
157160
strategy?: string;
158161
} = await response.json();
159162

160-
// If the response is not ok or the user is not present, return null
161-
if (!response.ok || !result.user) {
162-
return null;
163-
}
163+
if (response.ok && result.user) {
164+
// Update the local session
165+
const localSession = {
166+
user: result.user,
167+
expires: new Date(result.exp * 1000).toISOString(),
168+
collection: result.user.collection ?? userCollectionSlug,
169+
strategy: result.user._strategy ?? result.strategy, // Extract the strategy from user._strategy or for legacy support directly from the result
170+
};
171+
setLocalSession(localSession);
164172

165-
// Update the local session
166-
const localSession = {
167-
user: result.user,
168-
expires: new Date(result.exp * 1000).toISOString(),
169-
collection: result.user.collection ?? userCollectionSlug,
170-
strategy: result.user._strategy ?? result.strategy, // Extract the strategy from user._strategy or for legacy support directly from the result
171-
};
172-
setLocalSession(localSession);
173+
// Return the session
174+
return localSession;
175+
} else {
176+
// Reset the session
177+
setLocalSession(null);
173178

174-
// Return the session
175-
return localSession;
179+
return null;
180+
}
176181
};
177182

178183
return (

0 commit comments

Comments
 (0)