P粉9044509592023-09-02 00:28:37
Taken from: https://blog.logrocket.com/ultimate-guide-authentication-vue-js-supabase/
// router/index.js import { createRouter, createWebHistory } from 'vue-router'; function loadPage(view) { return () => import( /* webpackChunkName: "view-[request]" */ `@/views/${view}.vue` ); } const routes = [ { path: '/', name: 'Dashboard', component: loadPage("Dashboard"), meta: { requiresAuth: true, } }, { path: '/sign-up', name: 'SignUp', component: loadPage("SignUp") }, { path: '/sign-in', name: 'SignIn', component: loadPage("SignIn") }, ] const router = createRouter({ history: createWebHistory(process.env.BASE_URL), routes }) router.beforeEach((to, from, next) => { // get current user info const currentUser = supabase.auth.user(); const requiresAuth = to.matched.some (record => record.meta.requiresAuth); if(requiresAuth && !currentUser) next('sign-in'); else if(!requiresAuth && currentUser) next("/"); else next(); }) export default router