summaryrefslogtreecommitdiff
path: root/components/layout/SessionManager.tsx
blob: c917c5f35e57f33f76e437f4361522db91004710 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// components/layout/SessionManager.tsx
'use client'

import { useSession } from "next-auth/react"
import { useEffect, useState, useCallback } from "react"
import { useRouter } from "next/navigation"
import { AlertCircle, Clock, RefreshCw, X } from "lucide-react"
import { Button } from "@/components/ui/button"
import { Alert, AlertDescription } from "@/components/ui/alert"
import { Progress } from "@/components/ui/progress"
import { useToast } from "@/hooks/use-toast"
import { Card, CardContent } from "@/components/ui/card"
import { cn } from "@/lib/utils"

interface SessionManagerProps {
  lng: string;
}

// 다국어 메시지
const messages = {
  ko: {
    sessionExpiring: "세션 만료 경고",
    sessionWillExpire: "세션이 {minutes}분 후에 만료됩니다.",
    sessionExpired: "세션이 만료되었습니다",
    pleaseRelogin: "다시 로그인해주세요.",
    extend: "연장",
    extending: "연장 중...",
    close: "닫기",
    sessionExtended: "세션이 연장되었습니다",
    sessionExtendFailed: "세션 연장에 실패했습니다",
    autoLogoutIn: "{seconds}초 후 자동 로그아웃됩니다",
    staySignedIn: "로그인 유지",
    logout: "로그아웃"
  },
  en: {
    sessionExpiring: "Session Expiring",
    sessionWillExpire: "Your session will expire in {minutes} minute(s).",
    sessionExpired: "Session Expired",
    pleaseRelogin: "Please log in again.",
    extend: "Extend",
    extending: "Extending...",
    close: "Close",
    sessionExtended: "Session has been extended",
    sessionExtendFailed: "Failed to extend session",
    autoLogoutIn: "Auto logout in {seconds} seconds",
    staySignedIn: "Stay Signed In",
    logout: "Logout"
  }
} as const;

export function SessionManager({ lng }: SessionManagerProps) {
  const { data: session, update } = useSession()
  const router = useRouter()
  const { toast } = useToast()
  
  const [showWarning, setShowWarning] = useState(false)
  const [showExpiredModal, setShowExpiredModal] = useState(false)
  const [isExtending, setIsExtending] = useState(false)
  const [autoLogoutCountdown, setAutoLogoutCountdown] = useState(0)
  const [timeLeft, setTimeLeft] = useState<number | null>(null)

  const t = messages[lng as keyof typeof messages] || messages.en

  // 세션 연장 함수
  const extendSession = useCallback(async () => {
    if (isExtending) return;
    
    setIsExtending(true)
    try {
      await update({
        reAuthTime: Date.now()
      })
      
      setShowWarning(false)
      setTimeLeft(null)
      
      toast({
        title: t.sessionExtended,
        description: "세션이 성공적으로 연장되었습니다.",
        duration: 3000,
      })
    } catch (error) {
      console.error('Failed to extend session:', error)
      toast({
        title: t.sessionExtendFailed,
        description: "다시 시도해주세요.",
        variant: "destructive",
        duration: 5000,
      })
    } finally {
      setIsExtending(false)
    }
  }, [isExtending, update, toast, t])

  // 자동 로그아웃 처리
  const handleAutoLogout = useCallback(() => {
    setShowExpiredModal(false)
    setShowWarning(false)
    window.location.href = `/${lng}/evcp?reason=expired`
  }, [lng])

  // 세션 만료 체크
  useEffect(() => {
    if (!session?.user?.sessionExpiredAt) return

    const checkSession = () => {
      const now = Date.now()
      const expiresAt = session.user.sessionExpiredAt!
      const timeUntilExpiry = expiresAt - now
      const warningThreshold = 5 * 60 * 1000 // 5분
      const criticalThreshold = 1 * 60 * 1000 // 1분

      setTimeLeft(timeUntilExpiry)

      // 세션 만료됨
      if (timeUntilExpiry <= 0) {
        setShowWarning(false)
        setShowExpiredModal(true)
        setAutoLogoutCountdown(10) // 10초 후 자동 로그아웃
        return
      }

      // 1분 이내 - 긴급 경고
      if (timeUntilExpiry <= criticalThreshold) {
        setShowWarning(true)
        return
      }

      // 5분 이내 - 일반 경고
      if (timeUntilExpiry <= warningThreshold && !showWarning) {
        setShowWarning(true)
        return
      }

      // 경고 해제
      if (timeUntilExpiry > warningThreshold && showWarning) {
        setShowWarning(false)
      }
    }

    // 즉시 체크
    checkSession()

    // 5초마다 체크 (더 정확한 카운트다운을 위해)
    const interval = setInterval(checkSession, 5000)

    return () => clearInterval(interval)
  }, [session, showWarning])

  // 자동 로그아웃 카운트다운
  useEffect(() => {
    if (autoLogoutCountdown <= 0) return

    const timer = setTimeout(() => {
      if (autoLogoutCountdown === 1) {
        handleAutoLogout()
      } else {
        setAutoLogoutCountdown(prev => prev - 1)
      }
    }, 1000)

    return () => clearTimeout(timer)
  }, [autoLogoutCountdown, handleAutoLogout])

  // 사용자 활동 감지
  useEffect(() => {
    let activityTimer: NodeJS.Timeout
    let lastActivity = Date.now()

    const resetActivityTimer = () => {
      const now = Date.now()
      const timeSinceLastActivity = now - lastActivity
      
      // 5분 이상 비활성 후 첫 활동이면 세션 연장
      if (timeSinceLastActivity > 5 * 60 * 1000) {
        extendSession()
      }
      
      lastActivity = now
      clearTimeout(activityTimer)
      
      // 10분간 비활성이면 경고 표시
      activityTimer = setTimeout(() => {
        if (!showWarning && session?.user?.sessionExpiredAt) {
          const timeUntilExpiry = session.user.sessionExpiredAt - Date.now()
          if (timeUntilExpiry > 0 && timeUntilExpiry <= 10 * 60 * 1000) {
            setShowWarning(true)
          }
        }
      }, 10 * 60 * 1000)
    }

    const activities = ['mousedown', 'keydown', 'scroll', 'touchstart', 'click']
    
    activities.forEach(activity => {
      document.addEventListener(activity, resetActivityTimer, true)
    })

    resetActivityTimer() // 초기 타이머 설정

    return () => {
      clearTimeout(activityTimer)
      activities.forEach(activity => {
        document.removeEventListener(activity, resetActivityTimer, true)
      })
    }
  }, [extendSession, showWarning, session])

  const formatTime = (ms: number) => {
    const minutes = Math.floor(ms / (1000 * 60))
    const seconds = Math.floor((ms % (1000 * 60)) / 1000)
    return { minutes, seconds }
  }

  // 세션 만료 모달
  if (showExpiredModal) {
    return (
      <div className="fixed inset-0 bg-black/50 flex items-center justify-center z-[100]">
        <Card className="w-full max-w-md mx-4">
          <CardContent className="p-6">
            <div className="flex items-center space-x-3 mb-4">
              <AlertCircle className="h-6 w-6 text-destructive" />
              <h3 className="text-lg font-semibold">{t.sessionExpired}</h3>
            </div>
            
            <p className="text-muted-foreground mb-4">
              {t.pleaseRelogin}
            </p>
            
            {autoLogoutCountdown > 0 && (
              <div className="mb-4">
                <p className="text-sm text-muted-foreground mb-2">
                  {t.autoLogoutIn.replace('{seconds}', autoLogoutCountdown.toString())}
                </p>
                <Progress 
                  value={(10 - autoLogoutCountdown) * 10} 
                  className="h-2" 
                />
              </div>
            )}
            
            <div className="flex space-x-2">
              <Button 
                onClick={() => {
                  setAutoLogoutCountdown(0)
                  extendSession()
                  setShowExpiredModal(false)
                }}
                className="flex-1"
                disabled={isExtending}
              >
                {isExtending ? (
                  <>
                    <RefreshCw className="h-4 w-4 mr-2 animate-spin" />
                    {t.extending}
                  </>
                ) : (
                  t.staySignedIn
                )}
              </Button>
              <Button 
                variant="outline" 
                onClick={handleAutoLogout}
                className="flex-1"
              >
                {t.logout}
              </Button>
            </div>
          </CardContent>
        </Card>
      </div>
    )
  }

  // 세션 경고 알림
  if (showWarning && timeLeft) {
    const { minutes, seconds } = formatTime(timeLeft)
    const isCritical = timeLeft <= 60000 // 1분 이내
    const progressValue = Math.max(0, Math.min(100, (timeLeft / (5 * 60 * 1000)) * 100))

    return (
      <div className={cn(
        "fixed top-4 right-4 z-50 w-full max-w-sm",
        "animate-in slide-in-from-right-full duration-300"
      )}>
        <Alert className={cn(
          "border-2 shadow-lg",
          isCritical 
            ? "border-destructive bg-destructive/5" 
            : "border-warning bg-warning/5"
        )}>
          <Clock className={cn(
            "h-4 w-4",
            isCritical ? "text-destructive" : "text-warning"
          )} />
          
          <div className="flex-1">
            <div className="flex items-center justify-between mb-2">
              <h4 className="font-medium text-sm">
                {t.sessionExpiring}
              </h4>
              <Button
                variant="ghost"
                size="sm"
                className="h-auto p-1 hover:bg-transparent"
                onClick={() => setShowWarning(false)}
              >
                <X className="h-3 w-3" />
              </Button>
            </div>
            
            <AlertDescription className="text-xs mb-3">
              {minutes > 0 
                ? t.sessionWillExpire.replace('{minutes}', minutes.toString())
                : `${seconds}초 후 세션이 만료됩니다.`
              }
            </AlertDescription>
            
            <div className="space-y-2">
              <Progress 
                value={progressValue} 
                className={cn(
                  "h-1.5",
                  isCritical && "bg-destructive/20"
                )}
              />
              
              <div className="flex space-x-2">
                <Button
                  size="sm"
                  onClick={extendSession}
                  disabled={isExtending}
                  className="flex-1 h-7 text-xs"
                >
                  {isExtending ? (
                    <>
                      <RefreshCw className="h-3 w-3 mr-1 animate-spin" />
                      {t.extending}
                    </>
                  ) : (
                    t.extend
                  )}
                </Button>
                <Button
                  variant="outline"
                  size="sm"
                  onClick={() => setShowWarning(false)}
                  className="h-7 text-xs px-2"
                >
                  {t.close}
                </Button>
              </div>
            </div>
          </div>
        </Alert>
      </div>
    )
  }

  return null
}