How to handle infinite redirect in navigation guard?

Hello everyone, Can anyone help me out about the issue infinite redirect when we want to make middleware for the authentication user. So I want when the user has already logged in and then try to access login page again then they hasn't access to it.

My code:
// @ts-nocheck
export default defineNuxtRouteMiddleware((to, from) => {
    const { isAuthenticated } = useAuth();

    // Check if the user is authenticated
    if (!isAuthenticated()) {
        return navigateTo('/auth/login')
    } else {
        return navigateTo('/homepage')
    }
})
Was this page helpful?