Files
parakasa/components/dashboard/auto-logout-handler.tsx
2026-01-29 16:49:51 +03:00

61 lines
1.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"use client"
import { useEffect, useCallback, useRef } from "react"
import { useRouter } from "next/navigation"
import { createClient } from "@/lib/supabase-browser"
import { toast } from "sonner"
const INACTIVITY_TIMEOUT = 15 * 60 * 1000 // 15 minutes
export function AutoLogoutHandler() {
const router = useRouter()
const supabase = createClient()
const timerRef = useRef<NodeJS.Timeout | null>(null)
const handleLogout = useCallback(async () => {
await supabase.auth.signOut()
toast.info("Oturumunuz uzun süre işlem yapılmadığı için sonlandırıldı.")
router.push("/login")
router.refresh()
}, [router, supabase])
const resetTimer = useCallback(() => {
if (timerRef.current) {
clearTimeout(timerRef.current)
}
timerRef.current = setTimeout(handleLogout, INACTIVITY_TIMEOUT)
}, [handleLogout])
useEffect(() => {
// Events to listen for
const events = [
"mousedown",
"mousemove",
"keydown",
"scroll",
"touchstart",
]
// Initial set
resetTimer()
// Event listener wrapper to debounce slightly/reset
const onUserActivity = () => {
resetTimer()
}
events.forEach((event) => {
window.addEventListener(event, onUserActivity)
})
return () => {
if (timerRef.current) clearTimeout(timerRef.current)
events.forEach((event) => {
window.removeEventListener(event, onUserActivity)
})
}
}, [resetTimer])
return null
}