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
Try to change auth->user()
to request->user()
.
From:
if (auth()->user()->tokenCan('role:customer')) {
return $next($request);
}
To:
if ($request->user()->tokenCan('role:customer')) {
return $next($request);
}