Skip to content

Commit 4ca2cdf

Browse files
committed
fix: Extend PayloadSession with collection and strategy
1 parent 2c94c40 commit 4ca2cdf

File tree

8 files changed

+51
-20
lines changed

8 files changed

+51
-20
lines changed

packages/dev/src/app/components/auth/authjs/AuthjsSessionClient.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ export const AuthjsSessionClient = () => {
88

99
return (
1010
<>
11-
<div className="mb-2 flex flex-col items-start gap-2">
11+
<div className="mb-2 flex gap-1">
1212
<Badge
1313
variant={status === "authenticated" ? "green" : status === "loading" ? "yellow" : "red"}
1414
>
15-
status: {status}
15+
Status: {status}
1616
</Badge>
17-
{session?.expires && <Badge>Expires: {new Date(session.expires).toLocaleString()}</Badge>}
17+
{session?.expires && (
18+
<Badge variant="yellow">Expires: {new Date(session.expires).toLocaleString()}</Badge>
19+
)}
1820
</div>
1921
<pre className="overflow-auto rounded-lg bg-gray-100 p-4">
2022
{JSON.stringify(session?.user ?? null, null, 2)}

packages/dev/src/app/components/auth/authjs/AuthjsSessionServer.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ export const AuthjsSessionServer = async () => {
66

77
return (
88
<>
9-
<div className="mb-2 flex flex-col items-start gap-2">
9+
<div className="mb-2 flex gap-1">
1010
<Badge variant={session ? "green" : "red"}>
11-
status: {session ? "authenticated" : "unauthenticated"}
11+
Status: {session ? "authenticated" : "unauthenticated"}
1212
</Badge>
13-
{session?.expires && <Badge>Expires: {new Date(session.expires).toLocaleString()}</Badge>}
13+
{session?.expires && (
14+
<Badge variant="yellow">Expires: {new Date(session.expires).toLocaleString()}</Badge>
15+
)}
1416
</div>
1517
<pre className="overflow-auto rounded-lg bg-gray-100 p-4">
1618
{JSON.stringify(session?.user ?? null, null, 2)}

packages/dev/src/app/components/auth/payload/PayloadSessionClientWithUseAuth.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ export const PayloadSessionClientWithUseAuth = () => {
99

1010
return (
1111
<>
12-
<div className="mb-2 flex flex-col items-start gap-2">
12+
<div className="mb-2 flex gap-1">
1313
<Badge variant={user ? "green" : "red"}>
14-
status: {user ? "authenticated" : "unauthenticated"}
14+
Status: {user ? "authenticated" : "unauthenticated"}
1515
</Badge>
1616
</div>
1717
<pre className="overflow-auto rounded-lg bg-gray-100 p-4">

packages/dev/src/app/components/auth/payload/PayloadSessionClientWithUsePayloadSession.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,21 @@ export const PayloadSessionClientWithUsePayloadSession = () => {
88

99
return (
1010
<>
11-
<div className="mb-2 flex flex-col items-start gap-2">
11+
<div className="mb-2 flex gap-1">
1212
<Badge
1313
variant={status === "authenticated" ? "green" : status === "loading" ? "yellow" : "red"}
1414
>
15-
status: {status}
15+
Status: {status}
1616
</Badge>
1717
{session?.expires && (
18-
<Badge onClick={refresh}>Expires: {new Date(session.expires).toLocaleString()}</Badge>
18+
<Badge variant="yellow" onClick={refresh}>
19+
Expires: {new Date(session.expires).toLocaleString()}
20+
</Badge>
1921
)}
22+
{session?.collection ? (
23+
<Badge variant="dark">Collection: {session.collection}</Badge>
24+
) : null}
25+
{session?.strategy ? <Badge variant="dark">Strategy: {session.strategy}</Badge> : null}
2026
</div>
2127
<pre className="overflow-auto rounded-lg bg-gray-100 p-4">
2228
{JSON.stringify(session?.user ?? null, null, 2)}

packages/dev/src/app/components/auth/payload/PayloadSessionServer.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ export const PayloadSessionServer = async () => {
77

88
return (
99
<>
10-
<div className="mb-2 flex flex-col items-start gap-2">
10+
<div className="mb-2 flex gap-1">
1111
<Badge variant={session ? "green" : "red"}>
12-
status: {session ? "authenticated" : "unauthenticated"}
12+
Status: {session ? "authenticated" : "unauthenticated"}
1313
</Badge>
1414
{session?.expires && (
1515
<Badge
16+
variant="yellow"
1617
onClick={async () => {
1718
"use server";
1819

@@ -24,6 +25,10 @@ export const PayloadSessionServer = async () => {
2425
Expires: {new Date(session.expires).toLocaleString()}
2526
</Badge>
2627
)}
28+
{session?.collection ? (
29+
<Badge variant="dark">Collection: {session.collection}</Badge>
30+
) : null}
31+
{session?.strategy ? <Badge variant="dark">Strategy: {session.strategy}</Badge> : null}
2732
</div>
2833
<pre className="overflow-auto rounded-lg bg-gray-100 p-4">
2934
{JSON.stringify(session?.user ?? null, null, 2)}

packages/dev/src/app/components/general/Badge.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@ import clsx from "clsx";
22
import { type ComponentPropsWithoutRef, type ReactNode } from "react";
33

44
interface Props extends ComponentPropsWithoutRef<"span"> {
5-
variant?: "default" | "red" | "green" | "yellow";
5+
variant: "dark" | "blue" | "red" | "green" | "yellow";
66
children: ReactNode;
77
}
88

9-
const Badge = ({ variant = "default", children, className, ...props }: Props) => {
9+
const Badge = ({ variant, children, className, ...props }: Props) => {
1010
return (
1111
<span
1212
className={clsx(
1313
"me-2 rounded-sm px-2.5 py-0.5 text-xs font-medium",
1414
{
15-
"bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-300": variant === "default",
15+
"bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-gray-300": variant === "dark",
16+
"bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-300": variant === "blue",
1617
"bg-red-100 text-red-800 dark:bg-red-900 dark:text-red-300": variant === "red",
1718
"bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-300": variant === "green",
1819
"bg-yellow-100 text-yellow-800 dark:bg-yellow-900 dark:text-yellow-300":

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,12 @@ export const PayloadSessionProvider: React.FC<Props<CollectionSlug>> = <
7171
const response = await fetch(`/api/${userCollectionSlug}/me`, {
7272
signal,
7373
});
74-
const result: { user: DataFromCollectionSlug<TSlug> | null; exp: number } =
75-
await response.json();
74+
const result: {
75+
user: DataFromCollectionSlug<TSlug> | null;
76+
exp: number;
77+
collection?: CollectionSlug;
78+
strategy?: string;
79+
} = await response.json();
7680

7781
// Set loading to false
7882
setIsLoading(false);
@@ -86,6 +90,8 @@ export const PayloadSessionProvider: React.FC<Props<CollectionSlug>> = <
8690
const localSession = {
8791
user: result.user,
8892
expires: new Date(result.exp * 1000).toISOString(),
93+
collection: result.collection,
94+
strategy: result.strategy,
8995
};
9096
setLocalSession(localSession);
9197

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { headers } from "next/headers";
22
import type { CollectionSlug, DataFromCollectionSlug } from "payload";
33
import { cache } from "react";
4+
import type { AUTHJS_STRATEGY_NAME } from "../AuthjsAuthStrategy";
45

56
interface Options<TSlug extends CollectionSlug> {
67
/**
@@ -14,6 +15,8 @@ interface Options<TSlug extends CollectionSlug> {
1415
export interface PayloadSession<TSlug extends CollectionSlug> {
1516
user: DataFromCollectionSlug<TSlug>;
1617
expires: string;
18+
collection?: CollectionSlug;
19+
strategy?: typeof AUTHJS_STRATEGY_NAME | "local-jwt" | "api-key" | ({} & string);
1720
}
1821

1922
/**
@@ -40,8 +43,12 @@ export const getPayloadSession = cache(
4043
tags: ["payload-session"],
4144
},
4245
});
43-
const result: { user: DataFromCollectionSlug<TSlug> | null; exp: number } =
44-
await response.json();
46+
const result: {
47+
user: DataFromCollectionSlug<TSlug> | null;
48+
exp: number;
49+
collection?: CollectionSlug;
50+
strategy?: string;
51+
} = await response.json();
4552

4653
// If the response is not ok or the user is not present, return null
4754
if (!response.ok || !result.user) {
@@ -52,6 +59,8 @@ export const getPayloadSession = cache(
5259
return {
5360
user: result.user,
5461
expires: new Date(result.exp * 1000).toISOString(),
62+
collection: result.collection,
63+
strategy: result.strategy,
5564
};
5665
},
5766
);

0 commit comments

Comments
 (0)