php - Laravel | Redirect to verify email if not verified

Im new to Laravel. Currently, when you visit a page and your account is not verified, nothing happens. I want to make it so if your logged in and your email isnt verified, it will redirect you to the verify email page, but only if your logged in. I tried already but failed.
I made a Middleware with this but it seems not to work.
if (Auth::user() && ! Auth::user()->email_verified_at) {
return redirect('auth/verify');;
}
Here is my web.php
// Home Page
Route::get('/', function () {
return view('home');
})->middleware('verifiedEmail');
// Private Page
Route::get('/private', [HomeController::class, 'private']);
// Home Page
Route::get('/home', 'HomeController@index')->name('home');
// Shop Page
Route::get('/shop', 'ShopController@index')->name('shop');
/*
|===========================
| Other Routes
|===========================
*/
// Mailing
Route::get('/email', function() {
Mail::to($request->user())->send(new MailableClass);
return new WelcomeMail;
});
Route::get('/sendtestnews', [TestNewsController::class,
'sendTestNotification']);
// Verification
Auth::routes(['verify' => true]);
Answer
Solution:
Look like you are using wrong url while redirecting it should be
return redirect()->route('verification.notice');
To check verify routes.Run following command
php artisan route:list
this will show all routes defined inweb.php
andapi.php
| POST | email/resend | verification.resend | App\Http\Controllers\Auth\VerificationController@resend | web |
| | | | | | auth |
| | | | | | throttle:6,1 |
| | GET|HEAD | email/verify | verification.notice | App\Http\Controllers\Auth\VerificationController@show | web |
| | | | | | auth |
| | GET|HEAD | email/verify/{id}/{hash} | verification.verify | App\Http\Controllers\Auth\VerificationController@verify | web
Updated
|<form class="d-inline" method="POST" action="email/resend">
<input type="hidden" name="_token" value="o8OVdlN1Pvu5IrpKvtXybsLiYC0H8czZTW7AIf5Y"> <button type="submit" class="btn btn-link p-0 m-0 align-baseline">click here to request another</button>.
</form>
As you can see email/resend form look like above.email/resend(route verification.resend )
is post method
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: the requested url was not found on this server. xampp
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.