PHP Auth




Simply authentication library

Key points:

  • Supports drivers ('file' and 'database' adapters already included in the package).
  • Easy to use
  • Minimum configuration


This package is available via Composer:

    "require": {
        "dmitrymomot/php-auth": "1.*"

Example of usage

Usage adapter File

$auth = new \Auth\Adapter\File;
$auth->setUsers(array('test_user' => array('password' => 'hashed_password', 'role' => 'user')));

Log in

$auth->login('test_user', 'password'); // returns boolean value

Get user

$auth->getUser('guest') // returns current username or 'guest', if user isn't logged in

Get user role

$auth->getRole() // returns string

Check log in

$auth->loggedIn() // returned true

Check log in as

$auth->loggedIn('admin') // returned false
$auth->loggedIn('user') // returned true

Log out

$auth->logout(); // returns boolean value

Log in as another user

$auth->loginAs('username'); // returns boolean value

Come back to initial user

$auth->comeBack(); // returns boolean value

Usage adapter Database

(supports all the same that 'file' adapter)

In composer.json add package php-activerecord/php-activerecord

    "require": {
		"dmitrymomot/php-session": "1.*",

and update composer.

Set database config (read more in php-activerecord docs)

$cfg = \ActiveRecord\Config::instance();
	'development' => 'mysql://username_for_dev:password@localhost/username_for_dev',
	'production' => 'mysql://username:password@localhost/database_name'


$auth = new \Auth\Adapter\Database();

Initialization with custom model User

class CustomUser implements \Auth\Model\UserInterface {
	//....realisation of interface

$model = '\Custom\Model\CustomUser'; // full path to class
$auth = new \Auth\Adapter\Database($model);

Get user

$auth->getUser('guest') // returns instance of class \Auth\Model\User or 'guest', if user isn't logged in

Create new user

$auth->createUser(array('username' => 'test_user', 'password' => 'some_password', 'email' => '', ...)); // returns boolean value or array error messages

Update current user

$auth->updateUser(array('username' => 'test_user', 'password' => 'some_password', ....)); // returns boolean value or array error messages


echo \Auth\Auth::hash('admin'); // returns hashed string 'admin'

Also you can set hash key

\Auth\Auth::hashKey = 'vv34r3v4c34r';


The MIT License (MIT). Please see License File for more information.