Необходимо дважды нажать кнопку входа в систему; Использование vue -router и аутентификации firebase - PullRequest
1 голос
/ 12 июля 2020

Я настроил защиту маршрутизатора, поэтому, когда я вхожу в систему вместо того, чтобы мой маршрутизатор нажимал его на приборную панель в долю секунды, когда для аутентификации требуется firebase, он думает, что я не вошел в систему, и мне нужно подождать и снова нажать кнопку входа в систему.

Есть ли способ подождать, пока он войдет в систему, тогда маршрутизатор подтолкнет меня к панели управления.

Я новичок в этом, любая помощь будет оценена.

//routes 

export const routes = [
  {
    path: "/adduser",
    component: AddUser,
    meta: {
      requiresAuth: true
    }
  },
  {
    name: "details",
    path: "/details/:id",
    component: User,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: "/register",
    component: Register,
    meta: {
      requiresGuest: true
    }
  },
  {
    path: "/login",
    component: Login,
    meta: {
      requiresGuest: true
    }
  },
  {
    path: "/dashboard",
    component: Dashboard,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: "/",
    component: Dashboard,
    meta: {
      requiresAuth: true
    }
  },
  {
    name: "editUser",
    path: "edituser/:id",
    component: EditUser,
    meta: {
      requiresAuth: true
    }
  }
];

//the login function 

 emailLogin(email, password) {
      firebase
        .auth()
        .signInWithEmailAndPassword(email, password)
        .then(this.$router.push("/dashboard"))
        .then(() => {
          this.$store.dispatch("auth/login");
        });
    }

//the router guard

router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (!firebase.auth().currentUser) {
      next({
        path: "/login",
        query: {
          redirect: to.fullPath
        }
      });
    } else {
      next();
    }
  } else if (to.matched.some(record => record.meta.requiresGuest)) {
    if (firebase.auth().currentUser) {
      next({
        path: "/"
      });
    } else {
      next();
    }
  } else {
    next();
  }
});

1 Ответ

1 голос
/ 12 июля 2020

внутри then(this.$router.push("/dashboard")) pu sh дает обещание, которое должно быть возвращено стрелочной функции.

Таким образом, новая функция входа будет выглядеть так:

emailLogin(email, password) {
      firebase
        .auth()
        .signInWithEmailAndPassword(email, password)
        .then(() => {
          this.$router.push("/dashboard");
        })
        .then(() => {
          this.$store.dispatch("auth/login");
        });
    }
...