Support the ongoing development of Laravel.io →
Authentication Laravel

Hello, as I can see in the docs, sanctum can be used to protect web routes because he first attempt to autenticate by session cookie. https://laravel.com/docs/8.x/sanctum#protecting-routes

At now, the api routes works well but the web returns always a redirect 302 to the login route.

I need to protect a route that return an image from the storage, I have tried like this in my routes/web.php:

        Route::get('images/exercise/{imageId}', function (Request $request) {
            return Storage::get('demo/450.png');
        })->middleware('auth:sanctum')->where(['imageId' => '[1-9][0-9]*']);

Where I'm wrong? Thank you

Last updated 3 years ago.
0

Essentially I was authenticating without using the Auth facade.. but the session cookie was also created. 🤷‍♂️

Last updated 4 years ago.
0

Sign in to participate in this thread!

PHPverse

Your banner here too?

AleeeKoi aleeekoi Joined 26 Jan 2021

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.