英文:
tokenCan() method undefined
问题
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
英文:
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()
。<br>
从:
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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论