What causes the failure of this Laravel 8 API registration atempt?

Published

I am working on an API authentication application with Laravel 8.79.0 and Sanctum.

As far as I have seen, Sanctum is included in this version of Laravel, so I have not installed it myself but only uncommented this line from appHttpKernel.php:

// LaravelSanctumHttpMiddlewareEnsureFrontendRequestsAreStateful::class,

In routesapi.php, I have:

Route::post('/register', [AuthController::class, 'register']);
Route::post('/login', [AuthController::class, 'login']);

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

The up() method of the migration file responsible for creating the users table looks like this:

public function up()
 {
    Schema::create('users', function (Blueprint $table) {
        $table->id();
        $table->string('first_name');
        $table->string('last_name');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->string('city')->nullable();
        $table->rememberToken();
        $table->timestamps();
    });
}

It run successfully, the users table was created.

In appModelsUser.php:

<?php

namespace AppModels;
use IlluminateContractsAuthMustVerifyEmail;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateFoundationAuthUser as Authenticatable;
use IlluminateNotificationsNotifiable;
use LaravelSanctumHasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'first_name',
        'last_name',
        'email',
        'password',
        'country',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

In appHttpControllersAuthController.php:

use AppModelsUser;
use IlluminateHttpRequest;
use IlluminateSupportFacadesHash;

class AuthController extends Controller
 {
    public function register(Request $request) {
        $fields = $request->validate([
            'first_name' => 'required|string',
            'last_name' => 'required|string',
            'email' => 'required|string|unique:users,email',
            'password' => 'required|string|confirmed',
            'city' => 'string',
        ]);

        $user = User::create([
            'first_name' => $fields['first_name'],
            'last_name' => $fields['last_name'],
            'email' => $fields['email'],
            'password' => bcrypt($fields['password']),
            'city' => $fields['city'],
        ]);

        $token = $user->createToken('myapptoken')->plainTextToken;

        $response = [
            'user' => $user,
            'token' => $token
        ];

        return response($response, 201);
    }
}

The problem

I have attempted to register a user, with Postman, via the route http://mysite.test/api/register (I have a virtual host on a WAMP server), the response is 200 OK, and no user is added in the users table.

What am I doing wrong?

Source: Laravel

Published
Categorised as laravel, laravel-8, php Tagged , ,

Answers

Leave a Reply

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq