Skip to content

How to automatically log out of user's browser after a long period of inactivity or after specific time #6401

Answered by kn327
cha2hyun asked this question in Help
Discussion options

You must be logged in to vote

Usually, this is outside the scope of something like this tool as it requires adding multiple event listeners...

What I would recommend doing is building a wrapper around your app, like when you do theming or user contexts which manages this for you

The below is untested, but could help you getting in the right direction:

import { signOut, useSession } from "next-auth/react";
import { useEffect, useState } from "react";

export interface AutoLogoutProviderProps {
    timeoutMs?: number;
    timeoutCheckMs?: number;
    debug?: boolean;
    requireSession?: boolean;
}

type WindowActivityEvent = keyof WindowEventMap;

export function AutoLogoutProvider({
    timeoutMs,
    timeoutCheckMs =…

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@cha2hyun
Comment options

@angelitolm
Comment options

@SpasiboKojima
Comment options

@TomasBurianTheOne
Comment options

Answer selected by cha2hyun
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
5 participants