summaryrefslogtreecommitdiff
path: root/lib/auth/custom-signout.ts
diff options
context:
space:
mode:
authorjoonhoekim <26rote@gmail.com>2025-10-30 21:21:29 +0900
committerjoonhoekim <26rote@gmail.com>2025-10-30 21:21:29 +0900
commit9d77688b3fbce108e170e0f874fbd9da66fd25d1 (patch)
treeed6d8462c2c1ab3bbf932bb50309a1df308821f9 /lib/auth/custom-signout.ts
parent788eb678c45c6d3767cd2679c41ad5387ae6c3f0 (diff)
(김준회) 멀티도메인 대응 로그아웃 커스텀 처리, PO 생성 서버액션 연결
Diffstat (limited to 'lib/auth/custom-signout.ts')
-rw-r--r--lib/auth/custom-signout.ts51
1 files changed, 51 insertions, 0 deletions
diff --git a/lib/auth/custom-signout.ts b/lib/auth/custom-signout.ts
new file mode 100644
index 00000000..d59bd81c
--- /dev/null
+++ b/lib/auth/custom-signout.ts
@@ -0,0 +1,51 @@
+/**
+ * 커스텀 로그아웃 함수
+ * NextAuth의 signOut이 NEXTAUTH_URL로 강제 리다이렉트하는 문제를 해결하기 위해 직접 구현
+ */
+
+interface CustomSignOutOptions {
+ callbackUrl?: string;
+ redirect?: boolean;
+}
+
+/**
+ * 커스텀 로그아웃 함수
+ *
+ * @param options - callbackUrl: 로그아웃 후 이동할 URL (기본: 현재 origin + "/")
+ * @param options - redirect: 자동 리다이렉트 여부 (기본: true)
+ */
+export async function customSignOut(options?: CustomSignOutOptions): Promise<void> {
+ const { callbackUrl, redirect = true } = options || {};
+
+ try {
+ // 1. CSRF 토큰 가져오기
+ const csrfResponse = await fetch('/api/auth/csrf');
+ const { csrfToken } = await csrfResponse.json();
+
+ // 2. 서버에 로그아웃 요청
+ await fetch('/api/auth/signout', {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/x-www-form-urlencoded',
+ },
+ body: new URLSearchParams({
+ csrfToken,
+ json: 'true',
+ }),
+ });
+
+ // 3. 리다이렉트
+ if (redirect) {
+ const finalUrl = callbackUrl || window.location.origin;
+ window.location.href = finalUrl;
+ }
+ } catch (error) {
+ console.error('Custom sign out error:', error);
+ // 에러 발생 시에도 리다이렉트 (세션이 이미 만료되었을 수 있음)
+ if (redirect) {
+ const finalUrl = callbackUrl || window.location.origin;
+ window.location.href = finalUrl;
+ }
+ }
+}
+