That just sounds like email verification which is built in? https://laravel.com/docs/9.x/verification#main-content
VerifyEmail Laravel 9 and crud API
Hello everyone, I'm doing my first steps with Laravel and I want to implement a CRUD api to register a user. The client is on a Node.js server so I can't make use of resources in laravel.
If there is a POST request, where the user fields arrive, I have to send an email to the user to confirm it. What is the best way to do this?
What I mean is that I can save the values in the database and know if they registered their email or not if the confirmed field is complete in the DB table
This is the migration:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up()
{
Schema::create('ciudadanos', function (Blueprint $table) {
$table->id();
$table->bigInteger('cuil')->unique();
$table->string('nombre');
$table->string('apellido');
$table->string('email');
$table->string('password');
$table->timestamp('email_verified_at')->nullable();
$table->boolean('confirmed')->default(0);
$table->string('confirmation_code')->nullable();
$table->rememberToken();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('ciudadanos');
}
}
This are the fields I'd use to implement verify email:
$table->timestamp('email_verified_at')->nullable();
$table->boolean('confirmed')->default(0);
$table->string('confirmation_code')->nullable();
So, in the controller store method:
public function store(Request $request)
{
$this->validate($request, [
'cuil'=>'required',
'nombre'=>'required',
'apellido'=>'required',
'email'=>'required',
'password'=>'required',
]);
$ciudadano = Ciudadano::create($validated);
//new code to confirm email
$ciudadano->confirmation_code = rand(500, 15000);
send_email($ciudadano->confirmation_code);
$ciudadano->save();
return ("OK");
}
There is this lines:
$ciudadano->confirmation_code = rand(500, 15000);
send_email($ciudadano->confirmation_code);
Then, upon receiving OK, the user will show a screen where they must enter the code that came by email and there the field is updated $table->boolean('confirmed')->default(0); in the DB
What do you think? It's a good way to do it?
Thanks in advance
Please or to participate in this conversation.