Skip to content

Commit 0a5e859

Browse files
committed
fix(PayloadSessionProvider): Abort fetch session on unmount
1 parent ff3b25b commit 0a5e859

File tree

1 file changed

+41
-25
lines changed

1 file changed

+41
-25
lines changed

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

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -65,35 +65,50 @@ export const PayloadSessionProvider: React.FC<Props<CollectionSlug>> = <
6565
/**
6666
* Function to fetch the session
6767
*/
68-
const fetchSession = useCallback(async () => {
69-
// Fetch the session from the server
70-
const response = await fetch(`/api/${userCollectionSlug}/me`);
71-
const result: { user: DataFromCollectionSlug<TSlug>; exp: number } = await response.json();
72-
73-
// Set loading to false
74-
setIsLoading(false);
75-
76-
// If the response is not ok or the user is not present, return null
77-
if (!response.ok || !result.user) {
78-
return null;
79-
}
80-
81-
// Update the local session
82-
const localSession = {
83-
user: result.user,
84-
expires: new Date(result.exp * 1000).toISOString(),
85-
};
86-
setLocalSession(localSession);
87-
88-
// Return the session
89-
return localSession;
90-
}, [userCollectionSlug]);
68+
const fetchSession = useCallback(
69+
async ({ signal }: { signal?: AbortSignal } = {}) => {
70+
// Fetch the session from the server
71+
const response = await fetch(`/api/${userCollectionSlug}/me`, {
72+
signal,
73+
});
74+
const result: { user: DataFromCollectionSlug<TSlug> | null; exp: number } =
75+
await response.json();
76+
77+
// Set loading to false
78+
setIsLoading(false);
79+
80+
// If the response is not ok or the user is not present, return null
81+
if (!response.ok || !result.user) {
82+
return null;
83+
}
84+
85+
// Update the local session
86+
const localSession = {
87+
user: result.user,
88+
expires: new Date(result.exp * 1000).toISOString(),
89+
};
90+
setLocalSession(localSession);
91+
92+
// Return the session
93+
return localSession;
94+
},
95+
[userCollectionSlug],
96+
);
9197

9298
/**
9399
* On mount, fetch the session
94100
*/
95101
useEffect(() => {
96-
void fetchSession();
102+
const abortController = new AbortController();
103+
104+
void fetchSession({
105+
signal: abortController.signal,
106+
});
107+
108+
// Abort the fetch on unmount
109+
return () => {
110+
abortController.abort();
111+
};
97112
}, [fetchSession]);
98113

99114
/**
@@ -104,7 +119,8 @@ export const PayloadSessionProvider: React.FC<Props<CollectionSlug>> = <
104119
const response = await fetch(`/api/${userCollectionSlug}/refresh-token`, {
105120
method: "POST",
106121
});
107-
const result: { user: DataFromCollectionSlug<TSlug>; exp: number } = await response.json();
122+
const result: { user: DataFromCollectionSlug<TSlug> | null; exp: number } =
123+
await response.json();
108124

109125
// If the response is not ok or the user is not present, return null
110126
if (!response.ok || !result.user) {

0 commit comments

Comments
 (0)