Navigating to dashboard upon successful validation in VueJS is a crucial step that ensures user

How can user input validation be implemented before redirecting to the dashboard? The code provided below:

The code below is currently loading the dashboard first, but it should display the login page first. After validation, it should redirect to the dashboard page.

Please refer to the following link to download the project. I am using this template for my project, but it does not include login validation:

// app.vue

      <template>
        <div>
          <notifications></notifications>
          <router-view :key="$route.fullPath"></router-view>
        </div>
      </template>

      <script>
        export default {
          methods: {
            disableRTL() {
              if (!this.$rtl.isRTL) {
                this.$rtl.disableRTL();
              }
            },
            toggleNavOpen() {
              let root = document.getElementsByTagName('html')[0];
              root.classList.toggle('nav-open');
            }
          },
          mounted() {
            this.$watch('$route', this.disableRTL, { immediate: true });
            this.$watch('$sidebar.showSidebar', this.toggleNavOpen)

        }
        };
      </script>

      <style lang="scss"></style>

//route.js

      import DashboardLayout from 
           "@/layout/dashboard/DashboardLayout.vue";
      import LoginComponent from "@/layout/dashboard/login.vue"
      import NotFound from "@/pages/NotFoundPage.vue";

      // Admin pages
      const Dashboard = () => import(/* webpackChunkName: "dashboard" 
        */"@/pages/Dashboard.vue");
      const pole = () => import(/* webpackChunkName: "common" */ 
      "@/pages/Poles.vue");


      const routes = [
        {
      path: "/",
          component: DashboardLayout,
          redirect: "/dashboard",
          children: [
            {
              path: "dashboard",
              name: "dashboard",
              component: Dashboard
            },
            {
              path: "Poles",
              name: "Poles",
              component: pole
            } 
          ]
        },
        { path: "*", component: NotFound },
      ];
     export default routes;

//login.vue

       <template>
         <div class="login-wrapper border border-light">
           <div  class="form-signin">
        <h2  >Please sign in</h2>

        <input   type="text" name="username" v-model="input.username" 
              placeholder="Username"  class="form-control"   required 
                  autofocus>

        <input  type="password" name="password" v-model="input.password" 
             placeholder="Password"  class="form-control"   required>
        <button class="btn btn-lg btn-primary btn-block"v- 
                  on:click="login()">Login</button>
      </div>
    </div>
  </template>

<script>
    export default {
        name: 'login',
        data() {
            return {
                input: {
                    username: "",
                    password: ""
                }
            }
        },
        methods: {
            login() {
                if(this.input.username != "" && this.input.password != 
                         "") {
                    if(this.input.username == "admin"  && 
                    this.input.password =="admin") {

                      this.$router.push('DashboardLayout');  
                    } else {
                        alert("The username and / or password is 
                    incorrect");
                    }
                } else {
                    console.log("A username and password must be 
                 present");
                }
            }
        }
    };
    </script>

Answer №1

When using router.push, be sure to provide a path like this:

this.$router.push('dashboard');

and you should see the desired result.

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

Is it possible to trigger an event when the route changes before any DOM modifications occur, and outside of the

A few days back, I posted a question similar to this one and was advised to utilize beforeRouteLeave within the route component definition. However, as I am working on a Vue component, I do not have control over how developers choose to define their route ...

Having trouble updating Vuejs array with new values

Currently, I am facing an issue with the template code for my survey builder. I am successfully receiving responses from the server, but the addAnotherQuestion value is not updating as expected. Despite trying various approaches, I have been unable to reso ...

Executing multiple MSSQL queries in a Node.js environment

Currently, I am faced with the challenge of running multiple SQL queries. The issue lies in variables going out of scope due to the asynchronous nature of node.js. I am attempting to find a solution similar to the await keyword available in C#. To clarif ...

dynamically import a variety of components in vue.js and nuxt depending on certain conditions

Is there a way to implement dynamic imports for all 3 components in this scenario? Each component has different props, so using the switch option in the computed method is not feasible. I have come across solutions for using a single component dynamically ...

Tips for dynamically updating the id value while iterating through a class

Here's the HTML snippet I am working with: <div class="info"> <div class="form-group"> <label class="control-label col-sm-2">Title</label> <div class="col-xs-10 col-sm-8"> <inpu ...

What could be causing the issue with my app not showing the API response correctly?

My API is finally retrieving the correct information from my SQLite database (at least I think it is). Here's the response I am getting when I simply use {{ vendor }}: [ { "id": 1, "vendor_name": "pip" }, { "id" ...

Tips for modifying the language of an Angular Application's OneTrust Cookie Banner

I'm currently developing an Angular application and utilizing OneTrust for managing cookie consent. The issue I'm encountering is that while the rest of the components on the login page are properly translated into the target language, the OneTru ...

Unable to execute PHP functions within an Ajax-powered application

After implementing an if statement in my PHP script, my previously functional Ajax application seems to have encountered a mysterious issue. It appears that any attempt to manipulate data in PHP results in nothing being returned back to the Javascript laye ...

The error message "Jest Nuxt Cannot read property 'child' of undefined" indicates a problem where a property

Having trouble running Jest tests for Nuxt/Vue components. I've connected everything needed for testing, fixed issues with jsdom and babel, but can't figure out the Vue component problem. The error message is: TypeError: Cannot read property ...

Display a hidden div on hover using JQUERY

How can I make a hover popup appear when submitting a form, and have it disappear only when the mouse is out of both the popup div and the submit button? Currently, the hover popup shows up but disappears when entering the popup. Can someone assist me in r ...

Within a single component, there are various types present when looping through an array containing objects

Hey there, I'm looking to iterate through an array containing objects with different types within a single component. operations = [ { "id": 15525205, "type": "mise_en_prep", & ...

How can you make a drop-down menu in React/Bootstrap appear above all other elements?

My current goal is to implement a Bootstrap dropdown menu that appears when a button is clicked. Within my component structure, the code looks like this: <> <div className='1'> <navbar/> <div> < ...

Substitute the comma with a space

Here is my input code snippet: (((text(text here))) AND (test3) Near (test4) NOT (test5) NOT (Test6)),((tttt,tttt)),((and,lol)),((hbhbhbhbhbh)) This is the output I get: (((text(text here))) AND (test3) Near (test4) NOT (test5) NOT (Test6) (tttt,tttt) (an ...

Sending an array of objects over socket io: A step-by-step guide

Recently, I've been struggling with an issue when trying to send an array of objects through socket io. This is my server-side code: var addEntity = function(ent) { entityBag.push(ent); }; var entityBag = []; addEntity(new Circle({ ...

Updating the title with respect to the current route in AngularJS 1

Is it possible to dynamically change the title displayed in a header based on the current route provided by the router, even when outside of the controller scope? For example, I have a mainMenu controller that is loaded when a specific route is called. The ...

NPM Package Encountering Module Parsing Issue

I encountered a strange error while building my project with Webpack. The error is related to the Got package that I am trying to import from its `package.json` file. Module parse failed: .../node_modules/got/package.json Unexpected token (2:8) You may ne ...

Having trouble retrieving the Node.js file via a POST request

After coming across similar questions without a solution, I am reaching out for help here. I have generated XML content programmatically and displayed it in a Textarea. Now, I need to provide an option for users to export or download this content to their ...

Interacting with ref objects and functions in Vue with Leaflet

I have implemented a leaflet map in my vue project. Here is the relevant Vue code: <div style="height:70vh; width:100%; position: relative;"> <l-map ref="map" :zoom="maps_obj.zoom" :center="[maps_obj.latitu ...

Implement a click event for the X-Axis label in Angular 2 Highcharts

I'm currently facing a challenge with hand-rolling a solution that involves adding a click listener to an X-Axis label in a column chart using the HighCharts API within an Angular 2+ application. Here is what I have gathered so far: I am utilizing ...

JavaScript/Typescript is throwing an error because it is unable to access the property 'username' of an undefined object

In my project, I am attempting to compile a list of Profile objects and then extract specific elements from each object in the list. To accomplish this, I have defined a public interface named Profile, imported it into my component, and instantiated a new ...