PHPackages                             tp5er/think-auth - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. tp5er/think-auth

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

tp5er/think-auth
================

thinkphp 用户认证 Auth

1.0.0(1y ago)82.0k1MITPHPPHP &gt;=7.2.5

Since May 11Pushed 1y agoCompare

[ Source](https://github.com/pkg6/think-auth)[ Packagist](https://packagist.org/packages/tp5er/think-auth)[ Docs](https://www.zhiqiang.wang/)[ RSS](/packages/tp5er-think-auth/feed)WikiDiscussions develop Synced 1w ago

READMEChangelog (10)Dependencies (9)Versions (15)Used By (1)

[![Latest Stable Version](https://camo.githubusercontent.com/6ae9dde4837dbe3907f2bbae83b6854ffc2ebbda5248ad381da9f81ff58b0403/687474703a2f2f706f7365722e707567782e6f72672f74703565722f7468696e6b2d617574682f76)](https://packagist.org/packages/tp5er/think-auth) [![Total Downloads](https://camo.githubusercontent.com/af3e2ed97187a3b6fa38107871b645696e7781fc3d85998afa7aea6981bc8261/687474703a2f2f706f7365722e707567782e6f72672f74703565722f7468696e6b2d617574682f646f776e6c6f616473)](https://packagist.org/packages/tp5er/think-auth) [![Latest Unstable Version](https://camo.githubusercontent.com/8df03813c045343eb032948b03986fe41a4324ee062fff8b64e24b469bb72e9c/687474703a2f2f706f7365722e707567782e6f72672f74703565722f7468696e6b2d617574682f762f756e737461626c65)](https://packagist.org/packages/tp5er/think-auth) [![License](https://camo.githubusercontent.com/fabb142ddc3d2151d2c7f1b52ece335e9d07b2bd3ba1cbeb3a09ed94e2d3262f/687474703a2f2f706f7365722e707567782e6f72672f74703565722f7468696e6b2d617574682f6c6963656e7365)](https://packagist.org/packages/tp5er/think-auth) [![PHP Version Require](https://camo.githubusercontent.com/bb32c7f9de731d8d794c6cad56217a2c47040ec774c1ba989103278152e4f968/687474703a2f2f706f7365722e707567782e6f72672f74703565722f7468696e6b2d617574682f726571756972652f706870)](https://packagist.org/packages/tp5er/think-auth)

介绍
--

[](#介绍)

许多web应用程序为用户提供了一种通过应用程序进行身份验证和“登录”的方式。在web应用程序中实现此功能可能是一项复杂且潜在风险的工作。因此，think-auth努力为您提供快速、安全、轻松地实现身份验证所需的工具。 其核心是，think-auth的认证设施由“卫士”和“提供者”组成。防护程序定义了如何对每个请求的用户进行身份验证。例如，think-auth附带了一个会话保护程序，该程序使用会话存储和cookie来维护状态。 提供程序定义如何从持久存储中检索用户。think-auth提供了使用Eloquent和数据库查询生成器检索用户的支持。但是，您可以根据应用程序的需要自由定义其他提供程序。 您的应用程序的身份验证配置文件位于config/auth.php中。该文件包含几个详细记录的选项，用于调整think-auth的身份验证服务的行为。

安装
--

[](#安装)

```
composer require tp5er/think-auth

```

版本更新记录
------

[](#版本更新记录)

[CHANGELOG.md](https://github.com/pkg6/think-auth/blob/main/CHANGELOG.md)

命令行
---

[](#命令行)

```
//生成基础数据结构
php think auth:install
// 默认会生成 personal_access_token & user表结构

//生成测试数据
php think seed:run

//具体参考 `topthink/think-migration`

// 使用policy类
php think make:policy Post

```

Auth常用方法
--------

[](#auth常用方法)

```
//如果你愿意，除了用户的电子邮件和密码之外，还可以向身份验证查询中添加额外的查询条件。为了实现这一点，我们可以简单地将查询条件添加到传递给 attempt 方法的数组中。
Auth::attempt(['email' => 'zhiqiang2033@gmail.com', 'password' => '123456'], true);

//访问指定的看守器实例
if (Auth::guard('admin')->attempt($credentials)) {
    //
}

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

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

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

if (Auth::check()) {
    // 用户已登录...
}
//使用户退出登录（清除会话）
Auth::logout();

//添加自定义的看守器
Auth::extend("test",function (App $app, $name, $config){
    //返回实现Guard|StatefulGuard的对象

});
//添加自定义用户提供器
Auth::provider("test",function (App $app,$config){
    //返回实现UserProvider的对象
});

//动态设置配置信息
Auth::setConfigGuardProvider("admin","user_table","session");
Auth::configMergeGuards('sanctum', ["driver" => 'sanctum',"provider" => null])
Auth::configMergeProviders("admin", ['driver' => 'database','table' => "user"]);

```

使用policy
--------

[](#使用policy)

生成Post模型

```
php think make:model Post

```

#### 生成一个PostPolicy

[](#生成一个postpolicy)

```
php think make:policy Post

```

#### 加入配置`config/auth.php`

[](#加入配置configauthphp)

```
"policies" => [
    //'app\model\Model' => 'app\policies\ModelPolicy',
    \app\model\Post::class => \app\policies\Post::class,
],

```

使用
--

[](#使用)

```
use tp5er\think\auth\access\AuthorizesRequests

public function destroy(Post $post)
{
    $this->authorize('delete', $post);
    $post->delete();
    return redirect('/posts');
}

```

使用事件
----

[](#使用事件)

### 定义事件类LoginSuccessful

[](#定义事件类loginsuccessful)

```
