Skip to content

Commit 2b73399

Browse files
authored
Merge pull request #14 from TEAM-ROMROM/20250916_#6_기능추가_인증_Nextjs_로그아웃_연동
20250916 #6 기능추가 인증 nextjs 로그아웃 연동
2 parents 250eeca + d2e43cd commit 2b73399

5 files changed

Lines changed: 71 additions & 10 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import httpClient from "@/lib/http-client";
2+
import { CustomError } from "@/lib/error/custom-error";
3+
import { ErrorCode } from "@/lib/error/error-code";
4+
5+
export async function logout(): Promise<void> {
6+
const api = httpClient<Response>();
7+
const res = await api("/api/logout", {
8+
method: "POST"
9+
});
10+
if (!res.ok) {
11+
throw new CustomError(ErrorCode.INVALID_REQUEST);
12+
}
13+
}

src/app/api/login/route.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import { LoginRequest, TokenPair } from "@/app/(without-layout)/login/_shared/se
33
import { CustomError } from "@/lib/error/custom-error";
44
import { ErrorCode } from "@/lib/error/error-code";
55
import { isLocalhost } from "@/lib/utils";
6-
7-
const API_BASE_URL = process.env.API_BASE_URL ?? '';
6+
import { API_BASE_URL } from "@/lib/api/types";
87

98
export async function POST(req: NextRequest): Promise<Response> {
109
const ctx = req.headers.get('content-type') || '';

src/app/api/logout/route.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { API_BASE_URL } from "@/lib/api/types";
2+
import { CustomError } from "@/lib/error/custom-error";
3+
import { ErrorCode } from "@/lib/error/error-code";
4+
5+
export async function POST(): Promise<Response> {
6+
const form = new FormData();
7+
try {
8+
return await fetch(`${API_BASE_URL}/api/admin/logout`, {
9+
method: 'POST',
10+
body: form
11+
});
12+
} catch {
13+
throw new CustomError(ErrorCode.INVALID_REQUEST);
14+
}
15+
}

src/components/common/app-sidebar.tsx

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"use client"
2+
13
import {
24
Sidebar,
35
SidebarContent,
@@ -10,9 +12,13 @@ import {
1012
SidebarMenuButton,
1113
SidebarMenuItem
1214
} from "@/components/ui/sidebar";
13-
import { Home, Inbox, Settings, User } from "lucide-react";
15+
import { Home, Inbox, User } from "lucide-react";
1416
import Image from "next/image";
1517
import Link from "next/link";
18+
import { Button } from "@/components/ui/button";
19+
import { logout } from "@/app/(with-layout)/_shared/services/logout-api";
20+
import { useRouter } from "next/navigation";
21+
import { useState } from "react";
1622

1723
// Menu items.
1824
const items = [
@@ -30,15 +36,28 @@ const items = [
3036
title: "회원관리",
3137
url: "/member",
3238
icon: User,
33-
},
34-
{
35-
title: "로그아웃",
36-
url: "/logout",
37-
icon: Settings
3839
}
3940
]
4041

4142
export default function AppSidebar() {
43+
const router = useRouter();
44+
const [loading, setLoading] = useState<boolean>(false);
45+
46+
const onClickLogout = async () => {
47+
if (loading) {
48+
return;
49+
}
50+
setLoading(true);
51+
try {
52+
await logout();
53+
localStorage.removeItem('accessToken');
54+
localStorage.removeItem('refreshToken');
55+
router.replace('/login');
56+
} finally {
57+
setLoading(false);
58+
}
59+
}
60+
4261
return (
4362
<Sidebar>
4463
<SidebarHeader>
@@ -66,7 +85,19 @@ export default function AppSidebar() {
6685
</SidebarGroupContent>
6786
</SidebarGroup>
6887
</SidebarContent>
69-
<SidebarFooter />
88+
<SidebarFooter>
89+
<SidebarMenuItem key="로그아웃">
90+
<SidebarMenuButton asChild>
91+
<Button
92+
className="hover:cursor-pointer"
93+
onClick={onClickLogout}
94+
disabled={loading}
95+
>
96+
{loading ? "로그아웃 중..." : "로그아웃"}
97+
</Button>
98+
</SidebarMenuButton>
99+
</SidebarMenuItem>
100+
</SidebarFooter>
70101
</Sidebar>
71-
)
102+
);
72103
};

src/lib/api/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const API_BASE_URL = process.env.API_BASE_URL
2+
3+
export { API_BASE_URL }

0 commit comments

Comments
 (0)