2

thinkphp 用户认证 Auth

 2 years ago
source link: https://segmentfault.com/a/1190000041213063
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
composer require whereof/think-auth 1.0-alpha

基础user表

CREATE TABLE `users` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `remember_token` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `users_email_unique` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

考虑已有的数据库字段密码字段就不是 password那么解决方案来了

\whereof\think\auth\Model\Field::$password='pwd';
<?php
return [
    'default' => 'web',
    'guards'  => [
        'web' => [
            'driver'   => 'session',
            'provider' => [
                'driver' => 'database',
                'table'  => 'users',
            ],
        ],
        'api' => [
            'driver'   => 'session',
            'provider' => [
                'driver' => 'model',
                'model'  => \whereof\think\auth\User::class,
            ],
        ],
    ],
];

在provider中使用model,需要做一下操作

  1. 创建User模型
  2. 模型继承 whereof\think\auth\User

使其他设备上的 session 失效

你应该确保 /app/middleware.php 的全局中间件组中存在 \think\middleware\SessionInit::class 中间件,并且没有被注释掉:

<?php
// 全局中间件定义文件
return [
    // Session初始化
     \think\middleware\SessionInit::class
];

密码始终验证不通过

在默认情况下采用的是password_verify函数进行校验,所以您在加密的时候,需要使用函数password_hash进行加密

password_hash($password, PASSWORD_BCRYPT)

自定义密码验证

在很多系统上的密码加密方式是md5 或者双md5,那么久需要用到自定义密码验证

  1. 自定义md5的方式
<?php
namespace app;
use whereof\think\auth\Contracts\Authenticatable;
use whereof\think\auth\Contracts\Password;
/**
 * Class Md5
 * @author zhiqiang
 * @package app
 */
class Md5 implements Password
{
    /**
     * @param Authenticatable $user
     * @param string $password
     * @return bool
     */
    public function passwordVerify(Authenticatable $user, string $password)
    {
        return md5($password) == $user->getAuthPassword();
    }
}
  1. 修改配置文件
<?php
return [
        .............
    // 自定义密码验证
    'password' => \app\Md5Password::class,
        .............
];
//如果你愿意,除了用户的电子邮件和密码之外,还可以向身份验证查询中添加额外的查询条件。为了实现这一点,我们可以简单地将查询条件添加到传递给 attempt 方法的数组中。
Auth::attempt(['email' => '[email protected]', 'password' => '123456'], true);

//您可以将布尔值作为第二个参数传递给 login 方法。此值指示是否需要验证会话的 「记住我」 功能。请记住,这意味着会话将被无限期地验证,或者直到用户手动注销应用程序:
Auth::login(User::find(1), $remember = false);

//只验证一次
Auth::once(['email' => '[email protected]', 'password' => '123456']);
//只验证一次通过id
Auth::onceUsingId(1);

获取已认证的用户信息

use whereof\think\auth\Facades\Auth;

// 获取当前的认证用户信息 ...
$user = Auth::user();
// 获取当前的认证用户id ...
$id = Auth::id();

确定当前用户是否经过身份验证

use whereof\think\auth\Facades\Auth;

if (Auth::check()) {
    // 用户已登录...
}
use whereof\think\auth\Facades\Auth;

if (Auth::attempt(['email' => $email, 'password' => $password], $remember)) {
    // 正在为该用户执行记住我操作 ...
}

使用户退出登录(清除会话)

Auth::logout();

访问特定的看守器实例

Auth::guard('api')->attempt($credentials);
Auth::guard('api')->login(User::find(1), $remember = false);

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK