PHPackages                             jizhi/webman-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. jizhi/webman-auth

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

jizhi/webman-auth
=================

webman auth

1.0.1(2mo ago)080↓100%1MITPHPPHP &gt;=8.0

Since Feb 25Pushed 2mo agoCompare

[ Source](https://github.com/warmup413/webman-auth)[ Packagist](https://packagist.org/packages/jizhi/webman-auth)[ RSS](/packages/jizhi-webman-auth/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (3)Versions (2)Used By (1)

[!['Build Status'](https://camo.githubusercontent.com/83e28e26c835e012b5edef8cc9c1aba2916491fca72fe309fe38ab7b1a4e4bad/68747470733a2f2f7472617669732d63692e6f72672f6a697a68692f7765626d616e2d617574682e7376673f6272616e63683d6d61696e)](https://github.com/jizhi/webman-auth) [!['Latest Stable Version'](https://camo.githubusercontent.com/e82cf5331748082b1ee3583657b6b61cce473e9173c814d1540dbcc97da27f0e/68747470733a2f2f706f7365722e707567782e6f72672f6a697a68692f7765626d616e2d617574682f762f737461626c652e737667)](https://packagist.org/packages/jizhi/webman-auth) [!['Total Downloads'](https://camo.githubusercontent.com/80d8b0a82473a82c1672f103f57112d1987a993d7eafc4c104eab201fa4530cf/68747470733a2f2f706f7365722e707567782e6f72672f6a697a68692f7765626d616e2d617574682f642f746f74616c2e737667)](https://packagist.org/packages/jizhi/webman-auth) [!['License'](https://camo.githubusercontent.com/e5a16f54352481936d4b02ba3f0249c71d19091bce958f70c1061936a3220c1b/68747470733a2f2f706f7365722e707567782e6f72672f6a697a68692f7765626d616e2d617574682f6c6963656e73652e737667)](https://packagist.org/packages/jizhi/webman-auth)

安装
==

[](#安装)

```
composer require jizhi/webman-auth

```

配置文件
====

[](#配置文件)

```
//路径 config/plugin/jizhi/auth/app.php
// app_key 如果是laravel迁移过来的用户需与之前laravel的保持一致 如果是全新的 随意写个key即可
// jwt 配置项按自己需求配置即可 redis默认为false 如果要限制终端 则改为true
配置多用户

//初始化的示例 一定要改成自己实际的
'guard' => [
     'user' => [
         'key' => 'id', //主键
         'field' => ['id','name','email','mobile'], //设置允许写入扩展中的字段 一般为数据表存在的字段
         'num' => 0, //-1为不限制终端数量 0为只支持一个终端在线 大于0为同一账号同终端支持数量 建议设置为1 则同一账号同终端在线1个
         'model'=> app\model\Test::class
     ]
];
// 配置示例（根据自己真实情况 user一定要存在 因为默认就是user）
// field 是可以通过jwtKey解析出来的 请勿用敏感字段 以免信息泄露
'guard' => [
     'user' => [ // 普通用户
         'key' => 'id', //主键
         'field' => ['id','username','email','mobile','avatar'], //设置允许写入扩展中的字段 一般为数据表存在的字段
         'num' => 0, //-1为不限制终端数量 0为只支持一个终端在线 大于0为同一账号同终端支持数量 建议设置为1 则同一账号同终端在线1个
         'model'=> app\model\User::class //用户表模型
     ],
     'admin' => [ // 平台用户
         'key' => 'id', //主键
         'field' => ['id','name','avatar'], //设置允许写入扩展中的字段 一般为数据表存在的字段
         'num' => 0, //-1为不限制终端数量 0为只支持一个终端在线 大于0为同一账号同终端支持数量 建议设置为1 则同一账号同终端在线1个
         'model'=> app\model\Admin::class //管理员表模型
     ]
];

```

使用方法
====

[](#使用方法)

1. 生成JWT密钥(命令行)

```
php webman admin:auth

```

2. 加密密码

```
use WebmanAuth\facade\Auth;

//不可逆转 只能用password_verify来判断正确与否
$password = '123456';
Auth::bcrypt($password);
```

3.自动对字段进行验证且登入

```
use WebmanAuth\facade\Auth;

//验证字段一定得和设定得角色模型相匹配可以是任何字段组
// 这里自动进行了model查库操作 如果你的不支持 请用自定义登入
$tokenObject = Auth::attempt(['name'=> 'tycoonSong','password' => '123456']);

//返回对象$tokenObject 包含token_type,expires_in,refresh_expires_in,access_token,refresh_token

// 默认为user角色 当你是admin登入时
$tokenObject = Auth::guard('admin')->attempt(['name'=> 'tycoonSong','password' => '123456']);
```

4.自定义登入

```
use WebmanAuth\facade\Auth;
use app\model\User;
use app\model\Admin;
//返回对象$tokenObject 包含token_type,expires_in,refresh_expires_in,access_token,refresh_token

$user = User::first();
$tokenObject = Auth::login($user);//$user可以是对象 同样可以是数组

// 默认为user角色 当你是admin登入时
$admin = Admin::first();
$tokenObject = Auth::guard('admin')->login($admin);
```

5.获取当前登入用户信息

```
    use WebmanAuth\facade\Auth;
     $user = Auth::user(); //得到用户模型对象，查库数据，需查询动态数据时使用
     $user = Auth::user(true); // 得到扩展数据对象，非查库数据,比如只需得到用户ID或不常更新字段使用
     $admin = Auth::guard('admin')->user(); //当前登入管理员

```

6.退出登入

```
    use WebmanAuth\facade\Auth;
     $logout = Auth::logout(); //退出当前用户
     $logout = Auth::logout(true); // 退出所有当前用户终端
     $logout = Auth::guard('admin')->logout(); //管理员退出

```

7.刷新当前登入用户token

```
     use WebmanAuth\facade\Auth;
     $refresh = Auth::refresh();
     $refresh = Auth::guard('admin')->refresh(); //管理员刷新

```

8.单独设置过期时间

```
use WebmanAuth\facade\Auth;
use app\model\User;
$user = User::first();
Auth::accessTime(3600)->refreshTime(360000)->login($user);
Auth::accessTime(3600)->refreshTime(360000)->attempt(['name'=> 'tycoonSong','password' => '123456']);
Auth::accessTime(3600)->refresh();
```

9.获取报错信息 Auth::fail();

```
    //默认设定是不会报错的
    $user = Auth::user(); //当没有登入或异常时返回的null 用于用户可登入或可不登入场景里 只需要判断 $user == null 即可
    //而比如在会员中心调用时
    $user = Auth::fail()->user(); //走的是异常处理类https://www.workerman.net/doc/webman/exception.html

```

- 开启redis后,建议开启

```
    // 在使用过程中我们通常一个接口允许多端使用的情况 那么默认设置是不限制使用端口的
    // 可当你想允许比如web端同一账号只允许存在三个终端在线或同一账号APP只允许一个终端使用
    // 默认为web终端 传参client_type=web或你其它的终端client_type=ios
    //config/plugin/jizhi/auth/app.php设置
    'guard' => [
         'user' => [ // 普通用户
             'key' => 'id', //主键
             'field' => ['id','username','email','mobile','avatar'], //设置允许写入扩展中的字段 一般为数据表存在的字段
             'num' => 0, //-1为不限制终端数量 0为只支持一个终端在线 大于0为同一账号同终端支持数量 建议设置为1 则同一账号同终端在线1个
             'model'=> app\model\User::class //用户表模型
         ]
     ]
     'jwt' => [
         'redis' => false,
         ....
      ]

    Auth::logout(true); // 退出所有当前用户终端

```

- 获取所有redis用户及终端状态

```
    // 你可以使用redis hash对终端在线更好的管理 比如对某个用户进行下线处理，或查询用户的token有效期
    // 具体业务自行根据需求去实现 本系统未对这方面业务进行封装
    $guard = 'user';
    Redis::hGetAll('token_'.$guard);
    // 用户编号为1 的 token下线清除 ，可以批量
    Redis::hDel('token_'.$guard,[1]);

```

- 直接调用jwt

```
    use WebmanAuth\Facade\JWT as JwtFace;
    JwtFace::guard('user')->make($extend,$access_exp,$refresh_exp); //生成token 可为make($extend)
    JwtFace::guard('user')->refresh($accessTime = 0); //刷新令牌 可为refresh()
    JwtFace::guard('user')->verify($token); //$token可以不填则自动验证令牌 verify()
    JwtFace::guard('user')->getTokenExtend($token)//$token可以不填则自动验证令牌getTokenExtend()

```

###  Health Score

39

—

LowBetter than 85% of packages

Maintenance91

Actively maintained with recent releases

Popularity12

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity38

Early-stage or recently created project

 Bus Factor1

Top contributor holds 69% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

73d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/501c964d23378039edf0c40caa32f7f84ecaa9c5b103b280d3412dba562f24eb?d=identicon)[sym134](/maintainers/sym134)

---

Top Contributors

[![phcent](https://avatars.githubusercontent.com/u/70010565?v=4)](https://github.com/phcent "phcent (20 commits)")[![warmup413](https://avatars.githubusercontent.com/u/32946251?v=4)](https://github.com/warmup413 "warmup413 (6 commits)")[![shayvmo](https://avatars.githubusercontent.com/u/17956919?v=4)](https://github.com/shayvmo "shayvmo (3 commits)")

---

Tags

authwebman

### Embed Badge

![Health badge](/badges/jizhi-webman-auth/health.svg)

```
[![Health](https://phpackages.com/badges/jizhi-webman-auth/health.svg)](https://phpackages.com/packages/jizhi-webman-auth)
```

###  Alternatives

[ellaisys/aws-cognito

AWS Cognito package that allows Auth and other related features using the AWS SDK for PHP

120220.7k1](/packages/ellaisys-aws-cognito)[generationtux/jwt-artisan

JWT auth package for Laravel and Lumen

13953.1k](/packages/generationtux-jwt-artisan)[shopwwi/webman-auth

webman auth

194.3k3](/packages/shopwwi-webman-auth)[kinde-oss/kinde-auth-php

Kinde PHP SDK for authentication

2369.5k3](/packages/kinde-oss-kinde-auth-php)[swoft/auth

Auth component for swoft

127.2k1](/packages/swoft-auth)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
