Question: Disable Auto Login after registration in Laravel 5.3

Question

Disable Auto Login after registration in Laravel 5.3

Answers 4
Added at 2016-12-31 13:12
Tags
Question

Please can someone help me. How do I disable auto login after registration in laravel 5.3. I can do it in 5.2, but not in 5.3.

Answers to

Disable Auto Login after registration in Laravel 5.3

nr: #1 dodano: 2016-12-31 13:12

In 5.3 you just need to override the register method. You can add the following to your App\Http\Controllers\Auth\RegisterController:

/**
 * Handle a registration request for the application.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function register(Request $request)
{
    $this->validator($request->all())->validate();

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

    //The auto login code has been removed from here.

    return redirect($this->redirectPath());
}

Furthermore, you can either change the redirect path in the register method itself or just update the $redirectTo property towards the top of the class.

Hope this helps!

nr: #2 dodano: 2016-12-31 13:12

In 5.3 you need to override register() method in RegisterController.php which will be build after using make:auth command. Just comment login() part:

//$this->guard()->login($user);
nr: #3 dodano: 2016-12-31 13:12

There is no AuthController.php in Laravel 5.3.* so modify your RegisterController.php and add these lines

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

then, add this line to handle registration request for the above parameters

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());
}

 $this->guard()->login($user);
nr: #4 dodano: 2016-12-31 19:12

Inside RegistersUsers trait there is a function

protected function registered(Request $request, $user)
{
    if(!$user->active){
        // store msg in session and display to user
        Auth::logout();
    }
}

this is the function that executes as soon the user registers, here even you check check if user is verified and any other code and Auth::logout(), this is the best way to do it. No need to change any code elsewhere that can messup with framework.

Source Show
◀ Wstecz