bengor-user/user
Lightweight, flexible user management library built with Domain-Driven Design. Includes registration (basic/confirmation/invitation), password changes and resets, login/logout, user removal, role grant/revoke, and purging outdated invitation/remember tokens.
Installation
composer require bengor-user/user
Add to config/app.php under providers:
BenGor\User\UserServiceProvider::class,
Publish the config:
php artisan vendor:publish --provider="BenGor\User\UserServiceProvider"
First Use Case: Basic Registration
use BenGor\User\User;
use BenGor\User\Services\UserService;
$userService = app(UserService::class);
$user = $userService->register([
'email' => 'user@example.com',
'password' => 'secure123',
'name' => 'John Doe',
]);
Where to Look First
config/bengor-user.php (customize token TTL, password rules, etc.)BenGor\User\Services\UserService (core methods for CRUD operations)BenGor\User\User, BenGor\User\Role (entities and their invariants)BenGor\User\Events (listen for UserRegistered, PasswordChanged, etc.)Registration Variants
$userService->register($userData);
$token = $userService->generateConfirmationToken($user);
// Send email with $token
$userService->confirmUser($user, $token);
$invitation = $userService->createInvitation($inviter, $email);
// Send email with $invitation->token
$user = $userService->acceptInvitation($invitation, $password);
Authentication
$user = $userService->login($email, $password);
// Store $user->rememberToken in session
$userService->logout($user);
Password Management
$userService->changePassword($user, $oldPassword, $newPassword);
$token = $userService->generateRememberPasswordToken($email);
// Send email with $token
$userService->resetPassword($token, $newPassword);
Role Management
$userService->grantRole($user, 'admin');
$userService->revokeRole($user, 'admin');
if ($userService->hasRole($user, 'admin')) { ... }
Token Cleanup
$userService->purgeExpiredTokens();
Laravel Integration
BenGor\User\Http\Middleware\Authenticate for route protection.UserService to Laravel’s container in AppServiceProvider:
$this->app->bind('BenGor\User\Services\UserService', function ($app) {
return new UserService(
$app->make('BenGor\User\Repositories\UserRepository'),
$app->make('BenGor\User\Repositories\RoleRepository'),
$app->make('BenGor\User\Repositories\TokenRepository')
);
});
UserRegistered to trigger welcome emails:
UserRegistered::class => [MailService::class, 'sendWelcomeEmail'],
Customizing User Model
User model:
namespace App\Models;
use BenGor\User\User as BaseUser;
class User extends BaseUser
{
protected $table = 'custom_users';
protected $primaryKey = 'user_id';
}
UserRepository binding in UserServiceProvider to point to your custom repository.Testing
BenGor\User\Tests\TestCase as a base for unit/feature tests.UserService for isolated testing:
$mockService = Mockery::mock(UserService::class);
$mockService->shouldReceive('register')->andReturn($user);
API Usage
Passport or Sanctum for API authentication:
$userService->login($email, $password);
$token = $user->createToken('API Token')->accessToken;
Token Expiry
bengor-user.php).purgeExpiredTokens() periodically (e.g., via a scheduled task) to avoid token bloat.Password Rules
bengor-user.php:
'password' => [
'min_length' => 10,
'require_uppercase' => true,
],
Email Uniqueness
unique:users,email).users table.Role Hierarchy
admin inheriting editor permissions).Role model.Session Management
user_id or remember_token in the session after login.Database Migrations
make:migration:
php artisan make:migration create_users_table
id, email, password, remember_token, confirmed_at, created_at, updated_at.Token Issues
$token = $userService->generateConfirmationToken($user);
// Verify token exists in database via `tokens` table.
$token = Token::where('token', $request->token)->first();
if ($token->expires_at < now()) {
throw new \Exception('Token expired');
}
Password Hashing
APP_KEY is set in .env (required for password hashing).bengor-user.php:
'hash_algorithm' => 'bcrypt', // or 'argon2id'
Role Assignment
$user->roles; // Collection of assigned roles
$userService->getRoles($user); // Alternative method
Event Listeners
EventServiceProvider.handle(UserRegistered $event)).Custom Validation
User model to add custom validation rules:
public function rules()
{
return array_merge(parent::rules(), [
'custom_field' => 'required|unique:users',
]);
}
Custom Repositories
UserRepository, RoleRepository) to override default behavior:
class CustomUserRepository implements UserRepository
{
public function findByEmail($email)
{
return User::where('email', $email)->where('active', 1)->first();
}
}
UserServiceProvider:
$this->app->bind('BenGor\User\Repositories\UserRepository', function ($app) {
return new CustomUserRepository();
});
Custom Tokens
Token model to add custom token types:
class CustomToken extends Token
{
protected $type = 'custom';
}
TokenRepository to handle the new type.Custom Events
How can I help you explore Laravel packages today?