PHP code example of devster / uauth

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

    

devster / uauth example snippets




use Uauth\Basic;



// Here is the most simple usecase
$basic = new \Uauth\Basic("Secured Area", ['john' => 'd0e!', 'jon' => '5n0w']);
$basic->auth();
// All code below is secured by HTTP basic, and you can access the user
echo "Welcome ", $basic->getUser();

$basic = new \Uauth\Basic("Secured Area");
$basic
    // Implement your own user verification system.
    // The callable must return true if user is allowed, false if not
    ->verify(function ($username, $password) use ($db) {
        $user = $db->findUser($username);
        return $user->password == $password;
    })
    // this code is executed if the login modal is cancelled
    // or if the user is not verified
    ->deny(function () {
        echo "This text appears because you hit the cancel button";
    })
;
$basic->auth();

echo "Welcome ", $basic->getUser(), ", you password is ", $basic->getPassword();

$app = new Silex\Application();

$app['allowedUsers'] = ['jon' => '5n0w'];

// Create your silex middleware
$httpBasicAuth = function () use ($app) {
    $basic = new \Uauth\Basic("Secured Silex Area", $app['allowedUsers']);
    $basic->auth();

    // we save the current user
    $app['user'] = $basic->getUser();
};

// And now you can use your middleware where you see fit

// Protect the entire app
$app->before($httpBasicAuth);

// Or protect a collection of routes
$blog = $app['controllers_factory'];
$blog->before($httpBasicAuth);
$blog->get('/', function () {
    return 'Blog home page';
});
$app->mount('/private_blog', $blog);

// Or protect a single route
$app->get('/hello', function () use ($app) {
    return 'Hello '.$app->escape($app['user']);
})->before($httpBasicAuth);

$app->run();