How to disable new user registration in Laravel 5

I am trying to make a single-user admin panel (which has only one user) in Laravel 5, and I registered this user, so now I want to disable registration for new users, of course, the login form should work, but there is no new register from now on. How can i do this?

I use the default user login and register in version 5.

+94
php laravel
Mar 21 '15 at 13:37
source share
26 answers

The following functionality appeared in Laravel 5.7:

Auth::routes(['register' => false]); 

Possible options:

 Auth::routes([ 'register' => false, // Registration Routes... 'reset' => false, // Password Reset Routes... 'verify' => false, // Email Verification Routes... ]); 

For older versions of Laravel, simply override the showRegistrationForm() and register() methods in

  • AuthController for Laravel 5.0 - 5.4
  • Auth/RegisterController.php for Laravel 5.5
 public function showRegistrationForm() { return redirect('login'); } public function register() { } 
+159
Mar 21 '15 at 13:46
source share

If you are using Laravel 5.2 and you have installed its related functions using php artisan make:auth , then your app/Http/routes.php will include all auth-related routes by simply calling Route::auth() .

The auth () method can be found in vendor/laravel/framework/src/Illuminate/Routing/Router.php . Therefore, if you want to do as some people suggest and turn off registration by deleting unnecessary routes (maybe this is a good idea), you need to copy the routes that you still want to use using the auth () method, and put them in app/Http/routes.php (replacing call Route :: auth ()). For example:

 <?php // This is app/Http/routes.php // Authentication Routes... Route::get('login', 'Auth\AuthController@showLoginForm'); Route::post('login', 'Auth\AuthController@login'); Route::get('logout', 'Auth\AuthController@logout'); // Registration Routes... removed! // Password Reset Routes... Route::get('password/reset/{token?}', 'Auth\PasswordController@showResetForm'); Route::post('password/email', 'Auth\PasswordController@sendResetLinkEmail'); Route::post('password/reset', 'Auth\PasswordController@reset'); 

If you use a lower version than 5.2, then this is probably different, I remember that everything changed quite a bit from 5.0, at some point artisan make:auth was even removed by IIRC.

+53
Jun 08 '16 at 13:52
source share

This may be new in 5.7, but now there is an options method for the auth method. Just by changing Auth::routes(); in Auth::routes(['register' => false]); in your routes file after running php artisan make:auth will disable user registration.

+48
Oct 10 '18 at 18:45
source share

For Laravel 5.3 and 5.4, here is the correct way:

You must change:

 public function __construct() { $this->middleware('guest'); } 

to

 public function __construct() { $this->middleware('auth'); } 

in app / Http / Controller / Auth / RegisterController.php

+33
Feb 28 '17 at 15:40
source share

Starting with Laravel 5.7, you can pass an array of options to Auth::routes() . Then you can disable the route register with:

 Auth::routes(['register' => false]); 

You can see how this works from the source code: src / Illuminate / Routing / Router.php .

+28
Oct 28 '18 at 10:07
source share

Method 1 for version 5.3

There is no AuthController in laravel 5.3. To disable the registration route, you must modify the RegisterController constructor as follows:

You can change the form:

 public function __construct() { $this->middleware('guest'); } 

so that:

 use Illuminate\Support\Facades\Redirect; public function __construct() { Redirect::to('/')->send(); } 

Note: to use Redirect do not forget user Redirect; Thus, the user gets access to https: // hostname / registers his redirection to "/".

Method 2 for version 5.3

When we use php artisan make:auth it adds Auth::route(); automatically. Please redefine the route in /routes/web.php. You can change it like this: * you need to comment on this line: Auth::routes();

  <?php /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | This file is where you may define all of the routes that are handled | by your application. Just tell Laravel the URIs it should respond | to using a Closure or controller method. Build something great! | */ // Auth::routes(); Route::get('/login', 'Auth\LoginController@showLoginForm' ); Route::post('/login', 'Auth\LoginController@login'); Route::post('/logout', 'Auth\LoginController@logout'); Route::get('/home', 'HomeController@index'); 

Thank! I hope this can solve your problems.

+26
Oct 14 '16 at 2:23
source share

Overwriting getRegister and postRegister is complicated - if you use git, there is a high chance that .gitignore set to ignore the framework files, which will result in registration being still possible in your production environment (if laravel is installed through a composer, for example)

Another possibility is to use routes.php and add this line:

 Route::any('/auth/register','HomeController@index'); 

Thus, the frame files remain valid, and any request will still be redirected from the Framework register module.

+11
Sep 23 '15 at 15:54
source share

AuthController.php @limonte is located in App\Http\Controllers\Auth and not in the vendor directory, so Git does not ignore this change.

I added this function:

 public function register() { return redirect('/'); } public function showRegistrationForm() { return redirect('/'); } 

and it works correctly.

+11
Feb 18 '16 at 0:47
source share

LAravel 5.6

 Auth::routes([ 'register' => false, // Registration Routes... 'reset' => false, // Password Reset Routes... 'verify' => false, // Email Verification Routes... ]); 
+9
Dec 16 '18 at 0:31
source share

Here is my 5.4 solution:

 //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... //Route::get('register', 'Auth\RegisterController@showRegistrationForm')->name('register'); //Route::post('register', 'Auth\RegisterController@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'); 

Note. I commented on Auth::routes() and the two registration routes.

Important: you must also make sure that you delete all route('register') instances in the app.blade layout, or Laravel throws an error.

+8
Apr 21 '17 at 23:35
source share

The following method works just fine:

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

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

Add ALLOW_USER_REGISTRATION= to .env and control the user’s registration by setting it to true or false.

You now have full control over the routes, and the 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 of the previous answers, in particular Rafal G. and Daniel Sentore.

+7
Mar 23 '17 at 17:58
source share

On laravel 5.6 and above you can edit in web.php file

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

and you can make it true if you change your mind it's so easy for me

+6
May 14 '19 at 18:12
source share

In routes.php just add the following:

 if (!env('ALLOW_REGISTRATION', false)) { Route::any('/register', function() { abort(403); }); } 

You can then selectively control whether registration in the .env file is .env .

+5
Aug 10 '16 at 15:19
source share

I had to use:

 public function getRegister() { return redirect('/'); } 

Using Redirect :: to () gave me an error:

 Class 'App\Http\Controllers\Auth\Redirect' not found 
+3
Mar 25 '15 at 4:33
source share

In Laravel 5.4

You can find all routes registered through Auth::routes() in the class \Illuminate\Routing\Router in the auth() method

it looks like this:

 /** * Register the typical authentication routes for an application. * * @return void */ public function auth() { // 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')->name('password.request'); $this->post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email'); $this->get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset'); $this->post('password/reset', 'Auth\ResetPasswordController@reset'); } 

Just copy the routes you need / need and you're fine!

+3
Mar 17 '17 at 22:32
source share

In laravel 5.3, you must override the default showRegistrationForm() by entering the code below into the RegisterController.php file in app\Http\Controllers\Auth

  /** * Show the application registration form. * * @return \Illuminate\Http\Response */ public function showRegistrationForm() { //return view('auth.register'); abort(404); //this will throw a page not found exception } 

since you do not want to allow registration, it is better to simply throw 404 error so that the attacker knows that he is lost. And when you are ready to register in your application, uncomment //return view('auth.register'); , then comment on abort(404);

\\\\\\\\\\\\\\\\\\\\\\\\ JUST FYI /////////////////////// //////

If you need to use multiple authentication, for example to create auth for users, participants, students, administrators, etc., then I advise you to check this hesto / multi-auth is its awesome package for unlimited authorizations in L5 applications.

You can read more paragraphs on the Auth methodology and its associated file in this writeup.

+2
Dec 15 '16 at 23:12
source share

In Laravel 5.5

I tried to solve the same problem in Laravel 5.5. Instead of using Auth::routes() in the web.php routes file, I included only the I / O routes:

 Route::get('login', 'Auth\LoginController@showLoginForm')->name('login'); Route::post('login', 'Auth\LoginController@login'); Route::post('logout', 'Auth\LoginController@logout')->name('logout'); 
+2
Oct 07 '17 at 20:23
source share

This was mentioned in previous comments, but I would like to clarify that there are several ways to access authorization routes in your web.php file in Laravel ^ 5.7. it may look a little different depending on your version, but they achieve the same result.

First option

 Route::auth([ 'register' => false, // Registration Routes... 'reset' => false, // Password Reset Routes... 'verify' => false, // Email Verification Routes... ]); 

Second option

 Auth::routes([ 'register' => false, // Registration Routes... 'reset' => false, // Password Reset Routes... 'verify' => false, // Email Verification Routes... ]); 
+1
Aug 20 '19 at 10:07 on
source share

In order not to change the code, just create a middleware to determine if the request URL is url ("register"), then redirected to 404 or anywhere.

0
Jul 19. '16 at 15:49
source share

In Laravel 5.5

Working on a similar problem and setting the middleware argument from guest to "auth" seemed like a more elegant solution.

Edit file: app-> http-> Controllers-> Auth-> RegisterController.php

 public function __construct() { //replace this //$this->middleware('guest'); //with this argument. $this->middleware('auth'); } 

I could be wrong though ... but it seems smoother than editing routing with more lines and less credibility than just redirecting the page ... at least in this case, to block the registration for guests.

0
Dec 19 '17 at 8:10
source share

I think that would be a better solution.

Override the following methods as described below in

App \ Http \ Controller \ Auth \ RegisterController.php

 use Illuminate\Http\Response; . . . public function showRegistrationForm() { abort(Response::HTTP_NOT_FOUND); } public function register(Request $request) { abort(Response::HTTP_NOT_FOUND); } 
0
Jan 09 '18 at 19:07
source share

In Laravel 5.5, everything is very simple if you use the CRUD route system.

Go to app/http/controllers/RegisterController there is a namespace: Illuminate\Foundation\Auth\RegistersUser

You need to go to RegistersUser: Illuminate\Foundation\Auth\RegistersUser

There is a call to the showRegistrationForm method showRegistrationForm changes this: return view('auth.login'); for this: return redirect()->route('auth.login'); and remove the routing call registry from your blade page. It might look like this:

  <li role="presentation"> <a class="nav-link" href="{{ route('register') }}">Register</a> </li> 
0
Feb 10 '18 at 17:25
source share

I found this to be the easiest solution in Laravel 5.6! It redirects anyone trying to go to yoursite.com/register to yoursite.com

routes / web.php

 // redirect from register page to home page Route::get('/register', function () { return redirect('/'); }); 
0
Apr 12 '18 at 8:16
source share

All I did was replace the register blade code with the login blade code. Thus, registration still goes to the entrance.

resources/views/auth/register.blade.php is replaced by resources/views/auth/login.blade.php

0
Aug 23 '18 at 17:56
source share

For Laravel 5. 6+, paste the following methods in app\Http\Controller\Auth\RegisterController

 /* * Disabling registeration. * */ public function register() { return redirect('/'); } /* * Disabling registeration. * */ public function showRegistrationForm() { return redirect('/'); } 

Now you override these methods in the RegistersUser property, when you change your mind, delete these methods. You can also comment on register links in welcome.blade.php and login.blade.php views.

0
04 Sep '18 at 12:42
source share

add

 use \Redirect; 

at the top of the file

-10
May 05 '15 at 7:05
source share



All Articles