Target class [Auth\TwoFactorController] does not exist. in Laravel 9x php 8x

79 Views Asked by At

I'm having this problem

{"userId":3,"exception":"[object] (Illuminate\\Contracts\\Container\\BindingResolutionException(code: 0): Target class [Auth\\TwoFactorController] does not exist. 

when attempting 2FA in Laravel 9.52.14, following this [resource][Laravel Daily 2FA] and [repo][repository].

<?php

use App\Http\Controllers\Auth\TwoFactorController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\ExportController;
use App\Http\Controllers\LocaleController;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\TestController;
use App\Http\Controllers\MassiveUploadController;
use Laravel\Nova\Http\Controllers\LoginController;

/*
|--------------------------------------------------------------------------
| 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 redirect()->route('login');
});

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



Route::get('verify/resend', 'Auth\TwoFactorController@resend')->name('verify.resend');
Route::resource('verify', 'Auth\TwoFactorController')->only(['index', 'store']);

Route::middleware(['auth', 'TwoFactor'])->group(function () {
    Route::get('/2fa/verify', 'TwoFactorController@index')->name('2fa.verify');
    Route::post('/2fa/verify', 'TwoFactorController@store');
    Route::get('/2fa/resend', 'TwoFactorController@resend')->name('2fa.resend');
});

Route::get('/dashboard', function () {
    return redirect('dash');
})->name('dashboard');

Route::post('/massive_upload', [MassiveUploadController::class, 'store']);

Route::middleware(['auth:sanctum', 'verified'])->group(function (){
    Route::post('/massive_upload/create', [MassiveUploadController::class, 'store'])->name('massive_upload/create');
    Route::get('/massive_upload', [MassiveUploadController::class, 'index']);
    Route::get('/test_export',[ExportController::class,'index']);
});
Route::get('/test', [TestController::class, 'index']);

Route::get('logs', '\Rap2hpoutre\LaravelLogViewer\LogViewerController@index')->middleware('superadmin');

Route::get( '/locale/{locale}', [LocaleController::class,'handle'] )->name('nova-locale');

Route::get('/dashboard/{dashboard}',[DashboardController::class,'handle'])->name('change-dashboard');
Route::get('/2fa/verify', [TwoFactorController::class, 'index'])->name('2fa.verify');

I have this middleware TwoFactor.php script

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class TwoFactor
{
    /**
     * Handle an incoming request.
     *
     * @param  Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next): mixed
    {
        $user = auth()->user();

        if(auth()->check() && $user->two_factor_code)
        {
            if($user->two_factor_expires_at<now())
            {
                $user->resetTwoFactorCode();
                auth()->logout();

                return redirect()->route('login')
                    ->withMessage('The two factor code has expired. Please login again.');
            }

            if(!$request->is('verify*'))
            {
                return redirect()->route('verify.index');
            }
        }

        return $next($request);
    }
}

in app/Http/Middleware/TwoFactor.php. Also I added this controller

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Notifications\TwoFactorCode;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;

class TwoFactorController extends Controller
{
    public function __construct()
    {
        $this->middleware(['auth', 'twofactor']);
    }

    public function index(): Application|Factory|View
    {
        return view('auth.twoFactor');
    }

    public function store(Request $request): RedirectResponse
    {
        $request->validate([
            'two_factor_code' => 'integer|required',
        ]);

        $user = auth()->user();

        if($request->input('two_factor_code') == $user->two_factor_code)
        {
            $user->resetTwoFactorCode();

            return redirect()->route('admin.home');
        }

        return redirect()->back()->withErrors(['two_factor_code' => 'The two factor code you have entered does not match']);
    }

    public function resend(): RedirectResponse
    {
        $user = auth()->user();
        $user->generateTwoFactorCode();
        $user->notify(new TwoFactorCode());

        return redirect()->back()->withMessage('The two factor code has been sent again');
    }
}

in app/Http/Controllers/Auth/TwoFactorController.php (literally creating the /Auth folder), but as we can read it seems that Target class [Auth\TwoFactorController] does not exist.. Anyone knows the problem here?

0

There are 0 best solutions below