title: Auth::user() return null in Laravel 5.8
body: <p>I'm having a problem with authentication in my Laravel 5.8.10 project. I am not using the default form that Laravel creates for authentication. When I access the URL/dashboard in a browser, typically the user would get redirected redirect upon login. The application allows it anyway. Also when I give use <code>Auth::user()</code> it returns null.</p>
  
  <p>When I type an invalid username and password, it's not passed from the login screen. When I type invalid credentials it redirects to the dashboard. The problem of accessing the URL through the dashboard view also continues. It's as if you do not need authentication to access the route.</p>
  
  <p>Note: I have in my .env file a variable <code>PASSWORD_HASH</code> to enable or disable password encryption.</p>
  
  <p><strong>User model</strong></p>
  
  <pre class="lang-php prettyprint-override"><code>namespace App\Entities;
  
  use Illuminate\Notifications\Notifiable;
  use Illuminate\Contracts\Auth\MustVerifyEmail;
  use Illuminate\Foundation\Auth\User as Authenticatable;
  use Illuminate\Database\Eloquent\SoftDeletes;
  
  class User extends Authenticatable
  {
      use Notifiable;
      use SoftDeletes;
  
      protected $table = "users";
      public $timestamps = true;
  
      protected $fillable = [
          'cpf', 'name', 'phone', 'birth', 'gender', 'notes', 'email', 'password', 'status', 'permission'
      ];
  
      protected $hidden = [
          'password', 'remember_token',
      ];
  
      public function groups()
      {
          return $this-&gt;belongsToMany(Group::Class, 'user_groups');
      }
  
      public function setPasswordAttribute($value)
      {
          $this-&gt;attributes['password'] = env('PASSWORD_HASH') ? bcrypt($value) : $value;
      }
  }
  </code></pre>
  
  <p><strong>config/auth.php</strong></p>
  
  <pre class="lang-php prettyprint-override"><code>return [
      'defaults' =&gt; [
          'guard' =&gt; 'web',
          'passwords' =&gt; 'users',
      ],
      'guards' =&gt; [
          'web' =&gt; [
              'driver' =&gt; 'session',
              'provider' =&gt; 'users',
          ],
  
          'api' =&gt; [
              'driver' =&gt; 'token',
              'provider' =&gt; 'users',
              'hash' =&gt; false,
          ],
      ],
      'providers' =&gt; [
          'users' =&gt; [
              'driver' =&gt; 'eloquent',
              'model' =&gt; App\Entities\User::class,
          ],
      ],
      'passwords' =&gt; [
          'users' =&gt; [
              'provider' =&gt; 'users',
              'table' =&gt; 'password_resets',
              'expire' =&gt; 60,
          ],
      ],
  ];
  </code></pre>
  
  <p><strong>DashboardController</strong></p>
  
  <pre class="lang-php prettyprint-override"><code>public function auth(Request $request)
  {
      $data = [
          'email' =&gt; $request-&gt;get('username'),
          'password' =&gt; $request-&gt;get('password')
      ];
      try {
          if (env('PASSWORD_HASH')) {
              Auth::attempt($data, false);
          } else {
              $user = $this-&gt;repository-&gt;findWhere(['email' =&gt; $request-&gt;get('username')])-&gt;first();
  
              if (!$user)
                  throw new Exception("O e-mail informado é inválido. PEEEEN!");
              if ($user-&gt;password != $request-&gt;get('password'))
                  throw new Exception("A senha informada é inválida. PEEEEN!");
              Auth::login($user);
          }
          return redirect()-&gt;route('user.dashboard');
      } catch (Exception $e) {
          return $e-&gt;getMessage();
      }
  }
  </code></pre>
  
  <p><strong>Routes</strong></p>
  
  <pre class="lang-php prettyprint-override"><code>Route::get('/login', ['uses' =&gt; 'Controller@fazerlogin']);
  Route::post('/login', ['as' =&gt; 'user.login', 'uses' =&gt; 'DashboardController@auth']);
  Route::get('/dashboard', ['as' =&gt; 'user.dashboard', 'uses' =&gt; 'DashboardController@index']);
  </code></pre>
  
  <p><strong>View login</strong></p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;section id="conteudo-view" class="login"&gt;
      &lt;h1&gt;Investindo&lt;/h1&gt;
      &lt;h3&gt;O nosso gerenciador de investimento&lt;/h3&gt;
      {!! Form::open(['route' =&gt; 'user.login', 'method' =&gt; 'post']) !!}
      &lt;p&gt;Acesse o sistema&lt;/p&gt;
      &lt;label&gt;
          {!! Form::text('username', null, ['class' =&gt; 'input', 'placeholder' =&gt; "Usuário"]) !!}
      &lt;/label&gt;
      &lt;label&gt;
          {!! Form::password('password', ['placeholder' =&gt; 'Senha']) !!}
      &lt;/label&gt;
      {!! Form::submit('Entrar') !!}
      {!! Form::close() !!}
  &lt;/section&gt;
  </code></pre>
  
  <p><strong>.env</strong></p>
  
  <pre><code>PASSWORD_HASH=false
  </code></pre>
  
  <p>The idea is that when it's false when registering a user it stops encrypting the password, and when true, do the encryption. This is working.</p>
  
  <p><strong>Database User</strong></p>
  
  <p><a href="https://pasteboard.co/IcMC2ds.png" rel="nofollow noreferrer">https://pasteboard.co/IcMC2ds.png</a></p>