47

I am trying to disable the register route on my application which is running in Laravel 5.4.

In my routes file, I have only the

Auth::routes();

Is there any way to disable the register routes?

M Uzair Qadeer
  • 482
  • 1
  • 8
  • 19
Dev.W
  • 2,340
  • 9
  • 38
  • 77
  • 4
    In current laravel version 5.7.5 there's is option for this. `Auth::routes(['register' => false]);` – Zohaib Sep 22 '18 at 11:16
  • see https://github.com/laravel/framework/blob/5.7/src/Illuminate/Routing/Router.php#L1152 – Zohaib Sep 22 '18 at 11:26

13 Answers13

95

The code:

Auth::routes();

its a shorcut for this collection of 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');

So you can substitute the first with the list of routes and comment out any route you don't want in your application.

Edit for laravel version => 5.7

In newer versions you can add a parameter to the Auth::routes() function call to disable the register routes:

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

The email verification routes were added:

Route::get('email/verify', 'Auth\VerificationController@show')->name('verification.notice');
Route::get('email/verify/{id}', 'Auth\VerificationController@verify')->name('verification.verify');
Route::get('email/resend', 'Auth\VerificationController@resend')->name('verification.resend');

BTW you can also disable Password Reset and Email Verification routes:

Auth::routes(['reset' => false, 'verify' => false]);
dparoli
  • 8,891
  • 1
  • 30
  • 38
  • why to write this whole section of every auth route, when you can just simply do as `Auth::routes(['register' => false]);`, why? – Haritsinh Gohil Jul 17 '19 at 07:50
  • 2
    @Haritsinh. The question was about laravel version **5.4**. In **5.4** version the `Auth::routes()` function does not accept any argument, that was introduced only in more recent versions. – dparoli Jul 17 '19 at 08:05
  • ok, you are right my friend, `Auth::routes()` supports argument after laravel **5.7** but you can also do like this in laravel version < **5.7** `Route::redirect('register', 'login', 301);` which looks simple than above isn't it? – Haritsinh Gohil Jul 17 '19 at 08:17
  • 1
    @Haritsinh I want to point that this question is tagged and explicity asked for Laravel 5.4. In version 5.4 the function `Route::redirect()` did not exist. – dparoli Jul 17 '19 at 09:02
  • ok, @dparoli it's my fault, i have not seen that, `redirect` also added in `laravel 5.5`, sorry thanks for clearing. – Haritsinh Gohil Jul 17 '19 at 10:13
37

Since Laravel 5.7, a new $options parameter is introduced to the Auth::routes() method; through which you can pass an array to control the generation of the required routes for user-authentication (valid entries can be chosen from the 'register', 'reset', or 'verify' string literals).

Auth::routes(['register' => false]);
someOne
  • 1,975
  • 2
  • 14
  • 20
Irwing Reza
  • 371
  • 3
  • 2
  • 1
    The other option is to add email verification routes, which are disabled by default. `Auth::routes(['verify' => true]);` – Grant Nov 09 '18 at 05:18
  • This should be the answer as it is the best [practice] way to do it if you only want to disable the register route without wanting to edit the other default Auth routes. – Solid Feb 28 '19 at 09:51
  • 2
    This should be considered as the perfect answer, as this is the proper way to ignore the register routes and works with Laravel 5.8 also. – Mehul Panchasara Apr 14 '19 at 18:05
23

You could try this.

Route::match(['get', 'post'], 'register', function(){
    return redirect('/');
});

Add those routes just below the Auth::routes() to override the default registration routes. Any request to the /register route will redirect to the baseUrl.

Jason
  • 4,079
  • 4
  • 22
  • 32
Elvis Mugabi
  • 349
  • 1
  • 5
7

This is deceptively easy! You just need to override two methods in your app/Http/Controllers/Auth/RegisterController.php Class. See below which will prevent the form from being displayed and most importantly block direct POST requests to your application for registrations..

/**
 * Show the application registration form.
 *
 * @return \Illuminate\Http\Response
 */
public function showRegistrationForm()
{
    return redirect('login');
}

/**
 * Handle a registration request for the application.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function register(Request $request)
{
    abort(404);
}
Snaver
  • 434
  • 3
  • 6
6

In web.php, Replace

Auth::routes();

With

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

So that you can remove register route from default auth route list. i tried in 5.7 and it worked fine.

Karthik M
  • 61
  • 1
  • 3
3

Though the above solutions work but, I think changing the middleware to 'auth' in the App\Http\Controllers\Auth\RegisterController will be one of the easiest solutions. This will redirect all visitors to login page if they want to access any of the registration routes. Like this:

namespace App\Http\Controllers\Auth;
class RegisterController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }
asmmahmud
  • 4,844
  • 2
  • 40
  • 47
  • This will redirect user to the login page. And if user enters correct email/password there. He will still be redirected to the Registration page, which is incorrect as per OP's requirement. – Waqas May 06 '18 at 17:46
3

On my Laravel 5.6 project this method didn't work:

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

So I had to use the following method:

Route::match(['get', 'post'], 'register', function () {
    return abort(403, 'Forbidden');
})->name('register');
Abdelsalam Shahlol
  • 1,621
  • 1
  • 20
  • 31
2

I suppose you want to restrict access to some pages for guests and only the admin can register a guest. You can achieve it by adding your own middleware on kernel.php file like below:

protected $routeMiddleware = [
      'authenticated' => \App\Http\Middleware\AuthenticatedMiddleware::class
];

After creating the middleware you have to use it so you can go on web.php file where your routes are and add it to the route you want to restrict like below:

Route::get('register', 'Auth\RegisterController@showRegistrationForm')->name('register')->middleware('authenticated');
Route::post('register', 'Auth\RegisterController@register')->middleware('authenticated');

This way registration is restricted to guests but the admin can still access the page if he ever want to register some other admin!

Don't forget to replace the Auth::routes(); with the detailed list as below:

// 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')->middleware('authenticated');
Route::post('register', 'Auth\RegisterController@register')->middleware('authenticated');

// 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');
glou1986
  • 21
  • 1
2

I guess you can do it like this, in your web.php file:

Route::redirect('register', 'login', 301);
Evan
  • 1,004
  • 11
  • 12
0

Add this two method to app\Http\Controllers\Auth\RegisterController.php

public function showRegistrationForm(){
    return redirect('login');
}

public function register(){

}
sachinsuthariya
  • 435
  • 7
  • 11
0

Just overwrite your auth showRegistrationForm() method ( place this code inside your Auth/RegisterController )

public function showRegistrationForm(){
    return redirect()->route('login');
}

I'm just redirecting my register route to the login route. Here you don't need to append or remove any other code

Abdelsalam Shahlol
  • 1,621
  • 1
  • 20
  • 31
Manu Joseph
  • 389
  • 4
  • 10
-4

Yes, there is a way

Auth::routes();

Remote that route from your web.php in your routes directory.

That route is what controls registration.

Hanny
  • 2,078
  • 6
  • 24
  • 52
  • downvoters this answer not entirely wrong, its only half right – Salal Aslam Sep 14 '17 at 09:35
  • 1
    If you do that, you'll disable the login method too, so this answer is wrong! – user2519032 Sep 14 '17 at 12:07
  • Actually, it's only half right. `Auth::routes();` is a shortcut for a collection of routes per the documentation. If you remove `Auth::routes();` then you can put in whatever routes you *actually do want* individually (such as login). I figured that went without saying, because the documentation covered that - apparently I was wrong. This answer is technically correct and right. I don't know why people still downvoting this... it's literally half of the accepted answer. – Hanny Apr 10 '18 at 12:34
-5

Change to routes :

vendor\laravel\framework\src\Illuminate\Routing\Router.php

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');
}
  • 9
    making changes to any files in vendor folder is the worst idea – Salal Aslam Sep 14 '17 at 09:34
  • 3
    This is a bad idea as the next Laravel update would override the edited file. Thus your changes are lost. It also makes your code hard to follow. Another developer might not think to modify this file and thus never look their for any changes. – geeves Jul 24 '18 at 05:27