https://github.com/dmitrymomot/php-auth
Simply authentication library
https://github.com/dmitrymomot/php-auth
Last synced: 3 months ago
JSON representation
Simply authentication library
- Host: GitHub
- URL: https://github.com/dmitrymomot/php-auth
- Owner: dmitrymomot
- License: mit
- Created: 2014-04-22T14:10:19.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2014-04-27T20:13:40.000Z (about 11 years ago)
- Last Synced: 2025-01-11T18:30:12.281Z (4 months ago)
- Language: PHP
- Size: 219 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
php-auth
========Simply authentication library
Key points:
- Supports drivers ('file' and 'database' adapters already included in the package).
- Easy to use
- Minimum configuration## Installation
This package is available via Composer:
```json
{
"require": {
"dmitrymomot/php-auth": "1.*"
}
}
```## Example of usage
### Usage adapter [File](https://github.com/dmitrymomot/php-auth/blob/master/src/Adapter/File.php)
```php
$auth = new \Auth\Adapter\File;
$auth->setUsers(array('test_user' => array('password' => 'hashed_password', 'role' => 'user')));
```Log in
```php
$auth->login('test_user', 'password'); // returns boolean value
```Get user
```php
$auth->getUser('guest') // returns current username or 'guest', if user isn't logged in
```Get user role
```php
$auth->getRole() // returns string
```Check log in
```php
$auth->loggedIn() // returned true
```Check log in as
```php
$auth->loggedIn('admin') // returned false
$auth->loggedIn('user') // returned true
```Log out
```php
$auth->logout(); // returns boolean value
```Log in as another user
```php
$auth->loginAs('username'); // returns boolean value
```Come back to initial user
```php
$auth->comeBack(); // returns boolean value
```### Usage adapter [Database](https://github.com/dmitrymomot/php-auth/blob/master/src/Adapter/Database.php)
*(supports all the same that 'file' adapter)*In composer.json add package php-activerecord/php-activerecord
```json
"require": {
"dmitrymomot/php-session": "1.*",
"php-activerecord/php-activerecord":"dev-master"
},
```
and update composer.Set database config (read more in [php-activerecord docs](http://www.phpactiverecord.org/projects/main/wiki))
```php
$cfg = \ActiveRecord\Config::instance();
$cfg->set_connections(array(
'development' => 'mysql://username_for_dev:password@localhost/username_for_dev',
'production' => 'mysql://username:password@localhost/database_name'
));
```Initialization
```php
$auth = new \Auth\Adapter\Database();
```Initialization with custom model User
```php
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
```php
$auth->getUser('guest') // returns instance of class \Auth\Model\User or 'guest', if user isn't logged in
```Create new user
```php
$auth->createUser(array('username' => 'test_user', 'password' => 'some_password', 'email' => '[email protected]', ...)); // returns boolean value or array error messages
```Update current user
```php
$auth->updateUser(array('username' => 'test_user', 'password' => 'some_password', ....)); // returns boolean value or array error messages
```### Helpers
```php
echo \Auth\Auth::hash('admin'); // returns hashed string 'admin'
```
Also you can set hash key
```php
\Auth\Auth::hashKey = 'vv34r3v4c34r';
```## License
The MIT License (MIT). Please see [License File](https://github.com/dmitrymomot/php-auth/blob/master/LICENSE) for more information.