O
O
Obolduy2021-02-15 22:13:19
Laravel
Obolduy, 2021-02-15 22:13:19

How to fix mediator errors?

Hello, I ran into the following problem: I am writing authorization on Laravel, I did everything I wanted, now I want to write an intermediary that would check if the user is authorized (The site implies mandatory registration). I do everything according to the official guide, but I get an error:

Call to a member function send() on string
I tried both the pre-installed intermediary and the self-made one. Here is the original code:
<?php

namespace App\Http\Middleware;

use Illuminate\Auth\Middleware\Authenticate as Middleware;

class Authenticate extends Middleware
{
    /**
     * Get the path the user should be redirected to when they are not authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return string|null
     */
    protected function redirectTo($request)
    {
        if (! $request->expectsJson()) {
            return route('login');
        }
    }
}

Own:
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class AuthCheckMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        if (!Auth::user()) {

            return route('login');

        }

        return $next($request);
    }
}

Route:
Route::match(['get', 'post'], '/login', [LoginController::class, 'login'])->name('login');

What to do, I have no idea. At the same time, initially there was an eternal recursion in the Auth intermediary. I would be very grateful for your help!
upd: Everything, the question is removed, I figured it out. The thing was that I made an intermediary that throws "login" on the route, common to all routes, including for the Login itself, that is, it turned out to be eternal recursion. Everything was decided by grouping routes and attaching an intermediary to them:
Route::middleware(['auth'])->group(function () { 
// Тут все роуты, кроме тех, которые отвечают за аутентификацию 
});

Suddenly someone will help, because the answer was not found

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Obolduy, 2021-02-16
@Obolduy

public function handle(Request $request, Closure $next)
    {
        $value = session('auth');

        if (empty($value)) {

            return redirect()->route('login');

        }

        return $next($request);
    }

Decided to play around a bit. Now everything works, according to the console. Only now the redirect loops and the browser knocks out an error: "Firefox determined that the server is redirecting the request to this address in such a way that it will never complete."
Yes, when registering / logging into the session, it is in the 'auth' field that one should be entered.
Yes, and I tried it too. Uselessly. There is also nothing on the internet return redirect('login');return route('login');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question