summaryrefslogtreecommitdiff
path: root/hooks/use-settings-access.ts
blob: cd7938ac359230883684fe2dbc3a66f2540f7513 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// hooks/use-settings-access.ts (개선된 버전)
"use client"

import * as React from "react"
import { useSession } from "next-auth/react"
import { useRouter } from "next/navigation"

// 인증 방식 타입
type AuthMethod = 'otp' | 'email' | 'sgips' | 'saml'

interface UseSettingsAccessOptions {
  // 재인증 유효 시간 (밀리초, 기본값: 5분)
  validDuration?: number
  // S-Gips 사용자 리다이렉트 경로
  sgipsRedirectPath?: string
}

type AccessType = 'allowed' | 'reauth_required' | 'blocked_sgips' | 'loading' | 'unauthenticated'

interface SettingsAccessState {
  accessType: AccessType
  showReAuthModal: boolean
  isAuthenticated: boolean
  userEmail: string
  userId: string
  userDomain: string | null
  authMethod: AuthMethod | undefined
  handleReAuthSuccess: () => Promise<void>
  forceReAuth: () => void
}

export function useSettingsAccess(options: UseSettingsAccessOptions = {}): SettingsAccessState {
  const { 
    validDuration = 5 * 60 * 1000,
    sgipsRedirectPath = "/dashboard"
  } = options
  
  const { data: session, status, update } = useSession()
  const router = useRouter()
  const [showReAuthModal, setShowReAuthModal] = React.useState(false)

  // 사용자의 접근 타입 결정 (인증 방식 기반)
  const accessType: AccessType = React.useMemo(() => {
    if (status === "loading") return 'loading'
    if (status === "unauthenticated") return 'unauthenticated'
    if (!session?.user) return 'unauthenticated'

    const authMethod = session.user.authMethod
    
    // 인증 방식에 따른 접근 제어
    switch (authMethod) {
      case 'sgips':
        // S-Gips 사용자는 접근 차단
        return 'blocked_sgips'
      
      case 'saml':
        // SAML 사용자는 바로 접근 허용
        return 'allowed'
      
      case 'otp':
        // OTP 사용자는 바로 접근 허용 (이미 2차 인증 완료)
        return 'allowed'
      
      case 'email':
        // 일반 이메일 사용자는 재인증 필요
        const reAuthTime = session.user.reAuthTime
        if (!reAuthTime) return 'reauth_required'

        const now = Date.now()
        const isReAuthValid = (now - reAuthTime) < validDuration
        
        return isReAuthValid ? 'allowed' : 'reauth_required'
      
      default:
        // 인증 방식이 불명확한 경우 재인증 요구
        console.warn('Unknown auth method:', authMethod)
        return 'reauth_required'
    }
  }, [session, status, validDuration])

  // S-Gips 사용자 자동 리다이렉트
  React.useEffect(() => {
    if (accessType === 'blocked_sgips') {
      router.push(sgipsRedirectPath)
    }
  }, [accessType, router, sgipsRedirectPath])

  // 재인증 필요 시 모달 표시
  React.useEffect(() => {
    setShowReAuthModal(accessType === 'reauth_required')
  }, [accessType])

  const handleReAuthSuccess = React.useCallback(async () => {
    await update({
      reAuthTime: Date.now()
    })
    setShowReAuthModal(false)
  }, [update])

  const forceReAuth = React.useCallback(async () => {
    await update({
      reAuthTime: null
    })
    setShowReAuthModal(true)
  }, [update])

  return {
    accessType,
    showReAuthModal,
    isAuthenticated: accessType === 'allowed',
    userEmail: session?.user?.email || "",
    userId: session?.user?.id || "",
    userDomain: session?.user?.domain || null,
    authMethod: session?.user?.authMethod,
    handleReAuthSuccess,
    forceReAuth,
  }
}