在 Laravel API 应用程序中实现电子邮件验证

问题描述 投票:0回答:1

我有一个 Laravel 应用程序,充当 VueJS 前端的后端 API。 我已经覆盖了 EnsureEmailIsVerified 中间的句柄方法,以便前端句柄重定向。

我面临的问题与

/mail/send-verification
路由有关,该路由位于身份验证中间件后面以获取用户。注册过程返回访问和刷新令牌,以便前端能够调用
/mail/send-verification
。如果用户在验证电子邮件之前注册并尝试登录,后端会返回 403“您的电子邮件地址未验证”。这会阻止前端调用发送验证,因为它没有访问令牌。

Route::post('/email/send-verification', [VerificationController::class, 'send'])->middleware('auth:api');

我应该采取什么方法来解决这个问题?有没有更好的方法来实现注册电子邮件验证过程?

laravel security email-verification
1个回答
0
投票

首先您需要创建自定义通知。 其次,您需要控制器来请求验证和确认码。 第三个模型和表格行用于保存 vcode

在我的例子中,通过 api 请求进行自定义电子邮件验证,如下所示: 路径:app/Notifications/apiEmailVerification.php

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use App\User;
use App\Models\ApiEmailVerification as AEV;

class apiEmailVerification extends Notification
{
use Queueable;

/**
* Create a new notification instance.
*
* @return void     
*/

public function __construct()
{
//
}

/**
* Get the notification's delivery channels.
*
* @param  mixed  $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}

/**
* Get the mail representation of the notification.
*
* @param  mixed  $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
$user_id = $notifiable->id;
$vcode = mt_rand(11111,99999);

AEV::updateOrCreate(['user_id' => $user_id], ['evcode' => $vcode]);

return (new MailMessage())
->subject('your subject')
->line('some text')
->action('Notification Action', url('/your-url'))
->line($vcode);
}

/**
* Get the array representation of the notification.
*
* @param  mixed  $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}

现在电子邮件验证控制器通过邮件将验证码发送到用户的电子邮件: 例如 EmailVerificationController.php

<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Notifications\apiEmailVerification as emailApiVerification;
use App\User;

class EmailVerificationController extends Controller
{
    
    public function update(Request $request, $id)
      {  
        $user = User::where('id', $id)->first();        
        
        if($user != null)
          {
        $user->notify(new emailApiVerification()); 
           
        return response()->json(['status' => 1, 'message' => 'Code successfully sent to user email!'], 200);
        } else {
        return response()->json(['status' => 0, 'message' => 'Code not sent, user not found!'], 404); 
        }               
     }
}

和 EmailConfirmationController.php

    <?php
    
    namespace App\Http\Controllers\Api;
    
    use App\Http\Controllers\Controller;
    use Illuminate\Http\Request;
    use App\Models\ApiEmailVerification;
    use App\User;
    
    class EmailConfirmationController extends Controller
    {
        
        public function update(Request $request, $id)
          {
             if($request->has('evcode'))
              {         
                               
            $DbEvcode = ApiEmailVerification::where('user_id', $id)->first();               
            $DBCode = $DbEvcode->evcode;               
             
            if($DBCode == $request->evcode)
                {
            User::where('id', $id)->update(['email_verified_at' => now()]);
                 
return response()->json(['status' => 1, 'message' => 'Email successfully confirmed!'], 200);
               } else {             
return response()->json(['status' => 0, 'message' => 'Email not confirmed!'], 406);
               }        
           }      
        }
    }

需要一个电子邮件验证模型:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class ApiEmailVerification extends Model
{
    protected $guarded = [];
    protected $table = 'api_email_verification';
    protected $fillable=['user_id', 'evcode'];
 
    public function user()
    {
        return $this->belongsTo('App\Models\User', 'user_id');
    }
}

这只是一个例子。您可能需要添加一些与您的网站情况和需求相关的用于显示表单等的功能。

© www.soinside.com 2019 - 2024. All rights reserved.