Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

136
Vistas
How to check multiple conditions using Vue Router global navigation guard

In a vue 3 application which is using pinia, I want to achieve the following

  1. redirect a user to the sign in page whenever a user is not authenticated
  2. redirect a user to a verification page if the user authenticated but not verified
  3. redirect a user to dashboard if the user is authenticated & verified

At the moment I have been able to redirect unauthenticated users to the sign in page and redirect them to the dashboard when authenticated by writing my router index.js file like this

import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: "/signin",
            name: "signin",
            component: () => import("../views/SignIn.vue"),
        },
        {
            path: "/verify",
            name: "verify",
            component: () => import("../views/Verification.vue"),
            meta: { needsAuth: true }
        },
        {
            path: "/dashboard",
            name: "dashboard",
            component: () => import("../views/UserDashboard.vue"),
            meta: { needsAuth: true }
        }
    ]
})

router.beforeEach(async (to, from, next) => {
    if (to.meta.needsAuth && localStorage.getItem('accessToken') == null) next('signin')
    else next()
})

export default router

and here is the method that handles signin

const loginUser = async () => {
  try {
    const res = await axios.post(
      "https://some-api-url/login",
      signin.value,
      {
        headers: {
          Accept: "application/json",
          "Content-Type": "application/json",
        },
      }
    );

    localStorage.setItem("accessToken", res.data.data.accessToken);

    // redirect home
    router.push("/dashboard");
  } catch (error) {
    error = error.response.data.message;
    alert(error);
  }
};

Now my challenge is the signin endpoint I am calling only return an access token but the dashboard endpoint return the verified status. How can I achieve redirect unverified users to the verification page?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda