I have a logout function (to revoke the token), but when I fill in the Authorization headers with Bearer 6|4dc2p1jSWxXVdHPL8vHFK1x7SPSFysl0nOcVH78Ye3c75982 in Postman, it returns the response 'message': 'Call to a member function currentAccessToken() on null'. How can I resolve this?
my UserController.php:
<?php
namespace App\Http\Controllers\API;
use App\Helpers\ResponseFormatter;
use App\Http\Controllers\Controller;
use App\Models\User;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rules\Password;
use Livewire\Attributes\Validate;
class UserController extends Controller
{
...
/**
* Logout a user by revoking the current access token.
*
* @param Request $request The HTTP request
* @throws Some_Exception_Class description of exception
* @return Some_Return_Value
*/
public function logout(Request $request)
{
$token=$request->User()->currentAccessToken()->delete();
return ResponseFormatter::success($token, 'Token Revoked');
}
}
my routes/api.php:
<?php
use App\Http\Controllers\API\ProductCategoryController;
use App\Http\Controllers\API\ProductController;
use App\Http\Controllers\API\UserController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Laravel\Jetstream\Rules\Role;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "api" middleware group. Make something great!
|
*/
Route::middleware('auth:sanctum')->group(function(){
Route::get('user',[UserController::class,'fetch']);
Route::post('user',[UserController::class,'updateProfile']);});
Route::post('logout',[UserController::class,'logout']);
Route::get('products',[ProductController::class,'all']);
Route::get('categories',[ProductCategoryController::class,'all']);
Route::post('register',[UserController::class,'register']);
Route::post('login',[UserController::class,'login']);
my postman: postman resnponse
I want to be able to revoke/delete the token directly when I input it into Postman
UserController,$request->User()is null. tryauth('sanctum')->user()instead. (before logout, check if current user is present, withauth('sanctum')->check())uin the$request->User()be in lowercase letters?