osmo/auth

PHP 8 的身份验证。安全且简单。

v1.0.0 2023-03-08 08:10 UTC

This package is not auto-updated.

Last update: 2024-10-03 00:58:28 UTC


README

PHP 8 的身份验证。安全且简单。

Total Downloads Latest Stable Version License

需求

  • PHP 8.0+ (php 8)
  • PDO (PHP 数据对象) 扩展 (pdo)
  • MySQL 5.6+ MariaDB 5.5.23+ PostgreSQL 9.5.10+

安装

  1. 通过 Composer 包含库

    $ composer require osmo/auth
    
  2. 包含 Composer 自动加载器

    require __DIR__ . '/vendor/autoload.php';
  3. 设置数据库并创建所需的表

使用方法

连接到数据库

所需的三个数据是数据库名、用户名和密码。如果您的表不是名为 users,请将其指定为数组或使用 setTable 方法。

$con = new Osmo\Database([
    'username' => 'root',
    'database' => 'system',
    'password' => 'password'
  //'table'    => 'users_system' 
]);
//Default value {users}
//$con->setTable('users_table');

创建并配置一个新实例

要配置实例,您必须传递两个参数,$connection 和一个包含要验证的数据库两个字段的 array[]

$auth = new Osmo\Auth($con, ['email', 'password']);

配置登录

如果数据不正确,make() 函数将返回一个重定向到执行了 $_POST 方法的同一路径。

if ($auth->isPost()) {
    $auth->make($auth->inputEmailAddress(), $auth->input('password'), 'md5');
}

验证密码的方法

  • md5
  • sha1
  • crypt
  • password_verify (默认验证)
$auth->make($auth->input('email'), $auth->input('password'), 'crypt');

要使用 password_verify() 方法,请留第三个参数为空。

$auth->make($auth->input('email'), $auth->input('password'));

登录成功后的重定向

要重定向用户,您需要传递回调函数

$auth->make($auth->inputEmailAddress(), $auth->input('password'), 'md5', function (){
    Osmo\Response::redirect('/');
});

验证用户登录

您可以使用以下函数检查用户是否已登录

if(Osmo\SessionManager::auth()) {
    //
}

清理用户会话

要清理会话,您需要运行以下函数

Osmo\SessionManager::destroy();