1. Go to this page and download the library: Download phpixie/auth library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
phpixie / auth example snippets
$slice = new \PHPixie\Slice();
// The database component is only ase($slice->arrayData(array(
'default' => array(
'driver' => 'pdo',
'connection' => 'sqlite::memory:'
)
)));
// the security component handles hashing, random numbers and tokens
$security = new \PHPixie\Security($database);
// This plugin allows using the login/password auth
$authLogin = new \PHPixie\AuthLogin($security);
// To use HTTP authorization we must first
// build an HTTP context
$http = new \PHPixie\HTTP();
$request = $http->request();
$context = $http->context($request);
$contextContainer = $http->contextContainer($context);
$authHttp = new \PHPixie\AuthHTTP($security, $contextContainer);
$authConfig = $slice->arrayData(array(
// config options
));
// This is your class that must impplement the
// \PHPixie\Auth\Repositories\Registry interface
$authRepositories = new AuthRepositories();
// Initialize the Auth system with both extensions
$auth = new \PHPixie\Auth($authConfig, $authRepositories, array(
$authLogin->providers(),
$authHttp->providers()
));
class AuthRepositories extends \PHPixie\Auth\Repositories\Registry\Builder
{
protected function buildUserRepository()
{
return new YourRepository();
}
}
// that is the second parameter we passed to Auth
$authRepositories = new AuthRepositories();
namespace Project\App\ORMWrappers\User;
// Repository wrapper
class Repository extends \PHPixie\AuthORM\Repositories\Type\Login
{
// You can supply multiple login fields,
// in this case its both usernam and email
protected function loginFields()
{
return array('username', 'email');
}
}
namespace Project\App\ORMWrappers\User;
// Entity wrapper
class Entity extends \PHPixie\AuthORM\Repositories\Type\Login\User
{
// get hashed password value
// from the field in the database
public function passwordHash()
{
return $this->passwordHash;
}
}
namespace Project\App;
class ORMWrappers extends \PHPixie\ORM\Wrappers\Implementation
{
protected $databaseEntities = array('user');
protected $databaseRepositories = array('user');
public function userEntity($entity)
{
return new ORMWrappers\User\Entity($entity);
}
public function userRepository($repository)
{
return new ORMWrappers\User\Repository($repository);
}
}
namespace Project\App;
class AuthRepositories extends \PHPixie\Auth\Repositories\Registry\Builder
{
protected $builder;
public function __construct($builder)
{
$this->builder = $builder;
}
protected function buildUserRepository()
{
$orm = $this->builder->components()->orm();
return $orm->repository('user');
}
}
namespace Project\App;
class Builder extends \PHPixie\DefaultBundle\Builder
{
protected function buildAuthRepositories()
{
return new AuthRepositories($this);
}
}
// /assets/auth.php
return array(
'domains' => array(
'default' => array(
// using the 'user' repository from the 'app' bundle
'repository' => 'app.user',
'providers' => array(
//
// when a cookie is used to login
// persist login using session too
'persistProviders' => array('session'),
// token storage
'tokens' => array(
'storage' => array(
'type' => 'database',
'table' => 'tokens',
'defaultLifetime' => 3600*24*14 // two weeks
)
)
),
// password login suport
'password' => array(
'type' => 'login.password',
// remember the user in session
// note that we did not add 'cookies' to this array
// because we don't want every login to be persistent
'persistProviders' => array('session')
)
)
)
);