Redirect to requested page after login using vue-router

This can be achieved by adding the redirect path in the route as a query parameter.

Then when you login, you have to check if the redirect parameter is set:

  • if IS set redirect to the path found in param
  • if is NOT set you can fallback on root.

Put an action to your link for example:

onLinkClicked() {
    if(!isAuthenticated) {
        // If not authenticated, add a path where to redirect after login.
        this.$router.push({ name: 'login', query: { redirect: '/path' } });
    }
}

The login submit action:

submitForm() {
    AuthService.login(this.credentials)
        .then(() => this.$router.push(this.$route.query.redirect || "https://stackoverflow.com/"))
        .catch(error => { /*handle errors*/ })
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)