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

Vuejs tutorial add example for route guard #10052

Open
jpmeijers opened this issue Apr 19, 2022 · 0 comments
Open

Vuejs tutorial add example for route guard #10052

jpmeijers opened this issue Apr 19, 2022 · 0 comments

Comments

@jpmeijers
Copy link

jpmeijers commented Apr 19, 2022

Following the Vue Router docs on adding a route guard to prevent un-authenticated users from viewing any routes and get redirected to /login (https://router.vuejs.org/guide/advanced/navigation-guards.html#global-before-guards).

I tried the following index.js for the router component:

import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import {useAuth0} from "@auth0/auth0-vue";

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView
  },
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "about" */ '../views/LoginView.vue')
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach(async (to) => {
  const isAuthenticated = useAuth0().isAuthenticated;
  if (!isAuthenticated && to.name !== 'login') {
    return { name: 'login' }
  }
})

export default router

The problem is useAuth0() is undefined, also when I do const { isAuthenticated } = useAuth0();.

Please add an example how to correctly do this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant