|
| 1 | +import VueRouter from 'vue-router'; |
| 2 | +import store from './store/index'; |
| 3 | +import compareDate from 'date-fns/compare_asc' |
| 4 | +import addHours from 'date-fns/add_hours' |
| 5 | +import dateParse from 'date-fns/parse' |
| 6 | +const route = require('./../../../vendor/tightenco/ziggy/src/js/route').default; |
| 7 | + |
| 8 | +// Base Vuejs Templates |
| 9 | +import Login from './components/auth/Login'; |
| 10 | +import Dashboard from './components/dashboard/Dashboard'; |
| 11 | +import Account from './components/dashboard/Account'; |
| 12 | +import ResetPassword from './components/auth/ResetPassword'; |
| 13 | + |
| 14 | +const routes = [ |
| 15 | + { name: 'login', path: '/auth/login', component: Login }, |
| 16 | + { name: 'forgot-password', path: '/auth/password', component: Login }, |
| 17 | + { name: 'checkpoint', path: '/auth/checkpoint', component: Login }, |
| 18 | + { |
| 19 | + name: 'reset-password', |
| 20 | + path: '/auth/password/reset/:token', |
| 21 | + component: ResetPassword, |
| 22 | + props: function (route) { |
| 23 | + return { token: route.params.token, email: route.query.email || '' }; |
| 24 | + } |
| 25 | + }, |
| 26 | + |
| 27 | + { name : 'dashboard', path: '/', component: Dashboard }, |
| 28 | + { name : 'account', path: '/account', component: Account }, |
| 29 | + { name : 'account.api', path: '/account/api', component: Account }, |
| 30 | + { name : 'account.security', path: '/account/security', component: Account }, |
| 31 | + |
| 32 | + { |
| 33 | + name: 'server', |
| 34 | + path: '/server/:id', |
| 35 | + // component: Server, |
| 36 | + // children: [ |
| 37 | + // { path: 'files', component: ServerFileManager } |
| 38 | + // ], |
| 39 | + } |
| 40 | +]; |
| 41 | + |
| 42 | +const router = new VueRouter({ |
| 43 | + mode: 'history', routes |
| 44 | +}); |
| 45 | + |
| 46 | +// Redirect the user to the login page if they try to access a protected route and |
| 47 | +// have no JWT or the JWT is expired and wouldn't be accepted by the Panel. |
| 48 | +router.beforeEach((to, from, next) => { |
| 49 | + if (to.path === route('auth.logout')) { |
| 50 | + return window.location = route('auth.logout'); |
| 51 | + } |
| 52 | + |
| 53 | + const user = store.getters['auth/getUser']; |
| 54 | + |
| 55 | + // If user is trying to access the authentication endpoints but is already authenticated |
| 56 | + // don't try to load them, just send the user to the dashboard. |
| 57 | + if (to.path.startsWith('/auth')) { |
| 58 | + if (user !== null && compareDate(addHours(dateParse(user.getJWT().iat * 1000), 12), new Date()) >= 0) { |
| 59 | + return window.location = '/'; |
| 60 | + } |
| 61 | + |
| 62 | + return next(); |
| 63 | + } |
| 64 | + |
| 65 | + // If user is trying to access any of the non-authentication endpoints ensure that they have |
| 66 | + // a valid, non-expired JWT. |
| 67 | + if (!to.path.startsWith('/auth')) { |
| 68 | + // Check if the JWT has expired. Don't use the exp field, but rather that issued at time |
| 69 | + // so that we can adjust how long we want to wait for expiration on both server-side and |
| 70 | + // client side without having to wait for older tokens to pass their expiration time if |
| 71 | + // we lower it. |
| 72 | + if (user === null || compareDate(addHours(dateParse(user.getJWT().iat * 1000), 12), new Date()) < 0) { |
| 73 | + return window.location = route('auth.login'); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + // Continue on through the pipeline. |
| 78 | + return next(); |
| 79 | +}); |
| 80 | + |
| 81 | +export default router; |
0 commit comments