0
votes

I'm learning Laravel and I'm trying to use Laravel Passport. When I try to create a new user I'm getting error 404 not found.

RegisterController.php

app/Http/Controllers/Auth

I also have this path for my api controllers

app/Http/Controllers/Api

Route in api.php

Route::post('/create', 'Auth\RegisterController@create');

RegisterController

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
      return Validator::make($data, [
          'username' => ['required', 'string', 'max:255', 'unique:users'],
          'password' => ['required', 'string', 'min:6', 'confirmed'],
      ]);
        // return Validator::make($data, [
        //     'name' => ['required', 'string', 'max:255'],
        //     'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
        //     'password' => ['required', 'string', 'min:6', 'confirmed'],
        // ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\User
     */
    protected function create(array $data)
    {
        return User::create([
            'username' => $data['username'],
            'password' => Hash::make($data['password']),
        ]);
    }
}

Testing registration with Insomnia

enter image description here

I know I'm not passing any values for registration.

I've tested 127.0.0.1:8000/create¹

I've tested 127.0.0.1:8000/auth/create²

I've tested 127.0.0.1:8000/Auth/create³

I can't see a way to register the user and get the token back. What is wrong? Manual is not clear for begginers.

EDIT 1

Other routes (GET) that you can see in the image are working.

1
If this is passport, shouldn't you be posting to api/create? All the routes in api.php are prefixed with api to distinguish them from your web.php routes.Tim Lewis
Yes, it made me confuse. Your tip works.Alan Godoi

1 Answers

0
votes

you need to run following artisan command to create auth routes

php artisan make:auth

here is documentation link

https://laravel.com/docs/5.7/authentication#included-routing

Update

for laravel Passport you need to add passport routes like this in AppServiceProvider boot method

Passport::routes();

Read API Authentication documentation carefully everything is there

https://laravel.com/docs/5.7/passport