Do not login after registration if user has already registered

Yes, you read that correctly. Let me explain. I am using the default authentication process that comes with laravel 5.4 and I want admins to be able to register new users. I changed the middleware RedirectIfNotAuthenicated

to allow access to /register

if the user who is logged in is an administrator (in my case that means they have type "1"):

public function handle($request, Closure $next, $guard = null)
{
    if (Auth::guard($guard)->check()) {

        if(!Auth::user()->type == '1'){
            return redirect('/');
        }

    }

    return $next($request);
}

      

Now my question becomes, how inside an existing one RegisterController

CANNOT login after successful registration if the account is created by the administrator. Obviously this will involve checking to see if the user is currently and an admin, but where did I hang myself after this block, since to me it looks like something else is doing?

protected function create(array $data)
{

    if(isset($data["director"])){
        $this->redirectTo = '/organizations/create';
    }

    if(Auth::user()->type == '1'){

        //create the user and not log them in

    } else {

        return User::create([
            'first_name' => $data['first_name'],
            'last_name' => $data['last_name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);

    }

}

      

+3


source to share


1 answer


First of all, you don't need to use the RegisterController that comes with Laravel to create users as an administrator. In whatever AdminController you are using, you can create the following code to create a new user with the correct reason check:

User::create([
            'first_name' => $data['first_name'],
            'last_name' => $data['last_name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);

      

You just need to store the user data in the appropriate tables and once the user is logged in from the frontend LoginController, you will handle the rest.



Also, the RegisterController that ships with Laravel uses the RegistersUsers Trait function, which has additional functionality that you noticed in the controller.

Illuminate\Foundation\Auth\RegistersUsers.php

      

+1


source







All Articles