PHP code example of shopwwi / webman-auth

1. Go to this page and download the library: Download shopwwi/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/ */

    

shopwwi / webman-auth example snippets


use Shopwwi\WebmanAuth\Facade\Auth;

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


use Shopwwi\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']);


use Shopwwi\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);


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

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

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

use Shopwwi\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 shopwwi:auth


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

    use Shopwwi\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()