Skip to content

Commit 4b66142

Browse files
committed
fix(getPayloadSession): Preferably extract strategy from user._strategy & improved types
1 parent d98f3aa commit 4b66142

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

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

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,23 @@ export const PayloadSessionProvider: React.FC<Props<CollectionSlug>> = <
6767
*/
6868
const fetchSession = useCallback(
6969
async ({ signal }: { signal?: AbortSignal } = {}) => {
70-
// Fetch the session from the server
70+
/**
71+
* Fetch the session from the server
72+
*
73+
* @see https://payloadcms.com/docs/rest-api/overview#auth-operations
74+
*/
7175
const response = await fetch(`/api/${userCollectionSlug}/me`, {
7276
signal,
7377
});
7478
const result: {
7579
user: DataFromCollectionSlug<TSlug> | null;
7680
exp: number;
7781
collection?: CollectionSlug;
82+
/**
83+
* @deprecated Use user._strategy instead
84+
*
85+
* @see https://github.com/payloadcms/payload/pull/11701
86+
*/
7887
strategy?: string;
7988
} = await response.json();
8089

@@ -91,7 +100,7 @@ export const PayloadSessionProvider: React.FC<Props<CollectionSlug>> = <
91100
user: result.user,
92101
expires: new Date(result.exp * 1000).toISOString(),
93102
collection: result.collection,
94-
strategy: result.strategy,
103+
strategy: result.user._strategy ?? result.strategy, // Extract the strategy from user._strategy or for legacy support directly from the result
95104
};
96105
setLocalSession(localSession);
97106

@@ -121,12 +130,23 @@ export const PayloadSessionProvider: React.FC<Props<CollectionSlug>> = <
121130
* Function to refresh the session
122131
*/
123132
const refresh = async () => {
124-
// Refresh the session on the server
133+
/**
134+
* Refresh the session on the server
135+
*
136+
* @see https://payloadcms.com/docs/rest-api/overview#auth-operations
137+
*/
125138
const response = await fetch(`/api/${userCollectionSlug}/refresh-token`, {
126139
method: "POST",
127140
});
128-
const result: { user: DataFromCollectionSlug<TSlug> | null; exp: number } =
129-
await response.json();
141+
const result: {
142+
user: DataFromCollectionSlug<TSlug> | null;
143+
exp: number /**
144+
* @deprecated Use user._strategy instead
145+
*
146+
* @see https://github.com/payloadcms/payload/pull/11701
147+
*/;
148+
strategy?: string;
149+
} = await response.json();
130150

131151
// If the response is not ok or the user is not present, return null
132152
if (!response.ok || !result.user) {
@@ -137,6 +157,8 @@ export const PayloadSessionProvider: React.FC<Props<CollectionSlug>> = <
137157
const localSession = {
138158
user: result.user,
139159
expires: new Date(result.exp * 1000).toISOString(),
160+
collection: result.user.collection ?? userCollectionSlug,
161+
strategy: result.user._strategy ?? result.strategy, // Extract the strategy from user._strategy or for legacy support directly from the result
140162
};
141163
setLocalSession(localSession);
142164

packages/payload-authjs/src/payload/session/getPayloadSession.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ interface Options<TSlug extends CollectionSlug> {
1313
}
1414

1515
export interface PayloadSession<TSlug extends CollectionSlug> {
16-
user: DataFromCollectionSlug<TSlug>;
16+
user: {
17+
collection?: CollectionSlug;
18+
_strategy?: typeof AUTHJS_STRATEGY_NAME | "local-jwt" | "api-key" | ({} & string);
19+
} & DataFromCollectionSlug<TSlug>;
1720
expires: string;
1821
collection?: CollectionSlug;
1922
strategy?: typeof AUTHJS_STRATEGY_NAME | "local-jwt" | "api-key" | ({} & string);
@@ -47,6 +50,11 @@ export const getPayloadSession = cache(
4750
user: DataFromCollectionSlug<TSlug> | null;
4851
exp: number;
4952
collection?: CollectionSlug;
53+
/**
54+
* @deprecated Use user._strategy instead
55+
*
56+
* @see https://github.com/payloadcms/payload/pull/11701
57+
*/
5058
strategy?: string;
5159
} = await response.json();
5260

@@ -60,7 +68,7 @@ export const getPayloadSession = cache(
6068
user: result.user,
6169
expires: new Date(result.exp * 1000).toISOString(),
6270
collection: result.collection,
63-
strategy: result.strategy,
71+
strategy: result.user._strategy ?? result.strategy, // Extract the strategy from user._strategy or for legacy support directly from the result
6472
};
6573
},
6674
);

0 commit comments

Comments
 (0)