0

I use next auth version 4 with next for client side authentication, I want a modal to display when the session is expired. The value open (In the code ) always remains false even though the session was expired. Here's my code :

    const [open,setOpen] = useState(false);

    const fetchMySession = useCallback( async   () =>{
        const session = await getSession({});
        setOpen(!session?.user)
        console.log("Session Dialog",session,!session)
      }, [])
    
    useEffect(() => {
        console.log("Call fetch my API ")
        fetchMySession()
        
    }, [fetchMySession])
sokida
  • 433
  • 1
  • 8
  • 21
  • Do you have any code that triggers when the session has expired? What is calling `fetchMySession`? – juliomalves Jan 24 '22 at 21:33
  • fetchMySession is the function that check if session has been expired or not, normally session expiration done with next auth when we set the expiration session date to a value – sokida Jan 25 '22 at 08:41
  • 1
    I understand, but what's triggering `fetchMySession` to check the session has expired? Do you have a pooling mechanism? Or a callback maybe? This code by itself will fire `fetchMySession` once when the component it's in gets mounted. – juliomalves Jan 25 '22 at 08:57
  • I were thinking that if i add fetchMySession to the second argument of useEffect ,It will update the open value when getSession change value but i see the problem ,You have any idea how can i trigger my getSession when session timeout ? Excuse me for my language – sokida Jan 25 '22 at 09:22
  • 2
    You could look into passing up a [`refetchInterval`](https://next-auth.js.org/getting-started/client#refetch-interval) option to the `SessionProvider`, and avoid session expiry completely. – juliomalves Jan 25 '22 at 09:35
  • If i have understand correctly, I will add refetchInterval with the value of expiration date of the session. For example if i have the max age: 5*60 then refetchInterval = 5*60? – sokida Jan 25 '22 at 09:51
  • 2
    As the docs state: _"The value for `refetchInterval` should always be lower than the value of the session `maxAge` session option"_. – juliomalves Jan 25 '22 at 10:55
  • I got it thank you so much sir – sokida Jan 25 '22 at 13:55

0 Answers0