Categories: Development

How to allow only 1 user to register in a Laravel application

How to enforce only one user to register or be registered in a Laravel web application is very simple to implement.

Find your registered user controller most likely at App\Http\Controllers\Auth\RegisteredUserController.php and in the create() method add the following code before the register page view is returned:

if (User::count() >= 1) {//Allow only 1 user to register
    return redirect()->route('home')->with('Registration not allowed currently');
}

This checks for if there is more than 1 user in the database to return to the ‘home’ route with the message ‘Only 1 user allowed’.

The performance impact of this is very minor even though a count is used the database either has 0 or 1 rows anyway making it a fast process.

You can obviously also adjust the number of users you want to be allowed registered.

Make sure if you are using an API to create users that you also apply this conditional to the store() method and return a response in JSON:

return Response::json(['message' => 'Registration not allowed currently']);
Share

Recent Posts

Kennington reservoir drained drone images

A drained and empty Kennington reservoir images from a drone in early July 2024. The…

2 years ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

2 years ago

Early September spring shots

Some early September spring shots from a bushwalk, It is still too early for wildflowers.

3 years ago

Cloning an NVMe to SK Hynix Platinum P41 NVMe

I recently needed to clone over a smaller NVMe to a 2TB SK Hynix Platinum…

3 years ago

Using Laravel’s deleteDirectory to delete folders and their contents

If you ever needed a simple solution in Laravel to delete an entire directory and…

3 years ago

Using React Icons

React icons is the easiest way to get all the common and popular icon packs…

3 years ago