Skip to content

Commit f53e4f2

Browse files
authored
fix: perform log out server action with state (#23)
* feat: sign out server action * fix: logout action
1 parent 09b1bb8 commit f53e4f2

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

src/actions.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use server';
2+
3+
import { signOut } from '@/auth';
4+
import { SignOutParams } from 'next-auth/react';
5+
6+
export async function signOutAction(params: SignOutParams) {
7+
await signOut({ ...params });
8+
}

src/app/(app)/[repo]/page.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ export async function generateMetadata({ params }: Props): Promise<Metadata> {
2222
export default async function Page({ params }: Props) {
2323
const { repo: rawRepo } = await params;
2424
const repo = decodeURIComponent(rawRepo);
25-
console.log(repo);
2625

2726
// check if it has prefix '@'
2827
// otherwise call notFount

src/app/(app)/_components/header/user-dropdown.tsx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use client';
22

3+
import { signOutAction } from '@/actions';
34
import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar';
45
import {
56
DropdownMenu,
@@ -9,13 +10,17 @@ import {
910
DropdownMenuShortcut,
1011
DropdownMenuTrigger,
1112
} from '@/components/ui/dropdown-menu';
13+
import { Spinner } from '@/components/ui/spinner';
1214
import { ExternalLink, LogOutIcon } from 'lucide-react';
1315
import { useSession } from 'next-auth/react';
16+
import { useState } from 'react';
1417

1518
export default function UserDropdown() {
1619
const { data: session } = useSession();
1720
const { username, name, email, image } = session?.user ?? {};
1821

22+
const [isLoggingOut, setIsLoggingOut] = useState(false);
23+
1924
return (
2025
<DropdownMenu>
2126
<DropdownMenuTrigger>
@@ -40,10 +45,21 @@ export default function UserDropdown() {
4045
</DropdownMenuShortcut>
4146
</DropdownMenuItem>
4247
<DropdownMenuSeparator />
43-
<DropdownMenuItem className="cursor-pointer">
48+
<DropdownMenuItem
49+
className="cursor-pointer"
50+
disabled={isLoggingOut}
51+
onClick={async () => {
52+
setIsLoggingOut(true);
53+
await signOutAction({ redirectTo: '/login' });
54+
}}
55+
>
4456
Log out
4557
<DropdownMenuShortcut>
46-
<LogOutIcon className="size-4" />
58+
{isLoggingOut ? (
59+
<Spinner className="bg-foreground size-4" />
60+
) : (
61+
<LogOutIcon className="size-4" />
62+
)}
4763
</DropdownMenuShortcut>
4864
</DropdownMenuItem>
4965
</DropdownMenuContent>

src/app/(app)/new/_components/repo-list.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export default function RepoList() {
5858
const { data: session, status } = useSession();
5959
const accessToken = session?.accessToken;
6060
const username = session?.user?.username;
61-
console.log(session?.user);
61+
6262
const [search, setSearch] = useState('');
6363
const debouncedSearch = useDebounce(search, 500);
6464

0 commit comments

Comments
 (0)