Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

138
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda