Introduction
This tutorial teaches you how to build a secure SPA using Laravel 12 as backend API and Vue 3 as frontend in a single project.
Authentication is done with Laravel Sanctum using cookie-based sessions — no JWT needed on frontend.
Why Laravel Sanctum?
-
✅ No JWT handling on frontend
-
✅ No CORS issues
-
✅ Built-in CSRF protection
-
✅ Secure session-based authentication
-
✅ Production-ready setup
What You Will Build
-
User Registration & Login
-
Authenticated User Profile
-
Logout functionality
-
Protected API routes
-
Vue 3 SPA frontend
-
Laravel 12 API backend
Prerequisites
-
PHP 8.1+
-
Composer
-
Node.js & npm
-
MySQL
-
Basic knowledge of Laravel & Vue
Step 1: Create Laravel Project
Step 2: Setup Database
-
Create a database named
laravel_vue_authin MySQL. -
Edit
.envfile:
-
Run migrations:
Step 3: Install Laravel Sanctum
Step 4: Configure CORS
Edit config/cors.php:
Step 5: Create Form Request Validation
Generate requests:
Edit app/Http/Requests/RegisterRequest.php:
Edit app/Http/Requests/LoginRequest.php:
Step 6: Create API Auth Controller
Generate controller:
Edit app/Http/Controllers/API/AuthController.php:
Step 7: Create User API Resource
Generate resource:
Edit app/Http/Resources/UserResource.php:
Step 8: Define API Routes
Edit routes/api.php:
Step 9: Create Vue Project Directories & Files
Run in project root:
Step 10: Install Vue 3 and Dependencies
Step 11: Configure Vite
Create/Edit vite.config.js:
Step 12: Create Vue Project Structure
Step 13: Vue Router Setup
resources/js/router/index.js:
Step 14: Pinia Store Setup
resources/js/stores/auth.js:
Step 15: API Service Helper
resources/js/services/api.js:
Step 16: Vue Entry Point
resources/js/app.js:
Step 17: Main Vue Component
resources/js/App.vue:
Step 18: Vue Components
Login.vue
Register.vue
Dashboard.vue
Step 19: Blade View to Load Vue SPA
routes/web.php file to make your Laravel app serve the welcome view for any route that doesn’t match existing routes (usually for SPA routing fallback):
Edit or create resources/views/welcome.blade.php:
Step 20: Run Your Project
Open two terminals:
-
Start Laravel server:
-
Start frontend development server:
Step 21: Visit Your App
Open browser:
You now have a secure SPA with:
-
Laravel 12 API backend
-
Vue 3 SPA frontend
-
Laravel Sanctum session-based authentication (no manual Sanctum middleware setup needed)
Download the complete Project example from my GitHub repo here.

