Answer the question
In order to leave comments, you need to log in
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 stringI 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');
}
}
}
<?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::match(['get', 'post'], '/login', [LoginController::class, 'login'])->name('login');
Route::middleware(['auth'])->group(function () {
// Тут все роуты, кроме тех, которые отвечают за аутентификацию
});
Answer the question
In order to leave comments, you need to log in
public function handle(Request $request, Closure $next)
{
$value = session('auth');
if (empty($value)) {
return redirect()->route('login');
}
return $next($request);
}
return redirect('login');
return route('login');
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question