Javascript Programming Tutorials, Guides & Best Practices
Explore 93+ expertly crafted javascript tutorials, components, and code examples. Stay productive and build faster with proven implementation strategies and design patterns from DeveloperBreeze.
Adblocker Detected
It looks like you're using an adblocker. Our website relies on ads to keep running. Please consider disabling your adblocker to support us and access the content.
Tutorial
javascript php
Building a Custom E-commerce Platform with Laravel and Vue.js
import { createRouter, createWebHistory } from 'vue-router';
import Home from './views/Home.vue';
import Product from './views/Product.vue';
import Login from './views/Login.vue';
import Register from './views/Register.vue';
import Cart from './components/Cart.vue';
const routes = [
{ path: '/', name: 'Home', component: Home },
{ path: '/product/:id', name: 'Product', component: Product },
{ path: '/login', name: 'Login', component: Login },
{ path: '/register', name: 'Register', component: Register },
{ path: '/cart', name: 'Cart', component: Cart, meta: { requiresAuth: true } },
];
const router = createRouter({
history: createWebHistory(),
routes,
});
router.beforeEach((to, from, next) => {
const requiresAuth = to.matched.some(record => record.meta.requiresAuth);
const isAuthenticated = !!localStorage.getItem('auth_token'); // Assuming you're storing the auth token in localStorage
if (requiresAuth && !isAuthenticated) {
next('/login');
} else {
next();
}
});
export default router;This ensures that certain routes, like the cart page, are only accessible to authenticated users.
Aug 27, 2024
Read More