0

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

2
  • It sounds In your UserController, $request->User() is null. try auth('sanctum')->user() instead. (before logout, check if current user is present, with auth('sanctum')->check()) Commented Feb 3, 2024 at 11:14
  • one more thing, Shouldn't the letter ‍u‍‍‍ in the $request->User() be in lowercase letters? Commented Feb 3, 2024 at 11:16

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.