简体   繁体   中英

Laravel Middleware Auth Group is not working

Good Day. I had a problem with my authentication which I always redirected back into the login page. I found out that when I add in routes/web.php with this code below

Route::group(['middleware' => 'auth'], function() {

}

the page is always redirected back to the login page. But when I remove that code above, I can proceed to the home page. I trying to wonder how to solve this. I use route group in my past projects and I don't have any problems with that.

UPDATE: I used php artisan test and remodify my ExampleTest.php codes.

<?php

namespace Tests\Unit;

use PHPUnit\Framework\TestCase;
use App\User;

class ExampleTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testBasicTest()
    {
        $this->assertTrue(true);
    }

    public function testApplication()
    {
        $user = factory(User::class)->create();

        $response = $this->actingAs($user)
                         ->withSession(['foo' => 'bar'])
                         ->get('/');
    }
}

These are the results

C:\xampp\htdocs\nuadu_helpdesk\vendor\laravel\framework\src\Illuminate\Database\Eloquent\FactoryBuilder.php:273
    269|      */
    270|     protected function getRawAttributes(array $attributes = [])
    271|     {
    272|         if (! isset($this->definitions[$this->class])) {
  > 273|             throw new InvalidArgumentException("Unable to locate factory for [{$this->class}].");
    274|         }
    275|
    276|         $definition = call_user_func(
    277|             $this->definitions[$this->class],

  1   C:\xampp\htdocs\nuadu_helpdesk\vendor\laravel\framework\src\Illuminate\Database\Eloquent\FactoryBuilder.php:296
      Illuminate\Database\Eloquent\FactoryBuilder::getRawAttributes([])

  2   C:\xampp\htdocs\nuadu_helpdesk\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\GuardsAttributes.php:155
      Illuminate\Database\Eloquent\FactoryBuilder::Illuminate\Database\Eloquent\{closure}()

inside routes.php file you not write where to redirect. so please add like below..

Route::group(['middleware' => 'auth'], function() {
  return redirect('/');  //by default you can change it as your requirments.
}

I found out about my problem. I checked using the auth and dd. I used a different primary key with user_id and it is a string type. I forgot to declare it in my user.php

protected $primaryKey = 'user_id';
protected $keyType = 'string';
public $incrementing = false;

By default, the primary key will always be the id. If you declare a different column name that will serve as your primary key, don't forget to declare the codes that I stated above, or else you end not reading the auth or not logging in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM