PHP code example of codex13 / webman-auth

1. Go to this page and download the library: Download codex13/webman-auth library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

codex13 / webman-auth example snippets


use WebmanAuth\facade\Auth;

// bcrypt 为不可逆哈希,只能用 password_verify 校验
$password = '123456';
Auth::bcrypt($password);


use WebmanAuth\facade\Auth;

// 字段需与当前 guard 模型可查询字段匹配
// attempt 会自动查库校验;不适用时请使用自定义登录
$tokenObject = Auth::attempt(['name'=> 'tycoonSong','password' => '123456']);

// 返回对象包含:token_type、expires_in、refresh_expires_in、access_token、refresh_token
    
// 默认 guard 为 user,admin 登录示例
$tokenObject = Auth::guard('admin')->attempt(['name'=> 'tycoonSong','password' => '123456']);


use WebmanAuth\facade\Auth;
use app\model\User;
use app\model\Admin;
// 返回对象包含:token_type、expires_in、refresh_expires_in、access_token、refresh_token

$user = User::first();
$tokenObject = Auth::login($user); // $user 可为对象或数组
    
    
// 默认 guard 为 user,admin 登录示例
$admin = Admin::first();
$tokenObject = Auth::guard('admin')->login($admin);


    use WebmanAuth\facade\Auth;
     $user = Auth::user(); // 返回模型对象(查库)
     $user = Auth::user(true); // 返回 token 扩展字段(不查库)
     $admin = Auth::guard('admin')->user(); // 当前登录管理员
 

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

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

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();


php webman auth:key


    // 默认不抛异常,异常时返回 null
    $user = Auth::user(); // 适用于“可登录可不登录”的场景
    // 需要强制登录的场景可开启异常模式
    $user = Auth::fail()->user(); // 异常处理参考:https://www.workerman.net/doc/webman/exception.html

    // 启用 Redis 后可限制终端会话数量
    // 例如:同账号 web 最多 3 端,ios 最多 1 端
    // client_type 默认为 web,可通过 client_type=ios 指定
    // 在 config/plugin/codex13/auth/app.php 设置:
    'guard' => [
         'user' => [ // 普通用户
             'key' => 'id', // 主键字段
             'field' => ['id','username','email','mobile','avatar'], // 写入 token extend 的字段白名单
             'num' => 0, // -1 不限制;0 单端在线;>0 同一 client_type 最大在线终端数
             'model'=> app\model\User::class // 用户模型
         ]
     ]
     'jwt' => [
         'redis' => false,
         ....
      ]
     
    Auth::logout(true); // 退出该用户全部终端
    

    use WebmanAuth\Facade\JWT as JwtFace;
    JwtFace::guard('user')->make($extend,$access_exp,$refresh_exp); // 生成令牌,也可 make($extend)
    JwtFace::guard('user')->refresh($accessTime = 0); // 刷新令牌,也可 refresh()
    JwtFace::guard('user')->verify($token); // $token 可省略,省略时自动读取当前请求令牌
    JwtFace::guard('user')->getTokenExtend($token) // $token 可省略,省略时自动读取当前请求令牌