Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

Auth Test: Is User Logged In?

Premium
5 min read

Now, let's see how we can test the users and the access of logged-in users.


First, we must add a Middleware auth to the route.

routes/web.php:

Route::get('/', function () {
return view('home');
})->name('home');
 
Route::resource('products', ProductController::class)->middleware('auth');

After running the tests, we have three failed Feature tests. The two passed tests are Unit tests.

auth tests failed


Fixing Tests: Acting as a User

We need to create a user using Factories for each test function and perform the server request using that user.

For that, there is a method called actingAs(), which...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (29 h 14 min)

You also get:

54 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

SP
Steve Purcell ✓ Link copied!

Where do I change the login redirect in Laravel 11? I don't have an app/Http/Controllers/Auth/AuthenticatedSessionController.php