Skip to content

Commit e69bfbe

Browse files
committed
example: Use authjs signOut server action (#43)
1 parent c2d6698 commit e69bfbe

File tree

3 files changed

+23
-53
lines changed

3 files changed

+23
-53
lines changed
Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,14 @@
1-
"use client";
1+
import { signOut } from "@/auth";
22

3-
import type { CollectionSlug } from "payload";
4-
5-
export function SignOutButton({
6-
userCollectionSlug = "users",
7-
}: {
8-
userCollectionSlug?: CollectionSlug;
9-
}) {
3+
export function SignOutButton() {
104
return (
11-
<button
12-
type="button"
13-
onClick={async () => {
14-
await fetch(`/api/${userCollectionSlug}/logout`, {
15-
method: "POST",
16-
headers: {
17-
"Content-Type": "application/json",
18-
},
19-
});
20-
window.location.reload();
5+
<form
6+
action={async () => {
7+
"use server";
8+
await signOut();
219
}}
2210
>
23-
Sign Out
24-
</button>
11+
<button type="submit">Sign Out</button>
12+
</form>
2513
);
2614
}
Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
1-
"use client";
1+
import { signOut } from "@/auth.customers";
22

33
export function SignOutButton() {
44
return (
5-
<button
6-
type="button"
7-
onClick={async () => {
8-
await fetch(`/api/customers/logout`, {
9-
method: "POST",
10-
headers: {
11-
"Content-Type": "application/json",
12-
},
13-
});
14-
window.location.reload();
5+
<form
6+
action={async () => {
7+
"use server";
8+
await signOut();
159
}}
1610
>
17-
Sign Out (Customer)
18-
</button>
11+
<button type="submit">Sign Out (Customer)</button>
12+
</form>
1913
);
2014
}
Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,14 @@
1-
"use client";
1+
import { signOut } from "@/auth";
22

3-
import type { CollectionSlug } from "payload";
4-
5-
export function SignOutButton({
6-
userCollectionSlug = "users",
7-
}: {
8-
userCollectionSlug?: CollectionSlug;
9-
}) {
3+
export function SignOutButton() {
104
return (
11-
<button
12-
type="button"
13-
onClick={async () => {
14-
await fetch(`/api/${userCollectionSlug}/logout`, {
15-
method: "POST",
16-
headers: {
17-
"Content-Type": "application/json",
18-
},
19-
});
20-
window.location.reload();
5+
<form
6+
action={async () => {
7+
"use server";
8+
await signOut();
219
}}
2210
>
23-
Sign Out
24-
</button>
11+
<button type="submit">Sign Out</button>
12+
</form>
2513
);
2614
}

0 commit comments

Comments
 (0)