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:
- Create a new migration file for the table that will store the authentication data.
php artisan make:migration create_auth_table --create=auth_users
- 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();
});
}
- Run the migration to create the table.
php artisan migrate
- Update the
auth
guard in theapp/Providers/AuthServiceProvider.php
file to use the new table.
protected $guards = [
'web' => [
'driver' => 'session',
'provider' => 'users', // Change this to the name of your table
],
];
- Update the
Auth
model in theapp/Models/Auth.php
file to use the new table.
protected $table = 'auth_users';
- Update the
User
model in theapp/Models/User.php
file to extend theAuthenticatable
interface from theIlluminate\Foundation\Auth\User
class instead of theAuthenticatable
interface from theIlluminate\Auth\Authenticatable
class.
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
// ...
}
- Update the
User
model in theapp/Models/User.php
file to use the new table name in the$table
property.
protected $table = 'auth_users';
- Update the
User
model in theapp/Models/User.php
file to use the new table name in the$fillable
property.
protected $fillable = [
'name', 'email', 'password',
];
- Update the
User
model in theapp/Models/User.php
file to use the new table name in the$guardName
property.
protected $guardName = 'web';
- Update the
AuthServiceProvider
in theapp/Providers/AuthServiceProvider.php
file to use the new table name in theboot()
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');
});
});
}
- Update the
AuthServiceProvider
in theapp/Providers/AuthServiceProvider.php
file to use the new table name in theboot()
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
});
});
}
- Update the
routes/web.php
file to use the new table name in themiddleware
property of the routes that require authentication.
Route::middleware(['auth:web'])->group(function () {
// ...
});
- 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.