Laravel Sanctum SPA Tutorial - React SPA Authentication With Sanctum @ProgramWithGio
Laravel Sanctum SPA Tutorial - React SPA Authentication With Sanctum  @ProgramWithGio
Uploaded October 2020 | Updated November 2026, 3 hours ago
Authenticate your SPA with Laravel Sanctum where SPA & Laravel API are on two different repositories. In this video, you will learn how to authenticate your NextJS SPA, how to protect the dashboard from unauthorized access & how to configure cors for Laravel correctly.

🤝 You can support me by giving my videos a 👍 & by subscribing to the channel ✔️

--- Next.JS SPA Front-End Source Code ---
github.com/ggelashvili/nextjs-spa-auth-laravel-sanctum-frontend

--- Laravel Sanctum API Back-End Source Code ---
github.com/ggelashvili/nextjs-spa-auth-laravel-sanctum-backend

--- Note ---
This works for Laravel versions 7.x & up. If you are trying to do this on earlier versions then make sure to pull in the github.com/fruitcake/laravel-cors package, this is included in Laravel's core since 7.x.

Also at 3:23, my SESSION_DRIVER is set to cookie. This is not important and it is up to you what session driver you want to use. There were a lot of devs who had issues authenticating their SPAs that were on different ports locally and changing file to cookie was the solution. I had the same issue which is why I have it set to 'cookie'. So if you are having such issues you can try setting driver to the cookie & see if that helps. Note that having SESSION_DRIVER set to a cookie will expose encrypted session to the browser. In production, I recommend using Redis as the session driver which is what I use on my apps.

--- Table of Contents ---
00:00 - Intro
00:42 - Install Laravel Sanctum
02:23 - CORS/Session Configuration
04:57 - CSRF Protection
08:02 - Client-Side Login (Keeping User Logged In Using Custom Cookie)
08:50 - Protection Against User Manually Creating Cookie (Axios Interceptors)
10:42 - Server Side Redirect To Prevent Flashing Of Unauthorized Page
Laravel Sanctum SPA Tutorial - React SPA Authentication With SanctumQuick Tips to Avoid Overengineering: The Pitfalls of Premature OptimizationPHP - Object Cloning & Clone Magic Method - Full PHP 8 TutorialWhat is Laravel & Why You Should Learn It | Learn Laravel The Right WayIntro to Slim PHP Framework - Full PHP 8 TutorialDisplay Errors Using JavaScript & Bootstrap - Build Expense Tracker App With PHP 8Uploading Files to S3 Bucket - Build Expense Tracker App With PHP 8PHP PDO Tutorial Part 1 - Prepared Statements - SQL Injection - Full PHP 8 TutorialLets Build Task Tracker App With Laravel | Part 3: Dashboard, Refactoring, Code Quality & SecurityPHP Generators Explained - Full PHP 8 TutorialDisplay Validation Errors & Keep Form Filled - Build Expense Tracker App With PHP 8How to authenticate Mobile App using Laravel Sanctum & Laravel Fortify
Program With Gio |

Laravel Sanctum SPA Tutorial - React SPA Authentication With Sanctum

SHARE TO X SHARE TO REDDIT SHARE TO FACEBOOK WALLPAPER