Skip to main content

User Authentication

Users, passwords, tokens and per-user properties behind one service, on any database, without adopting a framework.

Sponsor Build Status Opensource ByJG Install MCP Server 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.

Why​

Authentication is the same in every application: store users and hashed passwords, validate a login, issue a token, keep a few extra attributes per user. Frameworks bundle it with everything else; doing it by hand means getting password hashing and tokens right yourself.

AuthUser is that layer on its own, built on a repository and a service:

$users = new UsersService($usersRepo, $propsRepo, LoginField::Username);

$user = $users->addUser('John Doe', 'johndoe', '[email protected]', 'SecurePass123');
$users->addProperty($user->getUserid(), 'department', 'Engineering');
$engineers = $users->getUsersByProperty('department', 'Engineering');
  • Properties without schema changes. Any number of key/value attributes per user, and queries by them, without adding columns.
  • Fits your schema. Mappers and custom fields adapt it to an existing users table.
  • Sessions or JWT. The authenticated user is kept in a session context, or issued as a JWT token.

Documentation​

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', '[email protected]', '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​

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.

Running Tests​

Because this project uses PHP Session you need to run the unit test the following manner:

./vendor/bin/phpunit --stderr

Open source ByJG