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
6 changes: 6 additions & 0 deletions apps/admin/src/app/auth/login/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ export default function AuthLoginPage() {
let isMounted = true;

const redirectIfAuthenticated = async () => {
const shouldSkipSessionCheck = new URLSearchParams(window.location.search).get("loggedOut") === "1";
if (shouldSkipSessionCheck) {
setCanRenderLogin(true);
return;
}

try {
const token = await ensureSessionToken();
if (!isMounted) return;
Expand Down
33 changes: 27 additions & 6 deletions apps/admin/src/components/layout/AdminLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
"use client";

import { LogOut } from "lucide-react";
import { useState } from "react";
import { toast } from "sonner";
import { adminSignOutApi } from "@/lib/api/auth";
import { clearSession } from "@/lib/auth/session";
import { type ActiveAdminMenu, AdminSidebar } from "./AdminSidebar";

Expand All @@ -11,10 +15,26 @@ interface AdminLayoutProps {
}

export function AdminLayout({ children, activeMenu, title, description }: AdminLayoutProps) {
const handleLogout = () => {
clearSession();
toast.success("로그아웃되었습니다.");
window.location.assign("/auth/login");
const [isLoggingOut, setIsLoggingOut] = useState(false);

const handleLogout = async () => {
if (isLoggingOut) {
return;
}

setIsLoggingOut(true);

try {
await adminSignOutApi();
toast.success("로그아웃되었습니다.");
} catch {
toast.error("서버 로그아웃 요청에 실패했습니다.", {
description: "로컬 세션을 정리하고 로그인 화면으로 이동합니다.",
});
} finally {
clearSession();
window.location.assign("/auth/login?loggedOut=1");
}
};

return (
Expand All @@ -35,10 +55,11 @@ export function AdminLayout({ children, activeMenu, title, description }: AdminL
<button
type="button"
onClick={handleLogout}
className="inline-flex items-center gap-1 rounded-md border border-k-200 px-3 py-1.5 text-k-700 typo-medium-4 hover:bg-k-50"
disabled={isLoggingOut}
className="inline-flex items-center gap-1 rounded-md border border-k-200 px-3 py-1.5 text-k-700 typo-medium-4 hover:bg-k-50 disabled:cursor-not-allowed disabled:opacity-60"
>
<LogOut className="h-4 w-4" />
로그아웃
{isLoggingOut ? "로그아웃 중..." : "로그아웃"}
</button>
</div>
</header>
Expand Down
9 changes: 9 additions & 0 deletions apps/admin/src/lib/api/auth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import axios, { type AxiosResponse } from "axios";
import { loadAccessToken } from "@/lib/utils/localStorage";
import type { AdminSignInResponse, ReissueAccessTokenResponse } from "@/types/auth";

const API_SERVER_URL = import.meta.env.VITE_API_SERVER_URL?.trim();
Expand All @@ -17,3 +18,11 @@ export const adminSignInApi = (email: string, password: string): Promise<AxiosRe

export const reissueAccessTokenApi = (): Promise<AxiosResponse<ReissueAccessTokenResponse>> =>
authAxiosInstance.post("/auth/reissue");

export const adminSignOutApi = (): Promise<AxiosResponse<void>> => {
const accessToken = loadAccessToken();

return authAxiosInstance.post("/auth/sign-out", undefined, {
headers: accessToken ? { Authorization: `Bearer ${accessToken}` } : undefined,
});
};
9 changes: 9 additions & 0 deletions apps/admin/src/lib/auth/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { isTokenExpired } from "@/lib/utils/jwtUtils";
import { loadAccessToken, removeAccessToken, saveAccessToken } from "@/lib/utils/localStorage";

let reissuePromise: Promise<string | null> | null = null;
let sessionVersion = 0;

const getValidAccessToken = (): string | null => {
const accessToken = loadAccessToken();
Expand All @@ -19,6 +20,8 @@ const getValidAccessToken = (): string | null => {
};

export const clearSession = () => {
sessionVersion += 1;
reissuePromise = null;
removeAccessToken();
};

Expand All @@ -27,11 +30,17 @@ export const reissueAccessTokenIfPossible = async (): Promise<string | null> =>
return reissuePromise;
}

const reissueSessionVersion = sessionVersion;

reissuePromise = (async () => {
try {
const response = await reissueAccessTokenApi();
const nextAccessToken = response.data.accessToken;

if (reissueSessionVersion !== sessionVersion) {
return null;
}

if (!nextAccessToken) {
clearSession();
return null;
Expand Down
Loading