簡體   English   中英

“用戶未登錄”-Spatie權限錯誤

[英]“User is not logged in”- Spatie Permissions Error

“消息”:“用戶未登錄。”,“例外”:“ Spatie \\ Permission \\ Exceptions \\ UnauthorizedException”,“文件”:“ / Users / robertjoseph / Dev / bengdi-api / vendor / spatie / laravel-permission /src/Exceptions/UnauthorizedException.php”,“行”:45,

每當具有錯誤角色的用戶訪問路由時,都會出現上述錯誤。 我已經在正常項目中使用了一段時間,但是這次正在構建一個API,並且我得到此錯誤是因為缺少一些不同之處。 我在$ routeMiddleware中添加了'permission'=> \\ Spatie \\ Permission \\ Middlewares \\ PermissionMiddleware :: class,'role'=> \\ Spatie \\ Permission \\ Middlewares \\ RoleMiddleware :: class 經過一番谷歌搜索后,我發現我應該在“ api”下的$ middlewareGroups中添加“ permission”和“ role”。 但是當我這樣做的時候我會得到以下錯誤

“ message”:“函數Spatie \\ Permission \\ Middlewares \\ PermissionMiddleware :: handle()的參數太少,在/ Users / Jay / Dev / BDI-api / vendor / laravel / framework / src / Illuminate / Pipeline / Pipeline中傳遞了2個第151行上的.php,並且恰好應為3。“,” exception“:” Symfony \\ Component \\ Debug \\ Exception \\ FatalThrowableError“,” file“:” / Users / Jay / Dev / BDI-api / vendor / spatie / laravel-permission /src/Middlewares/PermissionMiddleware.php”

當您使用需要參數的中間件(例如角色和/或權限)時,請定義以下路由:

// note the :super-admin. this checks if the user has the super admin role attached.
Route::group(['middleware' => ['role:super-admin']], function () {
    //
});

這將轉換為中間件類,作為缺少的第三個參數:

// RoleMiddleware
public function handle ($request, $next, $roles /* this comes through as ['super-admin'] */) {
    ...
}

$routeMiddleware只是中間件類的別名。 上面的路由組示例中使用了'role'

'permission' => \Spatie\Permission\Middlewares\PermissionMiddleware::class, 
'role' => \Spatie\Permission\Middlewares\RoleMiddleware::class,

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM