composer require shopwwi/webman-auth
use Shopwwi\WebmanAuth\Facade\Auth;
//在任意控制器里調用一次即可 比較懶沒寫命令 請原諒
Auth::jwtKey();
use Shopwwi\WebmanAuth\Facade\Auth;
//不可逆轉 只能用password_verify來判斷正確與否
Auth::bcrypt($password);
3.自動對字段進行驗證且登入
use Shopwwi\WebmanAuth\Facade\Auth;
//驗證字段一定得和設定得角色模型相匹配可以是任何字段組
$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 Shopwwi\WebmanAuth\Facade\Auth;
返回對象$tokenObject 包含token_type,expires_in,refresh_expires_in,access_token,refresh_token
$user = Users::first();
$tokenObject = Auth::login($user);//$user可以是對象 同樣可以是數(shù)組
// 默認為user角色 當你是admin登入時
$admin = Admin::first();
$tokenObject = Auth::guard('admin')->login($admin);
5.獲取當前登入用戶信息
$user = Auth::user(); //得到用戶模型對象,查庫數(shù)據,需查詢動態(tài)數(shù)據時使用
$user = Auth::user(true); // 得到擴展數(shù)據對象,非查庫數(shù)據,比如只需得到用戶ID或不常更新字段使用
$admin = Auth::guard('admin')->user(); //當前登入管理員
6.退出登入
$logout = Auth::logout(); //退出當前用戶
$logout = Auth::logout(true); // 退出所有當前用戶終端
$logout = Auth::guard('admin')->logout(); //管理員退出
7.刷新當前登入用戶token
$refresh = Auth::refresh();
$refresh = Auth::guard('admin')->refresh(); //管理員刷新
8.單獨設置過期時間
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(); //走的是異常處理類http://wtbis.cn/doc/webman/exception.html
10.開啟redis后,建議開啟
// 在使用過程中我們通常一個接口允許多端使用的情況 那么默認設置是不限制使用端口的
// 可當你想允許比如web端同一賬號只允許存在三個終端在線或同一賬號APP只允許一個終端使用
// 默認為web終端 傳參client_type=web或你其它的終端client_type=ios
//config/plugin/shopwwi/auth/app.php設置
'guard' => [
'user' => [
'key' => 'id',
'field' => ['id','name','email','mobile'], //設置允許寫入擴展中的字段
'num' => -1, // -1為不限制終端在線數(shù)量 0為只允許登入一個設備 大于0為每個終端同時在線數(shù)量 建議設置為1 則每個終端在線1個
'model'=> Shopwwi\B2b2c\Models\Users::class
]
],
Auth::logout(true); // 退出所有當前用戶終端
11.直接調用jwt
//本來是打算直接用tinywan/jwt的 可是跟我的業(yè)務邏輯不太匹配 因此改造了一些
use Shopwwi\WebmanAuth\Facade\JWT as JwtFace;
JwtFace::make(array $extend,int $access_exp = 0,int $refresh_exp = 0); //生成token
JwtFace::refresh(int $accessTime = 0); //刷新令牌
JwtFace::verify($token); //驗證令牌
Auth::logout(true);報錯
,Redis::hdel(),參數(shù)不能使用數(shù)組,否則會報錯的,array to string convertion,github 上已經提交issue~
點贊