Iniciaremos asumiendo que ya esta funcionando la autenticación de usuarios como lo explicamos anteriormente.
Para habilitar la verificación por correo hay tres pasos a seguir.
PASO 1. Se debe modificar la ruta de autenticación Auth::routes();
en el archivo de rutas normalmente routes/web.php.
Auth::routes(['verify' => true]);
o agregando las rutas manualmente.
Route::get('email/verify', 'Auth\VerificationController@show')->name('verification.notice'); Route::get('email/verify/{id}', 'Auth\VerificationController@verify')->name('verification.verify'); Route::get('email/resend', 'Auth\VerificationController@resend')->name('verification.resend');
PASO 2. Agregamos la implementación de la clase de verificación de correo electrónico en el modelo de usuario en el archivo app/User.php.
<?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; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'email', 'password', ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; /** * The attributes that should be cast to native types. * * @var array */ protected $casts = [ 'email_verified_at' => 'datetime', ]; }
PASO 3. Protegiendo las rutas o controladores con el middleware(['verified']);
.
Ruta
Route::get('/home', 'HomeController@index')->name('home')->middleware(['auth', 'verified']);
Controlador
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class HomeController extends Controller { /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware(['auth', 'verified']); } /** * Show the application dashboard. * * @return \Illuminate\Contracts\Support\Renderable */ public function index() { return view('home'); } }