英文:
tokenCan() method undefined
问题
我目前正在遵循这个教程:
https://nothingworks.netlify.app/blog/laravel-sanctum-multi-auth/
然而,在创建中间件时,tokenCan方法未定义。
我已经安装了Laravel Passport和Sanctum,并确保我的User模型已经调用了use HasApiTokens。请帮我解决问题,谢谢。
英文:
I am currently following this tutorial,
https://nothingworks.netlify.app/blog/laravel-sanctum-multi-auth/
However when creating the middleware, the tokenCan method is undefined.
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Laravel\Passport\HasApiTokens;
class CustomerMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if (auth()->user()->tokenCan('role:customer')) {
return $next($request);
}
return response()->json('Not Authorized', 401);
}
}
I have installed Laravel passport n sanctum, and I have ensured that my User model already calls use HasApiTokens. Please help me out, thank you
答案1
得分: 1
尝试将auth->user()
更改为request->user()
。
原代码:
if (auth()->user()->tokenCan('role:customer')) {
return $next($request);
}
修改后的代码:
if ($request->user()->tokenCan('role:customer')) {
return $next($request);
}
英文:
Try to change auth->user()
to request->user()
.<br>
From:
if (auth()->user()->tokenCan('role:customer')) {
return $next($request);
}
To:
if ($request->user()->tokenCan('role:customer')) {
return $next($request);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论