中间件不工作(Laravel Livewire)

ne5o7dgx  于 2023-03-24  发布在  其他
关注(0)|答案(4)|浏览(140)

中间件不工作(Laravel Livewire)我试图在laravel livewire中使用自定义auth对用户进行身份验证,但它不起作用。它打开所有路由,无论用户是否经过身份验证

CustomAuth(这是我的中间件文件)

<?php
namespace App\Http\Middleware;

use Closure;
use Session;
use Illuminate\Http\Request;
class CustomAuth
{
    public function handle(Request $request, Closure $next)
    {
        $path = $request->path();
        if(($path=='login' ||$path=='register') && Session::get('user'))
        {
            return redirect()->route('/userpanel');
        }
        else if(($path!='login' && !Session::get('user')) && ($path!='register' && !Session::get('user')))
        {
            return redirect()->route('/login');
        }
        return $next($request);
    }
}

Kernal.php(中间件注册)

<?php

namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    protected $middleware = [
        // \App\Http\Middleware\TrustHosts::class,
        \App\Http\Middleware\TrustProxies::class,
        \Fruitcake\Cors\HandleCors::class,
        \App\Http\Middleware\PreventRequestsDuringMaintenance::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
    ];

    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            //  \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

        'api' => [
            // \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
            'throttle:api',
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],
    ];

    protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
        'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
        'customauth' => \App\Http\Middleware\CustomAuth::class,
    ];
}

web.php(这是我的Route文件)

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Livewire\home;
use App\Http\Livewire\Gallery;
use App\Http\Livewire\Video;
use App\Http\Livewire\Faq;
use App\Http\Livewire\Team;
use App\Http\Livewire\Price;
use App\Http\Livewire\Auth\Login;
use App\Http\Livewire\Auth\Register;
use App\Http\Livewire\Userpanel;



Route::group(['middleware'=>'customauth'],function () { 
    Route::get('/register',Register::class)->name('register');
    Route::get('/login',Login::class)->name('login');
    Route::get('/', Home::class)->name('home')->middleware('auth');    
    Route::get('/gallery',Gallery::class)->name('gallery');
    Route::get('/video',Video::class)->name('video');
    Route::get('/faq',Faq::class)->name('faq');
    Route::get('/team',Team::class)->name('team');
    Route::get('/price',Price::class)->name('price');
    Route::get('/userpanel',Userpanel::class)->name('user');
});
g6ll5ycj

g6ll5ycj1#

这是在路由页面中使用中间件的一个示例
Route::get('/nuevopoais', Nuevopoais::class)->middleware('can:Nuevopoais_listar')->name('nuevopoais') ;

vhmi4jdf

vhmi4jdf2#

Livewire有一个配置来为它的路由设置一个中间件。请检查这个。
https://laravel-livewire.com/docs/2.x/authorization

44u64gxh

44u64gxh3#

我知道了,中间件因为路由缓存不工作运行以下命令后,中间件工作正常

php artisan route:cache

感谢@Jesus Emanuel Becerra Santamar和@Mohamed Tahan帮助我!!!

lyfkaqu1

lyfkaqu14#

如果有人在使用Laravel Spatie,并且想要保护他们的Livewire方法,他们应该在他们的组件中做以下事情:
1.导入器授权请求

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

1.在组件内部使用它

class Actividades extends Component
{
   use AuthorizesRequests;
   //More code...
}

1.在你的方法中调用authorize。假设你有一个名为“activities.create”的权限

public function store(){
    $this->authorize('activities.create');
    //More code...
}

相关问题