Search code examples
laravelmiddleware

Laravel assign middleware to auth route


I want to assign middelware to the register post route. For example like:

Route::post('contact', [
    'as'    => 'contact-store',
    'uses'  => 'ContactmeController@contactUSPost'
])->middleware(\Spatie\Honeypot\ProtectAgainstSpam::class);

My routes files contains

Auth::routes(['verify' => true]);

which generates your login and register routes automagically.

How do I assign the middleware ProtectAgainstSpam to the register POST method with Auth::routes?


Solution

  • you can use it by using group route

    Route::group(['middleware' => ['web']], function() {
        Auth::routes(['verify' => true]);
    });
    

    Auth::routes() is just a helper class that helps you generate all the routes required for user authentication. You can browse the code here https://github.com/laravel/framework/blob/5.3/src/Illuminate/Routing/Router.php instead.

    Here are the routes

        Route::group(['middleware' => ['web']], function() {
            // Authentication Routes...
            $this->get('login', 'Auth\LoginController@showLoginForm')->name('login');
            $this->post('login', 'Auth\LoginController@login');
            $this->post('logout', 'Auth\LoginController@logout')->name('logout');
        
            // Registration Routes...
            $this->get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
            $this->post('register', 'Auth\RegisterController@register');
        
            // Password Reset Routes...
            $this->get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm');
            $this->post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail');
            $this->get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm');
            $this->post('password/reset', 'Auth\ResetPasswordController@reset');
    
      });