【问题标题】:Laravel 8 Class 'Laravel\Fortify\Actions\Auth' not found未找到 Laravel 8 类“Laravel\Fortify\Actions\Auth”
【发布时间】:2021-03-13 18:45:48
【问题描述】:

我正在尝试进行电子商务管理员/用户身份验证我使用 laravel 8 注册了一个测试帐户并登录并发生了此错误。

错误 找不到类“Laravel\Fortify\Actions\Auth”

在我登录一个测试帐户后,它应该是这样的结果 https://ibb.co/Vq5LxBk

C:\Users\ACER\laravel8ecommerce\vendor\laravel\fortify\src\Actions\AttemptToAuthenticate.php:58

这是第 58 行

if(Auth::user()->utype === 'ADM')

我在 AttemptToAuthenticate.php 上的代码

<?php

namespace Laravel\Fortify\Actions;


use Illuminate\Auth\Events\Failed;
use Illuminate\Contracts\Auth\StatefulGuard;
use Illuminate\Validation\ValidationException;
use Laravel\Fortify\Fortify;
use Laravel\Fortify\LoginRateLimiter;

class AttemptToAuthenticate
{
    /**
     * The guard implementation.
     *
     * @var \Illuminate\Contracts\Auth\StatefulGuard
     */
    protected $guard;

    /**
     * The login rate limiter instance.
     *
     * @var \Laravel\Fortify\LoginRateLimiter
     */
    protected $limiter;

    /**
     * Create a new controller instance.
     *
     * @param  \Illuminate\Contracts\Auth\StatefulGuard  $guard
     * @param  \Laravel\Fortify\LoginRateLimiter  $limiter
     * @return void
     */
    public function __construct(StatefulGuard $guard, LoginRateLimiter $limiter)
    {
        $this->guard = $guard;
        $this->limiter = $limiter;
    }

    /**
     * Handle the incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  callable  $next
     * @return mixed
     */
    public function handle($request, $next)
    {
        if (Fortify::$authenticateUsingCallback) {
            return $this->handleUsingCustomCallback($request, $next);
        }

        if ($this->guard->attempt(
            $request->only(Fortify::username(), 'password'),
            $request->filled('remember'))
        ) {
            if(Auth::user()->utype === 'ADM')
            {
                session(['utype'=>'ADM']);
                return redirect(RouteServiceProvider::HOME);
            }
            elseif(Auth::user()->utype === 'USR')
            {
                session(['utype'=>'USR']);
                return redirect(RouteServiceProvider::HOME);
            }
            return $next($request);
        }

        $this->throwFailedAuthenticationException($request);
    }

    /**
     * Attempt to authenticate using a custom callback.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  callable  $next
     * @return mixed
     */
    protected function handleUsingCustomCallback($request, $next)
    {
        $user = call_user_func(Fortify::$authenticateUsingCallback, $request);

        if (! $user) {
            $this->fireFailedEvent($request);

            return $this->throwFailedAuthenticationException($request);
        }

        $this->guard->login($user, $request->filled('remember'));

        return $next($request);
    }

    /**
     * Throw a failed authentication validation exception.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return void
     *
     * @throws \Illuminate\Validation\ValidationException
     */
    protected function throwFailedAuthenticationException($request)
    {
        $this->limiter->increment($request);

        throw ValidationException::withMessages([
            Fortify::username() => [trans('auth.failed')],
        ]);
    }

    /**
     * Fire the failed authentication attempt event with the given arguments.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return void
     */
    protected function fireFailedEvent($request)
    {
        event(new Failed(config('fortify.guard'), null, [
            Fortify::username() => $request->{Fortify::username()},
            'password' => $request->password,
        ]));
    }

我的路线(web php)

<?php

use App\Http\Livewire\CartComponent;
use App\Http\Livewire\CheckoutComponent;
use App\Http\Livewire\HomeComponent;
use App\Http\Livewire\ShopComponent;
use App\Http\Livewire\User\UserDashboardComponent;
use App\Http\Livewire\Admin\AdminDashboardComponent;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

// Route::get('/', function () {
//    return view('welcome');
// });

Route::get('/',HomeComponent::class);

Route::get('/shop',ShopComponent::class);

Route::get('/cart',CartComponent::class);

Route::get('/checkout',CheckoutComponent::class);
// Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () {
//    return view('dashboard');
// })->name('dashboard');



// For User or Customer
Route::middleware(['auth:sanctum', 'verified'])->group(function(){
    Route::get('user/dashboard',UserDashboardComponent::class,)->name('user.dashboard');
});

// For Admin
Route::middleware(['auth:sanctum', 'verified','authadmin'])->group(function(){
    Route::get('admin/dashboard',AdminDashboardComponent::class,)->name('admin.dashboard');
});

非常感谢我为我在学校的项目做这件事的帮助

【问题讨论】:

  • use Auth; 放在AttemptToAuthenticate.php 的顶部。
  • 成功了。非常感谢

标签: php laravel authentication


【解决方案1】:

供未来用户使用;

在 AttemptToAuthenticate.php 上,就在 AttemptToAuthenticate.php 下,

添加use Auth; 它仍然会抛出一个错误类 '\laravel\fortify\actions\RouteServiceProvider' not found!

为了解决这个问题,

只需添加 use App\Providers\RouteServiceProvider;,将其定向到默认值 RouteServiceProvider 类。

【讨论】:

    【解决方案2】:

    添加 use Illuminate\Support\Facades\Auth; 在课堂上vendor\laravel\fortify\src\Actions\AttemptToAuthenticate

    【讨论】:

      【解决方案3】:
      use Illuminate\Support\Facades\Auth;
      use App\Providers\RouteServiceProvider;
      

      把这两个放在上面 供应商\laravel\fortify\src\Actions\AttemptToAuthenticate

      【讨论】:

      • 感谢 Amir 的回复,这个问题似乎有多个答案都说同样的话,所以它可能不需要额外的答案,除非它可以增加价值。
      【解决方案4】:
      use Illuminate\Support\Facades\Auth;
      use App\Providers\RouteServiceProvider;
      
      from to vendor\laravel\fortify\src\Actions\AttemptToAuthenticate
      

      但你必须删除行:

      use Laravel\Fortify\Actions\RouteServiceProvider;
      

      因为此行与 RouteServiceProvider.... 重复,但可能仅适用于我的项目

      【讨论】:

        【解决方案5】:
        <?php
        
        namespace Laravel\Fortify\Actions;
        
        
        use Illuminate\Auth\Events\Failed;
        use Illuminate\Contracts\Auth\StatefulGuard;
        use Illuminate\Validation\ValidationException;
        use Laravel\Fortify\Fortify;
        use Laravel\Fortify\LoginRateLimiter;
        use Illuminate\Support\Facades\Auth;
        use App\Providers\RouteServiceProvider;
        
        class AttemptToAuthenticate
        {
            /**
             * The guard implementation.
             *
             * @var \Illuminate\Contracts\Auth\StatefulGuard
             */
            protected $guard;
        
            /**
             * The login rate limiter instance.
             *
             * @var \Laravel\Fortify\LoginRateLimiter
             */
            protected $limiter;
        
            /**
             * Create a new controller instance.
             *
             * @param  \Illuminate\Contracts\Auth\StatefulGuard  $guard
             * @param  \Laravel\Fortify\LoginRateLimiter  $limiter
             * @return void
             */
            public function __construct(StatefulGuard $guard, LoginRateLimiter $limiter)
            {
                $this->guard = $guard;
                $this->limiter = $limiter;
            }
        
            /**
             * Handle the incoming request.
             *
             * @param  \Illuminate\Http\Request  $request
             * @param  callable  $next
             * @return mixed
             */
            public function handle($request, $next)
            {
                if (Fortify::$authenticateUsingCallback) {
                    return $this->handleUsingCustomCallback($request, $next);
                }
        
                if ($this->guard->attempt(
                    $request->only(Fortify::username(), 'password'),
                    $request->filled('remember'))
                ) {
                    if(Auth::user()->utype === 'ADM')
                    {
                        session(['utype'=>'ADM']);
                        return redirect(RouteServiceProvider::HOME);
                    }
                    elseif(Auth::user()->utype === 'USR')
                    {
                        session(['utype'=>'USR']);
                        return redirect(RouteServiceProvider::HOME);
                    }
                    return $next($request);
                }
        
                $this->throwFailedAuthenticationException($request);
            }
        
            /**
             * Attempt to authenticate using a custom callback.
             *
             * @param  \Illuminate\Http\Request  $request
             * @param  callable  $next
             * @return mixed
             */
            protected function handleUsingCustomCallback($request, $next)
            {
                $user = call_user_func(Fortify::$authenticateUsingCallback, $request);
        
                if (! $user) {
                    $this->fireFailedEvent($request);
        
                    return $this->throwFailedAuthenticationException($request);
                }
        
                $this->guard->login($user, $request->filled('remember'));
        
                return $next($request);
            }
        
            /**
             * Throw a failed authentication validation exception.
             *
             * @param  \Illuminate\Http\Request  $request
             * @return void
             *
             * @throws \Illuminate\Validation\ValidationException
             */
            protected function throwFailedAuthenticationException($request)
            {
                $this->limiter->increment($request);
        
                throw ValidationException::withMessages([
                    Fortify::username() => [trans('auth.failed')],
                ]);
            }
        
            /**
             * Fire the failed authentication attempt event with the given arguments.
             *
             * @param  \Illuminate\Http\Request  $request
             * @return void
             */
            protected function fireFailedEvent($request)
            {
                event(new Failed(config('fortify.guard'), null, [
                    Fortify::username() => $request->{Fortify::username()},
                    'password' => $request->password,
                ]));
            }
        }
        

        【讨论】:

        • 你能补充一些解释吗?
        【解决方案6】:

        在 AttemptToAuthenticate.php 文件顶部添加两行

        use Illuminate\Support\Facades\Auth;
        use App\Providers\RouteServiceProvider;
        

        【讨论】:

        • 感谢穆纳的回复。这个问题似乎有多个答案都说同样的事情,所以它可能不需要额外的答案,除非它有一些可以增加价值的东西。
        猜你喜欢
        • 2021-01-13
        • 2018-02-11
        • 1970-01-01
        • 2021-09-25
        • 1970-01-01
        • 2017-04-30
        • 1970-01-01
        • 2021-06-18
        • 2021-08-06
        相关资源
        最近更新 更多