Session Time Out In Firebase

Session Time Out In Firebase using idle timer, When the user is idle for 20 min we will be logging out the customer. The sample Code is provided below

import { useEffect, useState } from 'react'
import { auth } from "../../firebase";
import { useIdleTimer } from 'react-idle-timer'



export default function AuthCheck() {
    const [state, setState] = useState('Active')
    const [count, setCount] = useState(0)
    const [remaining, setRemaining] = useState(0)


    const onIdle = () => {
        if (auth.currentUser) {
            auth.signOut()
                .then(() => {
                    console.log('User signed out successfully');
                })
                .catch((error) => {
                    console.error('Error signing out:', error);
                });
        }
        setState('Idle', state)
    }


    const onActive = () => {
        setState('Active')
    }


    const onAction = () => {
        setCount(count + 1)
    }


    const { getRemainingTime } = useIdleTimer({
        onIdle,
        onActive,
        onAction,
        timeout: 1200000,
        throttle: 500
    })


    useEffect(() => {
        const interval = setInterval(() => {
            setRemaining(Math.ceil(getRemainingTime() / 1000))
        }, 500)


        return () => {
            clearInterval(interval)
        }
    }, [getRemainingTime])
    return null
}


Leave a comment

Your email address will not be published. Required fields are marked *