0

In my Laravel application, after a new registration, it connects automatically to this new account.

I just need to register and stay connected with the actual Auth Account. How can we change this default setting?

Because I'm creating new accounts in the application with the admin user. Thank you

This is my registerController code:

 use RegistersUsers;
protected function redirectTo()
{
if(Auth::user()->is_admin == 1){
  return 'persons';
}
return '/persons';
}
public function __construct()
{
    $this->middleware('auth');
}
protected function validator(array $data)
{

    return Validator::make($data, [
        'name' => 'required|string|max:255',
        'email' => 'required|string|email|max:255|unique:users',
        'password' => 'required|string|min:6|confirmed',

    ]);
}
protected function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
    ]);
}

In Registeruser.php I changed the function register to

public function register(Request $request)
{
    $this->validator($request->all())->validate();

    event(new Registered($user = $this->create($request->all())));


    return $this->registered($request, $user)
                    ?: redirect($this->redirectPath());
}

Note please that I create new users using person.blade.php, and not /register

4

1 回答 1

4

在您App/Http/Controllers/Auth/RegisterController需要registerRegistersUserstrait 覆盖该方法:

public function register(Request $request)
{
    $this->validator($request->all())->validate();

    event(new Registered($user = $this->create($request->all())));

    $this->guard()->login($user);

    return $this->registered($request, $user)
                    ?: redirect($this->redirectPath());
}

这一行:$this->guard()->login($user);是用户登录的地方。您可以删除它或修改它以满足您的需要。

现在,如果您想在注册后根据用户类型重定向到某个地方,您需要替换protected $redirectTo为:

protected function redirectTo()
{
    //You would need to modify this according to your needs, this is just an example.
    if(Auth::user()->hasRole('admin')){
      return 'path';
    }

    if(Auth::user()->hasRole('regular_user')){
      return 'path';
    }

    return 'default_path';
}

在您的文件顶部,添加以下内容:

use Illuminate\Http\Request; use Illuminate\Auth\Events\Registered;

于 2018-11-08T18:52:55.610 回答