How to disable registration new users in Laravel

后端 未结 27 2910
鱼传尺愫
鱼传尺愫 2020-12-07 10:34

I\'m using Laravel (v5).

I need one user and I\'ve already registered that. Now I want to disable registration for new users. Of course, I need the login form to wor

27条回答
  •  生来不讨喜
    2020-12-07 11:19

    The following method works great:

    Copy all the routes from /vendor/laravel/framework/src/Illuminate/Routing/Router.php and paste it into web.php and comment out or delete Auth::routes().

    Then setup a conditional to enable and disable registration from .env. Duplicate the 503.blade.php file in views/errors and create a 403 forbidden or whatever you like.

    Add ALLOW_USER_REGISTRATION= to .env and control user registration by setting its value to true or false.

    Now you have full control of routes and Vendor files remain untouched.

    web.php

    //Auth::routes();
    
    // Authentication Routes...
    Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
    Route::post('login', 'Auth\LoginController@login');
    Route::post('logout', 'Auth\LoginController@logout')->name('logout');
    
    // Registration Routes...
    if (env('ALLOW_USER_REGISTRATION', true))
    {
        Route::get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
        Route::post('register', 'Auth\RegisterController@register');
    }
    else
    {
        Route::match(['get','post'], 'register', function () {
            return view('errors.403');
        })->name('register');
    }
    
    // Password Reset Routes...
    Route::get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
    Route::post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
    Route::get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
    Route::post('password/reset', 'Auth\ResetPasswordController@reset');
    

    This is a combination of some previous answers notably Rafal G. and Daniel Centore.

提交回复
热议问题