What is the best approach to setting up dynamic Vue routing?

I am working on implementing dynamic routing for the website that relies on changes in agreements. Here is the path tree I have set up:

const routes = [
    {
        path: "/",
        redirect: "/home",
        component: DefaultLayout,
        meta: {
             requiresAuth: true,
        },
        children: [
            {
                path: "/home",
                name: "HomeView",
                component: HomeView,
            },
            {
                path: "/calendar",
                name: "CalendarView",
                component: CalendarView,
            },
            {
                path: "/notes",
                name: "NotesView",
                component: NotesView,
            },
            {
                path: "/settings",
                name: "SettingsView",
                component: SettingsView,
            },
        ],
    },
    {
        path: "/terms-of-use",
        name: "TermsOfUseView",
        component: TermsOfUseView,
        meta: {
            isGuest: true,
        },
    },
    {
        path: "/agreement",
        redirect: "/offer",
        component: AgreementLayout,
        children: [
            {
                path: "/offer",
                name: "OfferView",
                component: OfferView,
            },
            {
                path: "/public",
                name: "PublicView",
                component: PublicView,
            },
        ],
    },
    {
        path: "/auth",
        name: "AuthorizationView",
        component: AuthorizationView,
        meta: {
            requiresAuth: true,
        },
    },
    {
        path: "/plug",
        name: "PlugView",
        component: PlugView,
    },
];

Currently, I am only tracking changes in agreements.

const agreement = computed(() => store.state.agreement);

In addition, I have a hook for navigation:

router.beforeEach((to, from, next) => {
    if (to.meta.requiresAuth && !agreement.value) {
        next({ name: "TermsOfUseView" });
    } else if (
        agreement.value &&
        (!store.state.user.google_token || !store.state.user.apple_token)
    ) {
        next({ name: "AuthorizationView" });
    } else if (store.state.user.token && to.meta.isGuest) {
        next({ name: "HomeView" });
    } else {
        next();
    }
});

When a user clicks "I agree" on the TermsOfUse page, it triggers a change in the value of agreement. However, this results in an error:

Detected a possibly infinite redirection in a navigation guard when going from "/terms-of-use" to "/auth". Aborting to avoid a Stack Overflow.

What would be the most effective way to configure this dynamic routing based on changing values?

Answer №1

It appears that your path is stuck in a never-ending cycle. Try using

(!store.state.user.google_token && !store.state.user.apple_token)
instead of
(!store.state.user.google_token || !store.state.user.apple_token)
.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Exploring time differences in Javascript

I am trying to save a JSON AJAX response from the server in the browser's localStorage for a duration of one minute, along with a timestamp generated using new Date().getMinutes(). Upon triggering $(document).ready, I aim to check the stored timestam ...

Using space as a separator for thousands when formatting integers

In an attempt to change the appearance of 1000 to resemble 10 000, I found numerous examples online on how to add a separator such as a comma or some StringLocal. However, I am looking for a way to use a space instead. Can anyone advise me on which locale ...

The error message "res.jwt is not a function" is commonly encountered when using Node

I kept receiving the error message: res.jwt is not a function I have installed jwt-express and imported it like this: import jwt from 'jwt-express' This is my auth.js file: import Account from '../services/account.js' import env from ...

Discover the method to calculate the combined file size of multiple uploads with jquery

One of the challenges I'm facing is ensuring that users can only upload multiple files if the total size does not exceed 3GB. Is there a way I can enforce this limit? Below is the current code snippet I am using: var fileCount = 0; var showFileCo ...

New and personalized bindings in knockout.js for dynamically updating a dropdown menu based on the selection in another dropdown menu

I have been using knockout for a few months now and have been getting along just fine. However, I recently encountered an issue where I cannot update the options within a SELECT tag because the ajax methods that retrieve data from the server are inside a ...

Shift the sleek navigation arrows to the interior of the slides

Is there a way to relocate the navigation arrows on the slider images? I have tried various methods found online with no success. Currently using ASP.NET, but doubt it matters. Employing the latest version of SLICK. Here is the HTML code snippet: <%@ ...

When the button is clicked, bind the value to an object using the specified key in

I'm fairly new to Vue and I'm looking to generate buttons based on my data and show their information when clicked. The 'pets' object contains keys for id and info. (My actual data is more extensive and my code is a bit more complex, bu ...

Troubles arising from React component integration with Gatsby JS

As I dive into creating a stunning portfolio website with Gatsby js, I encountered an issue while trying to integrate the card component from the gatsby-plugin-material-ui package. The card resides in a separate file within my components directory and is e ...

Locate the class ID and refine the search results by another class

I am currently working on a task that involves extracting the first <li> element's id where it has the class name my_class, and checking if the <span> with the class Time contains a ":" using jquery. Below is the sample HTML structure: & ...

Creating template variable based on $state in AngularJS

Here is what I currently have: <span>{{ $root.page_header || "default" }}</span> However, I want it to default to default unless the current $state is a specific value. For example, if my states are: home, settings, & profile, then I wan ...

Create a shape using a series of points without allowing any overlap between the lines

JS fiddle There is an array of coordinates that gets populated by mouse clicks on a canvas. var pointsArray = []; Each mouse click pushes x and y values into this array using a click event. pointsArray.push({x: xVal, y: yVal}); The script iterates thr ...

Although everything appears to be running smoothly in Express, my request is returning null

I am facing an issue with a router in my code. In the main index.ts file, I have the following line: app.use("/api/tshirts", tshirts) And in tshirts.ts file, I have defined the following routes: router.get("/", tshirtsController.getTShirts) router.get("/ ...

Passing PHP data to a JavaScript file using JSON格式

I have a query regarding how to pass php variables and send their values to a js file using json. Here is what I currently have: <?php $data = array('artist' => $artistname, 'title' => $songname); echo json_encode($data); // ...

Creating variables in Typescript

I'm puzzled by the variable declaration within an Angular component and I'd like to understand why we declare it in the following way: export class AppComponent { serverElements = []; newServerName = ''; newServerContent = &apos ...

Ways to exit the current browser window?

Is there a way to close the current browser window using JavaScript or another language that supports this functionality? I've tried using the window.close() function, but it seems to only work for windows opened with window.open(). Thank you in adv ...

Simultaneous Vue components

Here's an example of a string: "Greetings Earthlings! I come in peace." Along with a set of words. ["Greetings", "Earthlings", "peace"] I want to automate the process of enclosing matching words within Vue components. For instance, it should look ...

Discord bot that combines the power of discord.js and node.js to enhance your music

I am currently working on a Discord bot designed to play music in voice chat rooms. However, I am facing some issues with properties. Whenever I try to launch the bot using "node main", I encounter the following error message: "TypeError: Cannot read prope ...

What is the reason why calling setState does not update the local state?

Hello everyone, I came across an intriguing React task and I'm struggling a bit with finding the solution. Task: Can you figure out why this code isn't working and fix it? Code: class BugFixer extends React.Component { constructor(props) { ...

Making modifications to the state within a modal dialogue box

I am developing a note-taking application where users can write a title and note, and when they click submit, the note is displayed on the page. I want to implement an editing feature where clicking on the edit button opens a modal with the user's tit ...

Is it possible to combine jQuery Mobile with Vue.js in a project?

Exploring new web technologies can be quite challenging, especially when it comes to making them work on mobile devices. I found AngularJS particularly tricky in this regard. I'm considering trying Vue.js along with some jQuery mobile elements instead ...