hkt/expressive-auth

使用 Aura.Auth 实现的 Zend Expressive 身份验证

dev-master 2016-08-02 06:14 UTC

This package is auto-updated.

Last update: 2024-09-07 20:42:24 UTC


README

目前仅适用于 Zend Expressive。如果其他 PSR-7 基础的库/框架集成两个(根据我的意见下一个 PSR 的)TemplateRendererInterfaceRouterInterface,此库将对所有人适用。

[实验版本]。

一些 @todo

  • 我们是否需要使用某种事件处理系统?想法是在用户从系统中登录/登出时触发事件。
  • 如何优雅地处理错误信息。目前只有一个 error 变量。
  • 一旦事情确定,编写一些测试。
composer require htk/expressive-auth

配置

目前仅支持 Aura.Di。您可能需要为其他依赖注入容器添加/配置。

// In /config/container.php

class_alias('Aura\Di\ContainerConfig', 'Aura\Di\Config');

$configClasses = [    
    // Aura.Auth Configuration    
    Aura\Auth\_Config\Common::class,        
    Hkt\ExpressiveAuth\Di\AuthConfig::class,

    // Modify Route accordingly

    Hkt\ExpressiveAuth\Di\RouteConfig::class,

    // more config classes ...
];

在您的 App\Di\Config 中,您可能想添加您打算使用的适配器。请参阅 Aura.Auth

以下示例为 Aura\Auth\Adapter\PdoAdapter

// In your App\Di\Config define()

$di->set('aura/auth:adapter', $di->lazyNew('Aura\Auth\Adapter\PdoAdapter'));
$di->params['Aura\Auth\Adapter\PdoAdapter']['pdo'] = $di->lazyGet('Aura\Sql\ExtendedPdo');
$di->params['Aura\Auth\Verifier\PasswordVerifier'] = array(
    'algo' => PASSWORD_DEFAULT,
);

路由

当前的登录和登出路由分别是 /login/logout。您可以根据需要修改。

登录后重定向到

一旦用户登录,它将重定向到名为 hkt/expressive-auth:home 的路由。您可以通过 Di/Config 注册路由或进行修改。它看起来像这样

// After logged in redirect user to route named `home`
$di->params['Hkt\ExpressiveAuth\Action\LoginAction']['redirectTo'] = 'home';

// After logged in redirect user to route named `login`
$di->params['Hkt\ExpressiveAuth\Action\LogoutAction']['redirectTo'] = 'login';

模板

用户和项目对模板的喜爱各不相同。您可以选择任何支持 zend-expressive-template 的东西。

在您的 expressive /config/autoload/templates.global.php 文件中,您可以设置模板的路径

    'templates' => [        
        'paths' => [
            // ....
            'hkt/expressive-auth'   => ['templates/auth'],
        ]
    ]

为了简单起见,这里有一个 /templates/auth/login.phtml 模板。

<form method="post" enctype="multipart/form-data" action="<?= $this->url('hkt/expressive-auth:login'); ?>">
  <div class="form-group">
    <label for="username">Username</label>
    <input type="text" class="form-control" id="username" name="username" placeholder="Username">
  </div>
  <div class="form-group">
    <label for="password">Password</label>
    <input type="password" class="form-control" id="password" name="password" placeholder="Password">
  </div>
  <button type="submit" class="btn btn-default">Login</button>
</form>