From 03b0bece2c6fc6f6e38eba226e18848e94abd620 Mon Sep 17 00:00:00 2001 From: Ovidiu U Date: Sat, 11 Apr 2026 17:08:19 +0100 Subject: [PATCH] feat: add auth guards and server-side logout with postcode search integration - Add navigation guard requiring authentication for dashboard routes - Create --- resources/js/router/index.js | 26 ++++++++++++- resources/js/views/Home.vue | 38 ++++++++----------- .../js/views/dashboard/DashboardLayout.vue | 8 ++-- routes/web.php | 11 ++++++ 4 files changed, 55 insertions(+), 28 deletions(-) diff --git a/resources/js/router/index.js b/resources/js/router/index.js index 3059e6a..be5c523 100644 --- a/resources/js/router/index.js +++ b/resources/js/router/index.js @@ -8,12 +8,23 @@ import SettingsLayout from '../views/dashboard/settings/SettingsLayout.vue' import Profile from '../views/dashboard/settings/Profile.vue' import Security from '../views/dashboard/settings/Security.vue' import Appearance from '../views/dashboard/settings/Appearance.vue' +import { useAuth } from '../composables/useAuth.js' const routes = [ { path: '/', component: Home, name: 'home' }, + { + path: '/logout', + name: 'logout', + component: { render: () => null }, + beforeEnter: () => { + window.location.href = '/logout' + return false + }, + }, { path: '/dashboard', component: DashboardLayout, + meta: { requiresAuth: true }, children: [ { path: '', component: Overview, name: 'dashboard' }, { path: 'saved-stations', component: SavedStations, name: 'dashboard.saved-stations' }, @@ -32,7 +43,20 @@ const routes = [ }, ] -export default createRouter({ +const router = createRouter({ history: createWebHistory(), routes, }) + +router.beforeEach(async (to) => { + if (to.meta.requiresAuth) { + const { isAuthenticated, fetchUser } = useAuth() + await fetchUser() + if (!isAuthenticated.value) { + window.location.href = '/login' + return false + } + } +}) + +export default router diff --git a/resources/js/views/Home.vue b/resources/js/views/Home.vue index 5a09ab2..85c1ea5 100644 --- a/resources/js/views/Home.vue +++ b/resources/js/views/Home.vue @@ -30,7 +30,7 @@ -
+
@@ -44,19 +44,7 @@ Join 50,000+ UK drivers using real-time insights to find the cheapest petrol and time their fill-ups perfectly.

-
-
- - -
- -
+
@@ -69,7 +57,7 @@
-
+
-
+

Smart Savings in 3 Steps

@@ -140,11 +128,11 @@
-
+
-
+

Real-Time Prices

@@ -195,7 +183,7 @@
-
+

Pricing for every driver

@@ -276,7 +264,7 @@
-
+
@@ -317,7 +305,7 @@
-
+

Ready to outsmart the pumps?

Sign up for free today and never pay over the odds for fuel again.

@@ -388,8 +376,14 @@ diff --git a/resources/js/views/dashboard/DashboardLayout.vue b/resources/js/views/dashboard/DashboardLayout.vue index e6fcb1c..59616de 100644 --- a/resources/js/views/dashboard/DashboardLayout.vue +++ b/resources/js/views/dashboard/DashboardLayout.vue @@ -96,7 +96,7 @@ import { ref, computed, onMounted, onUnmounted } from 'vue' import { RouterLink, RouterView, useRoute, useRouter } from 'vue-router' import { useAuth } from '../../composables/useAuth.js' -const { user, logout } = useAuth() +const { user } = useAuth() const route = useRoute() const router = useRouter() @@ -129,10 +129,8 @@ const userInitials = computed(() => { .toUpperCase() }) -async function handleLogout() { - dropdownOpen.value = false - await logout() - router.push('/') +function handleLogout() { + window.location.href = '/logout' } function isActive(to) { diff --git a/routes/web.php b/routes/web.php index 0ba2f7e..6e0121e 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,9 +1,20 @@ view('app'))->middleware(['auth', 'verified'])->name('dashboard'); +// Server-side logout — handles hard navigation to /logout +Route::get('/logout', function (Request $request) { + Auth::logout(); + $request->session()->invalidate(); + $request->session()->regenerateToken(); + + return redirect('/'); +})->middleware('auth')->name('logout'); + // SPA catch-all — must be last Route::get('/{any?}', fn () => view('app'))->where('any', '.*')->name('home');