I have a question regarding the verification of emails with Laravel.
There are certain server considerations:
The client consumes an api that doesn't have to return views, i.e. HTTP. So the server only must return json responses
This way all routes are defined in api.php and not in web.php
I have defined a model and a controller that in the user registration process, an email should be sent but if I follow the instructions of laravel:
https://laravel.com/docs/9.x/verification#main-content
This ends up sending a views (html document)
This is api.php:
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
Route::post('ciudadano', 'App\Http\Controllers\CiudadanoController@store');
this is the model:
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Ciudadano extends Model
{
protected $table = 'ciudadanos';
protected $fillable =[
'cuil',
'nombre',
'apellido',
'email',
'password',
];
protected $hidden = [
'password',
'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
];
}
and this is the controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Ciudadano;
class CiudadanoController extends Controller
{
public function index()
{
//
}
public function store(Request $request)
{
$validated = $this->validate($request, [
'cuil' => 'required',
'nombre' => 'required',
'apellido' => 'required',
'email' => 'required',
'password' => 'required',
]);
$ciudadano = Ciudadano::create($validated);
$ciudadano->save();
//here I must sends an email to the user for authentication
return "email send";
}
}
I have thought to add these lines to the controller
$ciudadano->confirmation_code = rand(500, 15000);
send_email($ciudadano->confirmation_code);
SO:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Ciudadano;
class CiudadanoController extends Controller
{
public function index()
{
//
}
public function store(Request $request)
{
$validated = $this->validate($request, [
'cuil' => 'required',
'nombre' => 'required',
'apellido' => 'required',
'email' => 'required',
'password' => 'required',
]);
$ciudadano = Ciudadano::create($validated);
$ciudadano->save();
$ciudadano->confirmation_code = rand(500, 15000);
send_email($ciudadano->confirmation_code); // I must define this function
return "email send";
}
}
What do you think? It's a good approuch? Because If I you MustVerifyEmail in the model, like this:
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
class Ciudadano extends Model implements MustVerifyEmail
{
protected $table = 'ciudadanos';
protected $fillable =[
'cuil',
'nombre',
'apellido',
'email',
'password',
];
protected $hidden = [
'password',
'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
];
}
I got this output:
WARN PHP Fatal error: Class App\Models\Ciudadano contains 4 abstract methods and must therefore be declared abstract or implement the remaining methods (Illuminate\Contracts\Auth\MustVerifyEmail::hasVerifiedEmail, Illuminate\Contracts\Auth\MustVerifyEmail::markEmailAsVerified, Illuminate\Contracts\Auth\MustVerifyEmail::sendEmailVerificationNotification, ...) in app/Models/Ciudadano.php on line 10.
And I don't know what it sends to the user email and how does it.