Laravel: Laravel 8 REST CRUD API with Passport Auth in Postman not working

  laravel, postman

I wanted to test my Laravel 8 REST CRUD API with Passport Auth in Postman but I always get a 404 error. I created already an Seeder and the model and did the integration with my mysql database.

After that I created some authentication methods in my AuthController.php

namespace AppHttpControllers;

use IlluminateHttpRequest;
use AppModelsUser;

class AuthController extends Controller
{
    public function register(Request $request) {
        $request->validate([
            'name' => 'required|string|max:255',
            'email' => 'required|string|email',
            'password' => 'required|string|confirmed'
        ]);

        $user = new User();
        $user->name = $request->name;
        $user->email = $request->email;
        $user->password = bcrypt($request->password);

        if ($user->save()) {
            return response()->json([
                'message' => 'User created succesfully!',
                'status_code' => 201
            ], 201);
        } else {
            return response()->json([
                'message' => 'Some error occured, please try again',
                'status_code' => 500
            ], 500);
        }
    }
}

In my api.php I created an Auth and CRUD API Route. It should be http://localhost:8000/api/auth/register.

<?php

use IlluminateHttpRequest;
use IlluminateSupportFacadesRoute;

use AppHttpControllersCategoryController;
use AppHttpControllersAuthController;


Route::group(['prefix' => 'auth'], function() {
    Route::post('register', '[email protected]');
});

Route::resource('categories', 'AppHttpControllersCategoryController');

I tried to test it in Postman with a post request and the route http://localhost:8000/api/auth/register

{
    "name": "User2",
    "email": "[email protected]",
    "password": "password",
    "password_confirmed": "password",
}

I am stuck here for some time now and donĀ“t know how to fix it. I am running Laravel 8 if it helps.

Source: Laravel

Leave a Reply