Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: redirect from login page if authenticated #1498

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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