Support the ongoing development of →
Last updated 3 months ago.

register/create a new middleware? and then check if user_id == 1? check other middlewares for examples.


I guess my first question is why are you trying to allow a certain user the permission to register another user while they are already authenticated? Why don't you just create a controller and a view that allows a form submission for a new user and then provide access to that user to use that controller?

I say this because currently by default there is middleware setup to redirect if authenticated. Please see app\Http\Middleware\RedirectIfAuthenticated.php.


Warning: Below is not well tested and likely not the best way of doing it, but here goes.

In Auth\AuthController.php add:

    public function getRegister()
        if (!Auth::check() || Auth::user()->id !== 1)
            return redirect('/');
        return view('auth.register');

This will override the default controller method and redirect people accessing /auth/register if they're are not logged and/or if they don't have user id 1.

Next you will probably have to add getRegister to except in the AuthController constructor, L5 by default redirects auth'ed users away from auth/login and auth/register, so try the change below.

    public function __construct()
        $this->middleware('guest', ['except' => ['getLogout', 'getRegister']]);

Alternative, you can change the RedirectIfAuthenticated.php middleware, but I think above is a nicer way of doing it.


@tkprocat - That's got me on right track. For some reason when I try it when I log in I get redirected back to the main page, but that is probably due to something in my routes.php file.

The reason why I am doing this is because I am user number 1, and I want to have absolute control over who I give access to.

I will still look at the middleware solutions. It's something I really need to understand in greater depth anyway.

Thanks Neil.


Sign in to participate in this thread!


Your banner here too?


We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2022 - All rights reserved.