P
P
pp_k2022-01-13 14:51:07
Laravel
pp_k, 2022-01-13 14:51:07

How to get rid of constant authentication check?

Hello! There is a task to implement a shopping cart. If the user is not authenticated, save the product in cookies, if authenticated then in the database. The problem is that I've implemented this with a permanent check in each add, delete, show... method of type if (Auth::check) i.e. each method starts with an if/else block.
I want to get rid of it, but I can't figure out how.
If through middleware, then it will turn out the same, but in intermediaries.
I'm not asking you to write code for me, just a hint or direction in which to move.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Anton, 2022-01-13
@pp_k

Need to read documentation: https://laravel.com/docs/8.x/container#binding-int... .
Well, do not forget about the transfer from cookies to the database when the user logs in

T
Timur Maslov, 2022-01-13
@tmaslov22

Create a group for routes and add checks on them through middleware

Route::middleware('auth')->group(function () {
// тут твои роуты для работы с корзиной
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question