Reputation: 339
Hi i want to check if user email verified only in one function in a controller, i don't want to set the check in the middle-ware or in the route like this:
public function __construct()
{
$this->middleware('verified');
}
because the controller is accessible to guest also just one function (create) in the controller require email to be verified and user logged in how do i do that, thanks
public function create()
{
// checking if authenticated but need to check if email verified also
if (Auth::check()) {
// Get the currently authenticated user...
$user = Auth::user();
// get the list of states
$states = State::orderBy('name', 'asc')->get();
// get all cities by state_id
$state_id = '1';
$cities = City::where('state_id', $state_id)->orderBy('name', 'asc')->get();
return view('pages.create_product', ['user' => $user, 'states' => $states, 'cities' => $cities]);
} else {
return redirect()->route('login');
}
}
Upvotes: 3
Views: 12620
Reputation: 4684
I would recommend you either do it in the web.php
routes file like this:
Route::get('example/create', ExampleController@create)->middleware('verified');
Or do it in the controller constructor but for a specific method like this:
public function __construct()
{
$this->middleware('verified')->only('create');
}
If you really want to do it within the controller method, you can check by using this code.
$user = Auth::user();
$user->hasVerifiedEmail();
And do not forget to include MustVerifyEmail
interface on your User
model.
<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable implements MustVerifyEmail
{
use Notifiable;
// ...
}
Upvotes: 16