简体   繁体   English

Email 验证与粗鲁 API 在 Laravel 9

[英]Email verification with crud API in Laravel 9

I have a question regarding the verification of emails with Laravel.我有一个关于使用 Laravel 验证电子邮件的问题。

There are certain server considerations:有一些服务器注意事项:

The client consumes an api that doesn't have to return views, ie HTTP.客户端使用一个不必返回视图的 api,即 HTTP。 So the server only must return json responses所以服务器只需要返回 json 响应

This way all routes are defined in api.php and not in web.php这样,所有路由都在 api.php 中定义,而不是在 web.ZE1BFD762321E409CEEZ4AC06 中定义

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: 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 https://laravel.com/docs/9.x/verification#main-content

This ends up sending a views (html document)这最终发送了一个视图(html文档)

This is api.php:这是 api.php:

<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;


Route::post('ciudadano', 'App\Http\Controllers\CiudadanoController@store');

this is the model:这是 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这是 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我曾想过将这些行添加到 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:因为 If I you MustVerifyEmail 在 model 中,像这样:

<?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:我得到了这个 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. WARN PHP 致命错误:Class App\Models\Ciudadano 包含 4 个抽象方法,因此必须声明为抽象方法或实现其余方法(Illuminate\Contracts\Auth\MustVerifyEmail::hasVerifiedEmail, Illuminate\Illuminat:Contracts\EmailAsVerified \Contracts\Auth\MustVerifyEmail::sendEmailVerificationNotification, ...) 在第 10 行的 app/Models/Ciudadano.php 中。

And I don't know what it sends to the user email and how does it.而且我不知道它发送给用户 email 的内容以及它是如何发送的。

Laravel already provides plug and play support for Email verification included in its starter kits like Fortify, Breeze etc. Laravel 已经为其 Fortify、Breeze 等入门套件中包含的 Email 验证提供即插即用支持。

Laravel included a default Email Verification template and can be customized, for details refer here, https://laravel.com/docs/9.x/verification#customization Laravel 包含一个默认的 Email 验证模板,可以自定义,详细参考这里, 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.假设Ciudadano Class是您的应用程序的主要身份验证 model。

Also make sure your Ciudadano table contains email_verified_at datetime column as it is required.还要确保您的 Ciudadano 表包含必要的email_verified_at日期时间列。

If you don't use Starter Kits, then the email verification will not work as you need to add these on your page trigger, eg After the user registers to your site如果您不使用 Starter Kits,则 email 验证将不起作用,因为您需要在页面触发器上添加这些,例如在用户注册到您的站点之后

Note: Put it in your Registration controller, not in Model.注意:将其放入您的注册 controller,而不是 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
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM