0

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.

1 Answer 1

2

Laravel already provides plug and play support for Email verification included in its starter kits like Fortify, Breeze etc.

Laravel included a default Email Verification template and can be customized, for details refer here, https://laravel.com/docs/9.x/verification#customization

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class Ciudadano extends Authenticatable implements MustVerifyEmail
{
    use Notifiable;

    protected $table = 'ciudadanos';

    protected $fillable =[ 
        'cuil',
        'nombre',
        'apellido',
        'email',
        'password',
    ];
    
}

Assuming Ciudadano Class is your Main Authentication model for your application.

Also make sure your Ciudadano table contains email_verified_at datetime column as it is required.

If you don't use Starter Kits, then the email verification will not work as you need to add these on your page trigger, e.g. After the user registers to your site

Note: Put it in your Registration controller, not in Model.

use Illuminate\Auth\Events\Registered;
     
class RegisterController
{
   public function register()
   {
       // Ciudadano Registrationw workflow....

      
       event(new Registered($user)); // This will trigger the sending of email verification
   }
}
Sign up to request clarification or add additional context in comments.

4 Comments

I have a query with RegisterController. Where should this go? in what folder? also that controller, must be directed to a particular url? where it is called
@JAOdev it should be called after your successful registration workflow. The RegisterController is just an example, but for general usage, just put it on the last line of your registration logic to execute the email verification. Folder doesn't matter, it depends where your RegisterController is located.
thanks. Just a last question. What is Registered? there is this line event(new Registered($user)) but if I add use Illuminate\Auth\Events\Registered; there is not email send
It depends if you have setup a queue or not, if you have then you should start your queue listener.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.