Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/app/(with-layout)/_shared/services/logout-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import httpClient from "@/lib/http-client";
import { CustomError } from "@/lib/error/custom-error";
import { ErrorCode } from "@/lib/error/error-code";

export async function logout(): Promise<void> {
const api = httpClient<Response>();
const res = await api("/api/logout", {
method: "POST"
});
if (!res.ok) {
throw new CustomError(ErrorCode.INVALID_REQUEST);
}
}
3 changes: 1 addition & 2 deletions src/app/api/login/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { LoginRequest, TokenPair } from "@/app/(without-layout)/login/_shared/se
import { CustomError } from "@/lib/error/custom-error";
import { ErrorCode } from "@/lib/error/error-code";
import { isLocalhost } from "@/lib/utils";

const API_BASE_URL = process.env.API_BASE_URL ?? '';
import { API_BASE_URL } from "@/lib/api/types";

export async function POST(req: NextRequest): Promise<Response> {
const ctx = req.headers.get('content-type') || '';
Expand Down
15 changes: 15 additions & 0 deletions src/app/api/logout/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { API_BASE_URL } from "@/lib/api/types";
import { CustomError } from "@/lib/error/custom-error";
import { ErrorCode } from "@/lib/error/error-code";

export async function POST(): Promise<Response> {
const form = new FormData();
try {
return await fetch(`${API_BASE_URL}/api/admin/logout`, {
method: 'POST',
body: form
});
} catch {
throw new CustomError(ErrorCode.INVALID_REQUEST);
}
}
47 changes: 39 additions & 8 deletions src/components/common/app-sidebar.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use client"

import {
Sidebar,
SidebarContent,
Expand All @@ -10,9 +12,13 @@ import {
SidebarMenuButton,
SidebarMenuItem
} from "@/components/ui/sidebar";
import { Home, Inbox, Settings, User } from "lucide-react";
import { Home, Inbox, User } from "lucide-react";
import Image from "next/image";
import Link from "next/link";
import { Button } from "@/components/ui/button";
import { logout } from "@/app/(with-layout)/_shared/services/logout-api";
import { useRouter } from "next/navigation";
import { useState } from "react";

// Menu items.
const items = [
Expand All @@ -30,15 +36,28 @@ const items = [
title: "회원관리",
url: "/member",
icon: User,
},
{
title: "로그아웃",
url: "/logout",
icon: Settings
}
]

export default function AppSidebar() {
const router = useRouter();
const [loading, setLoading] = useState<boolean>(false);

const onClickLogout = async () => {
if (loading) {
return;
}
setLoading(true);
try {
await logout();
localStorage.removeItem('accessToken');
localStorage.removeItem('refreshToken');
router.replace('/login');
} finally {
setLoading(false);
}
}

return (
<Sidebar>
<SidebarHeader>
Expand Down Expand Up @@ -66,7 +85,19 @@ export default function AppSidebar() {
</SidebarGroupContent>
</SidebarGroup>
</SidebarContent>
<SidebarFooter />
<SidebarFooter>
<SidebarMenuItem key="로그아웃">
<SidebarMenuButton asChild>
<Button
className="hover:cursor-pointer"
onClick={onClickLogout}
disabled={loading}
>
{loading ? "로그아웃 중..." : "로그아웃"}
</Button>
</SidebarMenuButton>
</SidebarMenuItem>
</SidebarFooter>
</Sidebar>
)
);
};
3 changes: 3 additions & 0 deletions src/lib/api/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const API_BASE_URL = process.env.API_BASE_URL

export { API_BASE_URL }