How can I define a default value for a dynamic route or page in Nuxt.js?

Whenever a user accesses my site through a referral link, I aim for the URL to appear as follows:

localhost:3000/<dynamic affiliate username>/home

However, in cases where someone visits my site directly as a guest, I would prefer the URL to default to:

localhost:3000/www/home

Does Nuxt.js offer a way for me to establish a default value for a dynamic route or page?

Answer №1

Utilize middleware

1. Page middleware

You can verify the user in middleware (using cookies or any other method you prefer. In this instance, I utilized the nuxt auth module)

Named middleware
/* pages/_user/home.vue */
export default {
  middleware: "auth"
}
Anonymous
/* pages/_user/home.vue */
export default {
  middleware({ $auth, redirect }) {
    if(!$auth.loggedIn) {
      redirect("/www/home")
    }
  }
}

2. Router middleware

If you wish to apply this logic globally across the project (on every child route of the _user page, for example), you can specify router middleware in nuxt.config.js

/* nuxt.config.js */
export default {
  router: {
    middleware: ["auth"] // or "auth" for a single middleware 
  }
}
/* middleware/auth.js */
export default ({ $auth, route, redirect }) => {
  if(funcToDetectUserPage(route.path)) {
    if(!$auth.loggedIn) {
      redirect("/www/home");
    }
  }
}

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

Creating an AJAX request in Play 2.x by using a shared button

Although I have successfully utilized AJAX requests in the past using a form and adding return false to prevent page refresh, I recently encountered an issue when moving my JavaScript into a separate file. The @ commands now fail, making it difficult for m ...

Getting the application host in Node.JS and Express without the need for a request

I am currently running a Node.JS & Express application on localhost (127.0.0.1) and I need to determine the current host (domain) without making any additional requests, as this information is required for a cron job that initiates when the server is creat ...

nodemon keeps attempting to restart the server after making changes to files, but the updates are not being reflected

I've been using the nodemon package, but I'm experiencing issues with it not restarting the server properly. Instead of showing "server running" after making changes like in tutorials, all it displays is "restarting due to changes". This also res ...

Vue 2.0 - How Children talk to their Parents

I am struggling to make the parent respond to a child's emit. Here is the code snippet I am working with: HTML: <ul> <!-- Tablet Parent --> <li :closeMySibling="closeMyChild"> <div class="inner-category"> ...

Ways to personalize php artisan ui vue --auth templates

Hello, I have a question about customizing the routes and files generated when using php artisan ui vue --auth. Is it possible to edit these files to add additional steps or customize the registration process? I noticed that the web.php file does not sho ...

When a cookie is set in NextJS with a static export, it reverts back to its original

My current project involves building a multilingual website. To handle language selection, I have implemented a system where the chosen language is stored in a cookie and retrieved using getInitialProps in the _app file, which is then passed as a context A ...

Transforming PHP Variable Using Ajax

I have a variable called $type and I need it to be either month or year. This change should occur when a div is clicked. I attempted to use an onclick event with an ajax call. The ajax call and the variable are both in the same script (index.php). Within ...

Is Protractor compatible with Internet Explorer 9?

For my Angular App that is running on IE9, I need to create end-to-end acceptance tests. I'm curious to know if the browser simulated by Protractor matches the behavior of IE9 or a newer version? ...

Using jQuery, you can easily pass an array in an AJAX request

I am working on a form that has multiple identical fields: <input type="text" id="qte" value="" name="qte[]"> How can I pass the array to my file processing? I have observed that the array sent via ajax is converted into a string. $("#form_comman ...

Issue with ChartJs version 2.8.0: The custom tooltip remains visible even when clicking outside the chart canvas or moving the mouse pointer

I am utilizing ChartJs to display a line chart with a custom tooltip that appears upon the click event of the data points. The challenge I am facing is that the custom tooltip remains visible even when the mouse pointer is outside the chart canvas area. ...

Trouble with activating dropdown toggle feature in Bootstrap 5

I recently upgraded to Bootstrap 5 and now my code is not functioning properly. <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria- controls="navbarCollaps ...

Encountered an issue while attempting to send a POST request using AngularJS $

I am facing an issue with accessing the POST method from my server. Whenever I log the response, it always returns status=0. Can anyone help me out or provide some advice? Note: I have tested the method in Postman and it works fine. Below is the code snip ...

Challenges with implementing a jQuery sortable table in Laravel

I've integrated tableclothjs and Twitter Bootstrap into my project, but the sortable headers are not appearing as expected. I've checked the JavaScript console for any errors or warnings, but there doesn't seem to be any issue indicated. Wh ...

Go to a distant web page, complete the form, and send it in

As the creator of Gearsbook.net, a social network for Gears of War players, I am constantly striving to improve the site's functionality. Currently, it is quite basic and in need of updates. One highly requested feature by users is the ability to lin ...

Error Message: "Displaying unexpected output in Vuetify Autocomplete"

I recently started using Vue 3 and I'm working on implementing the vuetify autocomplete feature in my project. After referring to the official documentation of Vuetify, I encountered an issue where the Autocomplete is displaying [object Object]. Any h ...

Implement an expand/collapse effect using CSS3 transitions

How can I implement a smooth expand/collapse effect? function expandCollapse(shID) { if (document.getElementById(shID)) { if (document.getElementById(shID + '-show').style.display != 'none') { document.getElem ...

What is the best way to encode an image into JSON format?

let canvas = document.createElement('canvas'); let context = canvas.getContext( '2d' ); context.drawImage( video, 0, 0 ); let image_src = canvas.toDataURL('image/jpeg'); let dataURL = canvas.toDataURL("image/jpeg"); let image= ...

What causes the unexpected behavior of JQuery's .animate() function?

I am currently working on a project that involves creating a div element for the purpose of dragging and dropping files. My goal is to have the height of the div increase when a file is dragged into it, and decrease when the file is dragged out. To achiev ...

What could be causing the multer middleware to fail to work properly?

Within the app.js file, there is a direct route to handle image uploads: app.post('/upload', upload.single('image'), (req, res) => { res.json({ message: 'pic uploaded' }); }); Posting to /upload successfully uploads the im ...

Unable to transfer data through Ionic popover

I've encountered an issue when trying to pass data to my popover component, as the data doesn't seem to be sent successfully. Code HTML <div class="message" id="conversation" *ngFor="let message of messages.notes"> <ion-row class= ...