What's the output of
public function logout($redirectRoute)
{
dd($redirectRoute);
Auth::logout();
Session::flush();
return to_route($redirectRoute);
}
I'm unsure why it's part of the url
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Laravel 9, using verify email on registration.
What I want to do is when a user's email is not verified and he tries to login, he should be logged out and redirected to the verify-email view.
Reading Laravel 9 documentation and following a tutorial I found, I came up with this:
In my routes/web.php:
Route::group(['middleware' => ['auth']], function() {
Route::get('/logout/{redirectTo}', [LogoutController::class, 'logout'])->name('logout-user');
});
In the LogoutController:
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
class LogoutController extends Controller
{
/**
* Log the user out of the application.
*
*
*/
public function logout($redirectRoute)
{
Auth::logout();
Session::flush();
return to_route($redirectRoute);
}
}
In the LoginController:
protected $redirectTo = RouteServiceProvider::HOME;
protected function redirectTo()
{
if (auth()->user()->hasRole('Customer')) {
if(auth()->user()->email_verified_at) {
return $this->redirectTo;
} else {
return to_route('logout-user', 'verification.notice');
}
}
return route('admin');
}
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
When trying to login with a user that his email is not verified, I get this error: Header may not contain more than a single header, new line detected
What am I doing wrong?
Please or to participate in this conversation.