PHP code example of deisss / slim-auth

1. Go to this page and download the library: Download deisss/slim-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/ */

    

deisss / slim-auth example snippets



namespace Slim\Extras\Middleware;
use Slim\Extras\Middleware\AbstractHTTPBasicAuth as AbstractHTTPBasicAuth;

/**
 * Our concrete authentification implementation
*/
class HTTPBasicAuth extends AbstractHTTPBasicAuth {
    /**
     * Constructor
     *
     * @param array $skipUrl Any revelant path to skip authentification check
    */
    public function __construct($skipUrl = null) {
        $this->setSkip($skipUrl);
    }

    /**
     * The function to handle the database/session/facebook check
     *
     * @param string $login The login supply by user
     * @param string $password The password supply by user
     * @param string $path The url user try to access
     * @return Any value 'null' for php empty function will be consider
     *         as a fail (and yet be refused), any non-empty value
     *         will be accepted
    */
    protected function getUserFromLogin($login, $password, $path) {
        // Your database/session check here

        // Any non-empty/false value will be consider as 'ok', we
        // recommand to send back full user object (as you can recover it later into route function - see below)
        return true;
    }
}


re 'HTTPBasicAuth.php';

$app = new \Slim\Slim();
$app->add(new \Slim\Extras\Middleware\HTTPBasicAuth(array(
    '/hello/:name'
)));
$app->get('/hello/:name', function ($name) use ($app) {
    echo 'Hello '.$name;
});
$app->get('/logged', function() use ($app) {
    $userFromAuth = $app->request()->headers('auth');
    // Same
    $userFromAuth = $app->request()->headers('user');
});

$app->run();


function isAdministrator() {
    $app = \Slim\Slim::getInstance();

    // userFromAuth is now a $_SESSION array instead of boolean value
    $userFromAuth = $app->request()->headers('auth');

    // We test, and refuse if the role is not OK
    if($userFromAuth['role'] != 'administrator') {
        $app->status(403);
        $app->stop();
    }
}

$app->get('/this-is-acl', 'isAdministrator', function() {
});
HTTPBasicAuth.php