diff options
Diffstat (limited to 'lib/auth/custom-signout.ts')
| -rw-r--r-- | lib/auth/custom-signout.ts | 51 |
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; + } + } +} + |
