summaryrefslogtreecommitdiff
path: root/app/api/auth/util.ts
blob: 9163c1c56d18dc82d61efe1dfad52c070c078cac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { signOut } from 'next-auth/react'

export async function completeLogout() {
  // 1. NextAuth 로그아웃
  await signOut({ redirect: false })
  
  // 2. 모든 NextAuth 관련 쿠키 제거
  const cookies = [
    'next-auth.session-token',
    '__Secure-next-auth.session-token',
    'next-auth.csrf-token',
    '__Host-next-auth.csrf-token',
    'next-auth.callback-url',
    '__Secure-next-auth.callback-url'
  ]
  
  cookies.forEach(cookieName => {
    document.cookie = `${cookieName}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`
    document.cookie = `${cookieName}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; secure`
    document.cookie = `${cookieName}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; domain=${window.location.hostname}`
  })
  
  // 3. 로컬 스토리지와 세션 스토리지 클리어
  localStorage.clear()
  sessionStorage.clear()
  
  // 4. 강제 페이지 리로드로 모든 캐시 제거
  window.location.href = '/ko/evcp'
}

// 사용자 도메인 변경 후 호출할 함수
export async function refreshUserSession() {
  try {
    const response = await fetch('/api/auth/refresh-user', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
    })
    
    if (response.ok) {
      // 성공하면 페이지 리로드
      window.location.reload()
    } else {
      // 실패하면 완전 로그아웃
      await completeLogout()
    }
  } catch (error) {
    console.error('Failed to refresh session:', error)
    await completeLogout()
  }
}