Aiming to master full-stack development? Pairing Laravel with Vue.js paves the path, particularly for authentication tasks.
In this guide, we’ll delve into how Laravel, Vue, and Laravel Sanctum can be merged to craft an API authentication in two distinct methods:
- Merging Laravel and Vue in a Single Page Application [SPA]
- Using Vue and API as individual projects
Let’s get started.
Setting Up a Laravel Single Page Application with Breeze and Vue
First things first, create a new Laravel project. Run this command:
composer require laravel/breeze --dev
Run the following commands one after the other:
php artisan breeze:install vue php artisan migrate npm install npm run dev
After this, you’ve got yourself a fully functional Single Page Application [SPA].
Head to
php artisan make:controller DemoController
6. Here, you'll find controllers that manage authentication. A noteworthy file is
php artisan make:controller DemoController
7, specifically the
php artisan make:controller DemoController
8 method:
public function store[LoginRequest $request]: RedirectResponse {
$request->authenticate[];
$request->session[]->regenerate[];
return redirect[]->intended[RouteServiceProvider::HOME];
}Let’s craft a “protected” demo component. This component will only be visible to logged-in users and will showcase the ID and name of the user currently logged in.
First, you need to create a new controller named
php artisan make:controller DemoController
9. Use the following command:
php artisan make:controller DemoController
Open up the
php artisan make:controller DemoController
9 you've just created under