Answer the question
In order to leave comments, you need to log in
How to get rid of automatic authorization after registration in Laravel 5.4?
When using Laravel basic authorization/registration, which is generated by artisan, there was a problem: after successful registration, the user is automatically authorized on the site. According to my idea - the user receives login data by mail and does not specify a password during registration. Accordingly, authorization is out of place.
I was looking for solutions to this problem, the most popular of them is to override the register method in RegisterController.php, taking it from the source file:
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);
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Mail\RegistrationSuccess;
use Illuminate\Support\Facades\Mail;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Http\Request;
use Illuminate\Auth\Events\Registered;
class RegisterController extends Controller
{
use RegistersUsers;
protected $redirectTo = '/cp';
public function showRegistrationForm()
{
return view('auth.register', ['no_padding' => true]);
}
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());
}
public function __construct()
{
$this->middleware('guest');
}
protected function registered(Request $request, $user)
{
return view('info', [
'breadcrumbs' => [
[
'name' => 'Регистрация',
'href' => route('register')
],
[
'name' => 'Завершение',
'href' => '#',
'current' => true
]
],
'page' => [
'title' => 'Завершение регистрации',
'content' => 'Регистрация успешно завершена! На адрес '.$request->email.' отправлена информация с данными для входа. Если не видите письма - проверьте его в папке "Спам". Для повторной отправки письма запросите восстановление пароля на странице авторизации'
]
]);
}
protected function validator(array $data)
{
return Validator::make($data, [
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
]);
}
protected function create(array $data)
{
$password = str_random(16);
$user_data = [
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'generate_pass' => $password,
'password' => bcrypt($password),
];
$create_user = User::create($user_data);
if($create_user){
Mail::to($create_user)->send(new RegistrationSuccess($user_data));
unset($user_data);
}
else{
dd('error');
}
}
}
Answer the question
In order to leave comments, you need to log in
In fact, in the default version of laravel authorization, it is enough to comment out the line in the RegistersUsers trait. $this->guard()->login($user);
There is an idea that the authorization was somehow changed, which is why such a problem arises.
I would advise you to install xdebug and follow step by step how the registration is going on (which will save time on finding a solution, and it will become clearer how it works "inside")
As an option that needs to be considered last - kill the session manually after registration
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question