The GET method is not supported for route api/customer/login. Supported methods: POST. in file

So I received this error in Postman when I tried logout function
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException: The GET method is not supported for route api/customer/login. Supported methods: POST. in file D:\Codes\Laravel\posdjuragan\vendor\laravel\framework\src\Illuminate\Routing\AbstractRouteCollection.php on line 122

AuthController.php

<?php

namespace App\Http\Controllers;

// app/Http/Controllers/AuthController.php

use App\Models\Customer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Laravel\Sanctum\Actions\CreateNewApiToken;

class AuthController extends Controller
{
    public function registerCustomer(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'nama' => 'required|string|max:255',
            'email' => 'required|string|email|unique:customers,email',
            'password' => 'required|string|min:8',
        ]);

        if ($validator->fails()) {
            return response(['error' => $validator->errors()->first()], 422);
        }

        $customer = Customer::create([
            'nama' => $request->nama,
            'email' => $request->email,
            'password' => Hash::make($request->password),
            'role' => 'customer',
        ]);

        $customer->sendEmailVerificationNotification();

        return response(['customer' => $customer, 'token' => $customer->createToken('auth_token')->plainTextToken]);
    }


    public function login(Request $request)
    {
       $credentials = $request->only('email', 'password');

       $customer = Customer::where('email', $request->email)->firstOrFail();

       if ($customer && Hash::check($credentials['password'], $customer->password)) {
           $token = $customer->createToken('auth_token')->plainTextToken;

           return response(['user' => $customer, 'token' => $token]);
       }

       return response(['error' => 'Invalid credentials'], 401);
    }

    public function verify($id, Request $request)
    {
        if (!$request->hasValidSignature()) {
            return response()->json([
                'status' =>  false,
                'message' => 'Verifying email gagal.'
            ], 400);
        }

        $user = Customer::where('id', $id)->where('role', 'customer')->first();

        if ($user && !$user->hasVerifiedEmail()) {
            $user->markEmailAsVerified();
            $user->email_verified_at = now();
            $user->save();
        }

        return redirect()->to("https://stackoverflow.com/");
    }

    public function notice()
    {
        return response()->json([
            'status' => false,
            'message' => 'Anda belum melakukan verifikasi email.'
        ], 400);
    }


    public function logout(Request $request)
    {
       auth()->user()->tokens()->delete();

       return response()->json(['message' => 'Logged out successfully']);
    }
}

And here’s the API route

api.php

<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AuthController;

/*
|--------------------------------------------------------------------------
| 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!
|
*/
// Auth

Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
   return $request->user();
});


Route::prefix('customer')->group(function () {
   Route::post('register', [AuthController::class, 'registerCustomer']);
   Route::post('login', [AuthController::class, 'login'])->name('login');

   Route::middleware(['auth:sanctum', 'role:customer'])->group(function () {
    Route::post('logout', [AuthController::class, 'logout'])->name('logout');
    // Route::get('email/verify/{id}', [AuthController::class, 'verify'])->name('verification.verify');
    // Route::get('email/verify', [AuthController::class, 'notice'])->name('verification.notice');
});
});

Apparently the login works but not the logout, it doesn’t delete the personal_access_data

I tried to change POST to GET and tried a different auth method also changed the logout function several times.

  • Well, as long as you define POST methods, the GET method probably won’t exist. You say you tried setting it to GET? What happened, other error or same error? If same error, did you clear caches?

    – 




Leave a Comment