Code Monkey home page Code Monkey logo

Comments (4)

silentworks avatar silentworks commented on May 21, 2024 1

If that is a middleware please don't get the request from the $container. It is better to do

$container["HttpBasicAuthentication"] = function ($request, $response) {
    $route = $request->getAttribute("route");
    $account = $route->getArgument("account");
    $pdo = new PDO("sqlite:/tmp/{$account}.sqlite");

    return new HttpBasicAuthentication([
        "authenticator" => new PdoAuthenticator([
            "pdo" => $pdo
        ])
    ]);
};

$app->add("HttpBasicAuthentication");

from slim-basic-auth.

tuupola avatar tuupola commented on May 21, 2024

I guess you could either create a custom authenticator or you could use container for setting up the middleware with something like (totally untested code):

use Tuupola\Middleware\HttpBasicAuthentication;
use Tuupola\Middleware\HttpBasicAuthentication\PdoAuthenticator;

$container = $app->getContainer();

$container["HttpBasicAuthentication"] = function ($container) {
    $request = $container["request"];
    $route = $request->getAttribute("route");
    $account = $route->getArgument("account");

    $pdo = new PDO("sqlite:/tmp/{$account}.sqlite");

    return new HttpBasicAuthentication([
        "authenticator" => new PdoAuthenticator([
            "pdo" => $pdo
        ])
    ]);
};

$app->add("HttpBasicAuthentication");

Here are some examples on how to use container for setting up the middleware.

from slim-basic-auth.

rduarte avatar rduarte commented on May 21, 2024

Hi,

I understood the code but I don't understand where it is placed. Can you help?

My slim-framework index.php:

if (PHP_SAPI == 'cli-server') {
    $url  = parse_url($_SERVER['REQUEST_URI']);
    $file = __DIR__ . $url['path'];
    if (is_file($file)) {
        return false;
    }
}
require __DIR__ . '/../vendor/autoload.php';
session_start();

use \Slim\Middleware\HttpBasicAuthentication\AuthenticatorInterface;
use \Slim\Middleware\HttpBasicAuthentication\PdoAuthenticator;

$pdo = new \PDO("mysql:host=localhost;dbname=brainsoft", "root"," root", array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''));

$settings = require __DIR__ . '/../src/settings.php';
$app = new \Slim\App($settings);
$app->add(new \Slim\Middleware\HttpBasicAuthentication([
    "path" => ["/chats"],
    "authenticator" => new PdoAuthenticator([
        "pdo" => $pdo
    ])
]));

require __DIR__ . '/../src/dependencies.php';
require __DIR__ . '/../src/middleware.php';
require __DIR__ . '/../src/routes.php';

$app->run();

Thanks for helping.

from slim-basic-auth.

tuupola avatar tuupola commented on May 21, 2024

Oh, @silentworks version is much better.

@rduarte You can put it in the place where you setup your middleware. Looking at the example code you pasted most likely src/middleware.php. Note that you should add the HttpBasicAuthentication middleware only once. Remove any old references to it.

from slim-basic-auth.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.