title: Laravel authentication with multiple roles
body: <p>Hi I'm a beginner at laravel, I have to develop a project for human resources management and I have two roles admin and employee. I followed a tutorial at laracast to build roles and abilities table and everything seems to be working. Now I just installed laravel/ui package and I can see login and register which are working fine with the users I registered in the database. My problem now is that I don't know how to connect things together. How can I check if the logged in user is admin so the admin panel opens. Waiting for your replies. Here is the code;
  Error I'm receiving: 419 page expired</p>
  <p>This is what I tried but doesn't work</p>
  <pre><code>protected function authenticated(Request $request, $user)
      {
          // to admin dashboard
          if(auth()-&gt;user()-&gt;roles()-&gt;name === 'admin') {
              return redirect(route('admin'));
          }
  
          // to user dashboard
          else if(auth()-&gt;user()-roles()-&gt;name === 'user') {
              return redirect(route('home'));
          }
  
          abort(404);
      }
  </code></pre>
  <p>Routes</p>
  <pre><code>   Route::get('/', function () {
          return view('welcome');
      });
      
      Auth::routes();
      
      Route::get('/home', 'HomeController@index')-&gt;name('home');
  Route::get('/admin', 'LoginController@authenticated')-&gt;name('admin');
  </code></pre>
  <p>users table</p>
  <pre><code>public function up()
      {
          Schema::create('users', function (Blueprint $table) {
              $table-&gt;id();
              $table-&gt;string('name');
              $table-&gt;string('email')-&gt;unique();
              $table-&gt;timestamp('email_verified_at')-&gt;nullable();
              $table-&gt;string('password');
              $table-&gt;rememberToken();
              $table-&gt;timestamps();
          });
      }
  </code></pre>
  <p>create roles table</p>
  <pre><code>class CreateRolesTable extends Migration
  {
      /**
       * Run the migrations.
       *
       * @return void
       */
      public function up()
      {
          Schema::create('roles', function (Blueprint $table) {
              $table-&gt;bigIncrements('id');
              $table-&gt;string('name');
              $table-&gt;string('label')-&gt;nullable();
              $table-&gt;timestamps();
  
          });
  
          Schema::create('abilities', function (Blueprint $table) {
              $table-&gt;bigIncrements('id');
              $table-&gt;string('name');
              $table-&gt;string('label')-&gt;nullable();
              $table-&gt;timestamps();
  
          });
  
          Schema::create('ability_role', function (Blueprint $table) {
              $table-&gt;primary(['role_id','ability_id']);
  
              $table-&gt;unsignedBigInteger('role_id');
              $table-&gt;unsignedBigInteger('ability_id');
              $table-&gt;timestamps();
  
              $table-&gt;foreign('role_id')
                  -&gt;references('id')
                  -&gt;on('roles')
                  -&gt;onDelete('cascade');
  
              $table-&gt;foreign('ability_id')
                  -&gt;references('id')
                  -&gt;on('roles')
                  -&gt;onDelete('cascade');
          });
  
          Schema::create('role_user', function (Blueprint $table) {
              $table-&gt;primary(['user_id','role_id']);
  
              $table-&gt;unsignedBigInteger('user_id');
              $table-&gt;unsignedBigInteger('role_id');
              $table-&gt;timestamps();
  
              $table-&gt;foreign('user_id')
              -&gt;references('id')
              -&gt;on('users')
              -&gt;onDelete('cascade');
  
              $table-&gt;foreign('role_id')
                  -&gt;references('id')
                  -&gt;on('roles')
                  -&gt;onDelete('cascade');
  
          });
      }
  </code></pre>
  <p>User.php</p>
  <pre><code>public function roles()
      {
          if(is_string($role))
          {
              $role = Role::whereName($role)-&gt;firstOrFail();
          }
          return $this-&gt;belongsToMany(Role::class)-&gt;withTimestamps();
      }
  
      public function assignRole($role)
      {
          $this-&gt;roles()-&gt;sync($role, false);
      }
  
      public function abilities($role)
      {
         return $this-&gt;roles-&gt;map-&gt;abilities-&gt;flatten()-&gt;pluck('name')-&gt;unique();
      }
  </code></pre>
  <p>Role.php</p>
  <pre><code>class Role extends Model
  {
      protected $guarded = [];
      
      public function abilities()
      {
          return $this-&gt;belongsToMany(Ability::class)-&gt;withTimestamps();
      }
  
      public function allowTo($ability)
      {
          $this-&gt;abilities()-&gt;sync($ability,false);
      }
  }
  </code></pre>
  <p>Ability.php</p>
  <pre><code>{
      protected $guarded = [];
      
      public function roles()
      {
          if(is_string($ability))
          {
              $ability = Ability::whereName($ability)-&gt;firstOrFail();
          }
          return $this-&gt;belongsToMany(Role::class)-&gt;withTimestamps();
      }
  }
  </code></pre>