krafthaus/bauhaususer
Adds a simple login/user system for the Bauhaus admin package. Installs via Composer, registers the service provider, updates Bauhaus auth permission to use Auth::check(), switches the auth model to BauhausUser\User, runs migrations, and creates users via Artisan.
Installation:
composer require krafthaus/bauhaususer:dev-master
Add the service provider to config/app.php:
'providers' => [
KraftHaus\Bauhaus\BauhausServiceProvider::class,
KraftHaus\BauhausUser\BauhausUserServiceProvider::class,
],
Configure Auth:
Update config/packages/krafthaus/config/admin.php:
'auth' => [
'permission' => function () { return Auth::check(); },
],
Update config/auth.php:
'model' => KraftHaus\BauhausUser\User::class,
Run Migrations:
php artisan migrate --package=krafthaus/bauhaususer
First Use Case: Create a user via Artisan:
php artisan bauhaus:user:create --email=admin@example.com --password=secret
Test login via Bauhaus admin panel (e.g., /admin/login).
User Management:
# Create
php artisan bauhaus:user:create --email=user@example.com --password=pass123
# List
php artisan bauhaus:user:list
# Update
php artisan bauhaus:user:update 1 --email=new@example.com
# Delete
php artisan bauhaus:user:delete 1
use KraftHaus\BauhausUser\User;
$user = User::where('email', 'user@example.com')->first();
$user->update(['password' => bcrypt('newpass')]);
Integration with Bauhaus:
auth middleware (Laravel’s built-in) to protect Bauhaus routes.KraftHaus\BauhausUser\User for granular permissions:
use KraftHaus\BauhausUser\User;
class PostPolicy {
public function update(User $user, Post $post) { ... }
}
Customization:
users table via migrations, then extend the model:
namespace KraftHaus\BauhausUser;
class User extends \KraftHaus\BauhausUser\User {
protected $fillable = ['name', 'role']; // Add custom fields
}
BauhausUserServiceProvider to modify authentication behavior.API Integration:
Auth::attempt() or Auth::login() for API-based logins:
if (Auth::attempt(['email' => $email, 'password' => $password])) {
return response()->json(Auth::user());
}
Migration Conflicts:
users table already exists, drop it or manually merge migrations to avoid schema conflicts.Auth Guard Mismatch:
config/auth.php uses the correct guard (e.g., web or api) and that the model points to KraftHaus\BauhausUser\User.Auth::user() returns null if misconfigured.Permission Logic:
auth.permission callback in admin.php must return true or false synchronously. Async logic (e.g., DB queries) may cause issues.Auth::check() directly or cache permission checks.Artisan Command Errors:
bauhaus:user:create may fail if the users table lacks required columns (e.g., password).users table schema.Check Auth Events:
auth.attempting, auth.failed, or auth.logout events in EventServiceProvider:
protected $listen = [
'auth.failed' => [\App\Listeners\LogFailedLogin::class],
];
Verify Middleware:
auth middleware:
Route::middleware(['auth'])->group(function () {
Route::get('/admin', 'AdminController@index');
});
Logging:
BauhausUserServiceProvider boot method:
public function boot() {
\Log::info('BauhausUser initialized with model:', [User::class]);
}
Custom User Fields:
users table, then extend the model:
// Migration
Schema::table('users', function (Blueprint $table) {
$table->string('api_token')->nullable();
});
// Model
class User extends \KraftHaus\BauhausUser\User {
protected $fillable = ['api_token'];
}
Override Login Logic:
php artisan vendor:publish --provider="KraftHaus\BauhausUser\BauhausUserServiceProvider"
resources/views/vendor/bauhaususer/auth/login.blade.php.Multi-Auth:
guard() helper to switch between BauhausUser and other auth systems:
if (Auth::guard('bauhaus')->attempt($credentials)) {
// ...
}
config/auth.php.Testing:
actingAs helper for tests:
$user = User::factory()->create();
$this->actingAs($user)->get('/admin')->assertOk();
How can I help you explore Laravel packages today?