1

I am building a web app with two layouts (for login page, and dashboard). Each of them are represented as SPA application, so each has router-view. The main problem is 'How to connect them and redirect from one to another?'.

I have a App.vue - check if user is authorized. if yes - redirect to Dashboard.vue, else - redirect to Login.vue. Each of them has there own router-view.

2
  • Why are you separating them out? You should just use a navigation guard to check the authentication status of the user and redirect appropriately. Commented Nov 6, 2017 at 11:25
  • @craig_h my auth page consist of Base.vue (Layout), Login.vue and Restore.vue. When user is not authorized App.vue redirect to Base.vue, there we check where we are (/login or /restore) and depend on it we import component we need. Is it correct way or i should change my project structure ? My dashboard has same structure Commented Nov 6, 2017 at 11:33

2 Answers 2

2

An SPA should be a single html file which serves up your app and all the routes, so the basic structure should be:

HTML

<div id="app">

</div>

<!-- bundled file -->
<script src="app.js"></script>

app.js

import Vue from 'vue' 
import VueRouter from 'vue-router' 

Vue.use(VueRouter)

import App from './components/App.vue' // import Base component

// Import views to register with vue-router
import Login from './components/views/Login.vue'
import Dashboard from './components/views/Dashboard.vue'

const guard = function(to, from, next) {
    // Check if user is logged in (you will need to write that logic)
    if (userIsLoggedIn) {
        next();
    } else {
        router.push('/login');
    }
};

const routes = [{
      path: '/login',
        component: Login
      },{
        path: '/dashboard',
        component: Dashboard,
        beforeEnter: (to, from, next) => {
            guard(to, from, next); // Guard this route
      }
}]

const router = new VueRouter({
    mode: 'history', // history mode 
    routes
})

new Vue({
  el: '#app',
  router,
  render: h => h(App) // mount base component
})

App.vue

<template>
  <div>
    <!-- Your layout -->

    <!-- All views get served up here -->
    <router-view></router-view>
  </div>  
</template>

I haven't tested that, but in this scenario every view component gets served up by App.vue which is mounted on the main vue instance. You then use the beforeEach guard to check that the user is logged in, if they are then you call next() which takes them to the route, if they are not then you redirect them to login.

Sign up to request clarification or add additional context in comments.

7 Comments

i knew that, thx, the problem is that login page consist of two pages (login and restore) like that -> path: 'auth', component: Base, children: [ { path: 'login', component: Login }, { path: 'restore', component: Restore }] path: 'dashboard', component: Base, children: [ { path: 'schedule', component: Schedule }, { path: 'Settings', component: Settings }] and so on
It's not exactly clear why you are doing that, could you post some code to show what you are actually doing?
Oh OK, that's not another SPA that's nested routes you just use router.push to push to the relevant route, it's no different, Base will still be served up by your top level router-view
yeah, exactly. So the problem is when i am using router.push in Login.vue to /dashboard, its still use Base.vue of Auth
I think you should just be using a guard to redirect to login or restore which should be top level routes, rather than trying to nest those routes in a component that does the authentication, there's no need for that, especially if the component is not providing a layout. I tend to think as nested routes as good for things like tabs, not authenticating and redirecting users.
|
-1

vue-router has the ability to create custom guards for any route. You do not need 2 separate applications, just some safety with the routes in your router.

https://router.vuejs.org/guide/advanced/navigation-guards.html

Your guard could be a function that checks for authentication.

Here's a full implementation tutorial from Auth0: https://auth0.com/blog/vuejs2-authentication-tutorial/

1 Comment

The first link is dead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.