Framework: 'verified' middleware does not work with 'auth' middleware?

Created on 13 Dec 2018  路  8Comments  路  Source: laravel/framework

  • Laravel Version: 5.7.17 (latest)
  • PHP Version: 7.2.13
  • Database Driver & Version: MySQL 5.7

Description:

When adding ->middleware(['auth', 'verified']) to a route, the verified middleware doesn't apply.

Steps To Reproduce:

  • laravel new blog
  • php artisan make:auth
  • Create database and modify .env file
  • php artisan migrate
  • php artisan tinker then:
\App\User::create([
    'name' => 'Test User',
    'email' => '[email protected]',
    'password' => bcrypt('secret'),
]);
  • Add $this->middleware(['auth', 'verified']); to the HomeController constructor
  • Login and the home view is still shown even though you haven't verified the email address.
needs more info

Most helpful comment

@ryanmortier infact you didn't waste our tym, very many people forget this

All 8 comments

It's normal that your login view is still shown as this is controlled by the LoginController and not the HomeController. You're sure you're not mixing up the "welcome" view and "home" view?

It's definitely not the welcome view. It's specifically the resources/views/home.blade.php view, the one that says "You are logged in!" and is presented in the HomeController index method, the same controller that has both those middleware attached.

Can you post your User model?

Since this is a fresh install it's the default:

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    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',
    ];
}

Are you able to reproduce the issue? It should only take 30 seconds.

You need to implement the MustVerifyEmail interface: https://laravel.com/docs/5.7/verification#introduction

That should do it.

Oops, sorry for wasting your time.

@ryanmortier infact you didn't waste our tym, very many people forget this

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Fuzzyma picture Fuzzyma  路  3Comments

PhiloNL picture PhiloNL  路  3Comments

kerbylav picture kerbylav  路  3Comments

felixsanz picture felixsanz  路  3Comments

jackmu95 picture jackmu95  路  3Comments