Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/byjg/php-authuser
A simple and customizable class for enable user authentication inside your application. It is available on XML files, Relational Databases and Moodle.
https://github.com/byjg/php-authuser
authentication authentication-flow authentication-strategy php-sessions user-storage
Last synced: about 1 month ago
JSON representation
A simple and customizable class for enable user authentication inside your application. It is available on XML files, Relational Databases and Moodle.
- Host: GitHub
- URL: https://github.com/byjg/php-authuser
- Owner: byjg
- License: mit
- Created: 2015-08-03T01:03:27.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2024-01-05T20:36:53.000Z (11 months ago)
- Last Synced: 2024-05-02T05:17:19.517Z (7 months ago)
- Topics: authentication, authentication-flow, authentication-strategy, php-sessions, user-storage
- Language: PHP
- Homepage:
- Size: 262 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Auth User PHP
[![Build Status](https://github.com/byjg/php-authuser/actions/workflows/phpunit.yml/badge.svg?branch=master)](https://github.com/byjg/php-authuser/actions/workflows/phpunit.yml)
[![Opensource ByJG](https://img.shields.io/badge/opensource-byjg-success.svg)](http://opensource.byjg.com)
[![GitHub source](https://img.shields.io/badge/Github-source-informational?logo=github)](https://github.com/byjg/php-authuser/)
[![GitHub license](https://img.shields.io/github/license/byjg/php-authuser.svg)](https://opensource.byjg.com/opensource/licensing.html)
[![GitHub release](https://img.shields.io/github/release/byjg/php-authuser.svg)](https://github.com/byjg/php-authuser/releases/)A simple and customizable class for enable user authentication inside your application. It is available on XML files, Relational Databases and Moodle.
The main purpose is just to handle all complexity of validate a user, add properties and create access token abstracting the database layer.
This class can persist into session (or file, memcache, etc) the user data between requests.## Creating a Users handling class
Using the FileSystem (XML) as the user storage:
```php
isValidUser('someuser', '12345');
if (!is_null($user))
{
$userId = $user->getUserid();$sessionContext = new \ByJG\Authenticate\SessionContext(\ByJG\Cache\Factory::createSessionPool());
$sessionContext->registerLogin($userId);
}
```## Check if user was previously authenticated
```php
isAuthenticated()) {// Get the userId of the authenticated users
$userId = $sessionContext->userInfo();// Get the user and your name
$user = $users->getById($userId);
echo "Hello: " . $user->getName();
}
```## Saving extra info into the user session
You can save data in the session data exists only during the user is logged in. Once the user logged off the
data stored with the user session will be released.Store the data for the current user session:
```php
setSessionData('key', 'value');
```Getting the data from the current user session:
```php
getSessionData('key');
```Note: If the user is not logged an error will be throw
## Adding a custom property to the users
```php
getById($userId);
$user->setField('somefield', 'somevalue');
$users->save();
```## Logout from a session
```php
registerLogout();
```## Important note about SessionContext
`SessionContext` object will store the info about the current context.
As SessionContext uses CachePool interface defined in PSR-6 you can set any storage
to save your session context.In our examples we are using a regular PHP Session for store the user context
(`Factory::createSessionPool()`). But if you are using another store like MemCached
you have to define a UNIQUE prefix for that session. Note if TWO users have the same
prefix you probably have an unexpected result for the SessionContext.Example for memcached:
```php
'fieldname of userid',
UserDefinition::FIELD_NAME => 'fieldname of name',
UserDefinition::FIELD_EMAIL => 'fieldname of email',
UserDefinition::FIELD_USERNAME => 'fieldname of username',
UserDefinition::FIELD_PASSWORD => 'fieldname of password',
UserDefinition::FIELD_CREATED => 'fieldname of created',
UserDefinition::FIELD_ADMIN => 'fieldname of admin'
]
);
```### Adding custom modifiers for read and update
```php
the current value to be updated
// $instance -> The array with all other fields;
$userDefinition->defineClosureForUpdate(UserDefinition::FIELD_PASSWORD, function ($value, $instance) {
return strtoupper(sha1($value));
});// Defines a custom function to be applied After the field UserDefinition::FIELD_CREATED is read but before
// the user get the result
// $value --> the current value retrieved from database
// $instance -> The array with all other fields;
$userDefinition->defineClosureForSelect(UserDefinition::FIELD_CREATED, function ($value, $instance) {
return date('Y', $value);
});// If you want make the field READONLY just do it:
$userDefinition->markPropertyAsReadOnly(UserDefinition::FIELD_CREATED);
```## Extending UserModel
It is possible extending the UserModel table, since you create a new class extending from UserModel to add the new fields.
For example, imagine your table has one field called "otherfield".
You'll have to extend like this:
```php
setOtherfield($field);
}public function getOtherfield()
{
return $this->otherfield;
}public function setOtherfield($otherfield)
{
$this->otherfield = $otherfield;
}
}
```After that you can use your new definition:
```php
byjg/micro-orm
byjg/authuser --> byjg/cache-engine
byjg/authuser --> byjg/jwt-wrapper
```----
[Open source ByJG](http://opensource.byjg.com)