byjg/authuser
A simple and customizable library for user authentication in PHP applications using a clean repository and service layer architecture.
Maintainers
Fund package maintenance!
Requires
- php: >=8.3 <8.6
- byjg/cache-engine: ^6.0
- byjg/jwt-wrapper: ^6.0
- byjg/micro-orm: ^6.0
Requires (Dev)
- phpunit/phpunit: ^10.5|^11.5
- vimeo/psalm: ^5.9|^6.13
Suggests
None
Provides
None
Conflicts
None
Replaces
None
MIT 1c48d682bcadea52e4759d070e700d62a0520917
README
| sidebar_key | authuser | ||
|---|---|---|---|
| tags |
|
User Authentication
A simple and customizable library for user authentication in PHP applications using a clean repository and service layer architecture.
👁 Sponsor
👁 Build Status
👁 Opensource ByJG
👁 GitHub source
👁 GitHub license
👁 GitHub release
The main purpose is to handle all complexity of user validation, authentication, properties management, and access tokens, abstracting the database layer. This class can persist user data into session (or file, memcache, etc.) between requests.
Documentation
- Getting Started
- Installation
- User Management
- Authentication
- Session Context
- User Properties
- Database Storage
- Password Validation
- JWT Tokens
- Custom Fields
- Mappers
- Examples
Quick Start
Installation
composer require byjg/authuser
See Installation Guide for detailed setup instructions and requirements.
Basic Usage
<?php use ByJG\AnyDataset\Db\DatabaseExecutor; use ByJG\AnyDataset\Db\Factory as DbFactory; use ByJG\Authenticate\Enum\LoginField; use ByJG\Authenticate\Model\UserModel; use ByJG\Authenticate\Model\UserPropertiesModel; use ByJG\Authenticate\Repository\UsersRepository; use ByJG\Authenticate\Repository\UserPropertiesRepository; use ByJG\Authenticate\Service\UsersService; use ByJG\Authenticate\SessionContext; use ByJG\Cache\Factory; // Initialize repositories and service $dbDriver = DbFactory::getDbInstance('mysql://user:pass@host/db'); $db = DatabaseExecutor::using($dbDriver); $usersRepo = new UsersRepository($db, UserModel::class); $propsRepo = new UserPropertiesRepository($db, UserPropertiesModel::class); $users = new UsersService($usersRepo, $propsRepo, LoginField::Username); // Create and authenticate a user $user = $users->addUser('John Doe', 'johndoe', 'john@example.com', 'SecurePass123'); $authenticatedUser = $users->isValidUser('johndoe', 'SecurePass123'); if ($authenticatedUser !== null) { $sessionContext = new SessionContext(Factory::createSessionPool()); $sessionContext->registerLogin($authenticatedUser->getUserid()); echo "Welcome, " . $authenticatedUser->getName(); }
Set the third constructor argument to LoginField::Email if you prefer authenticating users by email instead of username.
See Getting Started for a complete introduction and Examples for more use cases.
Features
- User Management - Complete CRUD operations. See User Management
- Authentication - Username/email + password or JWT tokens. See Authentication and JWT Tokens
- Session Management - PSR-6 compatible cache storage. See Session Context
- User Properties - Store custom key-value metadata. See User Properties
- Password Validation - Built-in strength requirements. See Password Validation
- Database Storage - Supports MySQL, PostgreSQL, SQLite, and more. See Database Storage
- Custom Schema - Map to existing database tables. See Database Storage
- Field Mappers - Transform data during read/write. See Mappers
- Extensible Model - Add custom fields easily. See Custom Fields
Running Tests
Because this project uses PHP Session you need to run the unit test the following manner:
./vendor/bin/phpunit --stderr
Architecture
┌───────────────────┐
│ SessionContext │
└───────────────────┘
│
│
┌───────────────────┐
│ UsersService │ (Business Logic)
└───────────────────┘
│
┌────────────────────┴────────────────────┐
│ │
┌───────────────────┐ ┌──────────────────────┐
│ UsersRepository │ │ PropertiesRepository │
└───────────────────┘ └──────────────────────┘
│ │
┌───────┴───────┐ ┌──────────┴──────────┐
│ │ │ │
┌───────────────┐ ┌────────┐ ┌───────────────┐ ┌──────────────┐
│ UserModel │ │ Mapper │ │ PropsModel │ │ Mapper │
└───────────────┘ └────────┘ └───────────────┘ └──────────────┘
License
This project is licensed under the MIT License - see the LICENSE file for details.
Dependencies
flowchart TD byjg/authuser --> byjg/micro-orm byjg/authuser --> byjg/cache-engine byjg/authuser --> byjg/jwt-wrapperLoading
