Laravel basic auth using another table for credentials.

Updated: Feb 05, 2025

Laravel basic auth using another table for credentials.

To implement basic authentication in Laravel using a table other than the default users table for storing credentials, follow these steps:

  1. Create a new migration file for the table that will store the authentication data.
php artisan make:migration create_auth_table --create=auth_users
  1. Define the schema for the new table in the up() method of the migration file.
public function up()
{
    Schema::create('auth_users', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}
  1. Run the migration to create the table.
php artisan migrate
  1. Update the auth guard in the app/Providers/AuthServiceProvider.php file to use the new table.
protected $guards = [
    'web' => [
        'driver' => 'session',
        'provider' => 'users', // Change this to the name of your table
    ],
];
  1. Update the Auth model in the app/Models/Auth.php file to use the new table.
protected $table = 'auth_users';
  1. Update the User model in the app/Models/User.php file to extend the Authenticatable interface from the Illuminate\Foundation\Auth\User class instead of the Authenticatable interface from the Illuminate\Auth\Authenticatable class.
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    // ...
}
  1. Update the User model in the app/Models/User.php file to use the new table name in the $table property.
protected $table = 'auth_users';
  1. Update the User model in the app/Models/User.php file to use the new table name in the $fillable property.
protected $fillable = [
    'name', 'email', 'password',
];
  1. Update the User model in the app/Models/User.php file to use the new table name in the $guardName property.
protected $guardName = 'web';
  1. Update the AuthServiceProvider in the app/Providers/AuthServiceProvider.php file to use the new table name in the boot() method.
public function boot()
{
    $this->app->make(Authenticator::class)->configure('guard', function ($guard) {
        $guard->defaults([
            'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKWxm6DBjrPj6eZNvaFasIuMwDQifK6CEq0omfBMhaeM4fBC1Bdn18FD2aIDLj4NQj0zJ6SgZ64add1pq8' // Password is for testing purpose only.
        ]);

        $guard->provider(function ($app, $defaultGuard = null) {
            return Auth::provider($this->app['config']['auth.defaults.guard'] ?? 'web');
        });
    });
}
  1. Update the AuthServiceProvider in the app/Providers/AuthServiceProvider.php file to use the new table name in the boot() method.
public function boot()
{
    $this->app->make(Authenticator::class)->configure('guard', function ($guard) {
        $guard->defaults([
            'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKWxm6DBjrPj6eZNvaFasIuMwDQifK6CEq0omfBMhaeM4fBC1Bdn18FD2aIDLj4NQj0zJ6SgZ64add1pq8' // Password is for testing purpose only.
        ]);

        $guard->provider(function ($app, $defaultGuard = null) {
            return new UserProvider($app['db'], 'auth_users'); // Use your table name here
        });
    });
}
  1. Update the routes/web.php file to use the new table name in the middleware property of the routes that require authentication.
Route::middleware(['auth:web'])->group(function () {
    // ...
});
  1. Update the app/Http/Kernel.php file to use the new table name in the $routeMiddleware property.
protected $routeMiddleware = [
    // ...
    'auth:web' => \App\Http\Middleware\Authenticate::class,
];

Now, Laravel will use the new table for storing authentication data.