Skip to content

Commit

Permalink
fix: redirect from login page if authenticated
Browse files Browse the repository at this point in the history
Signed-off-by: Pedro Lamas <[email protected]>
  • Loading branch information
pedrolamas committed Sep 24, 2024
1 parent 74890da commit cc9eb0c
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/router/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Vue from 'vue'
import VueRouter, { type NavigationGuardNext, type Route, type RouteConfig } from 'vue-router'
import VueRouter, { type RouteConfig } from 'vue-router'

// Views
import Dashboard from '@/views/Dashboard.vue'
Expand All @@ -22,19 +22,19 @@ import Icons from '@/views/Icons.vue'

Vue.use(VueRouter)

const ifAuthenticated = (to: Route, from: Route, next: NavigationGuardNext<Vue>) => {
if (
router.app.$store.getters['auth/getAuthenticated'] ||
!router.app.$store.state.socket.apiConnected
) {
next()
} else {
next('/login')
}
}
const isAuthenticated = () => (
router.app.$store.getters['auth/getAuthenticated'] ||
!router.app.$store.state.socket.apiConnected
)

const defaultRouteConfig: Partial<RouteConfig> = {
beforeEnter: ifAuthenticated,
beforeEnter: (to, from, next) => {
if (isAuthenticated()) {
next()
} else {
next('/login')
}
},
meta: {
fileDropRoot: 'gcodes'
}
Expand Down Expand Up @@ -140,6 +140,13 @@ const routes: Array<RouteConfig> = [
path: '/login',
name: 'Login',
component: Login,
beforeEnter: (to, from, next) => {
if (isAuthenticated()) {
next('/')
} else {
next()
}
},
meta: {
fillHeight: true
}
Expand Down

0 comments on commit cc9eb0c

Please sign in to comment.