Summary of laravel verification code authentication learning record

Time:2021-11-14

Note: the registration and other functions used here are used for the user authentication mechanism of laravel

Registration verification code

1. Composer installation verification code


composer require "mews/captcha:~3.0"

2. Run the following command to generate the configuration file config / captcha.php


php artisan vendor:publish --provider='Mews.aptcha.aptchaServiceProvider' 

3. Front end display verification code

< img SRC = "{captcha_src ('flat ')}}" onclick = "this. SRC =' / captcha / flat? '+ Math. Random()" title = "click the picture to get the verification code again" >

4. Back end verification (the extension package is customized for laravel, fully compatible with laravel registration function, and very convenient for verification) only needs to add the following code to the verification rules in APP / HTTP / Controllers / auth / registercontroller.php:


 'captcha' => ['required', 'captcha'],

Mailbox authentication

Email authentication provided by laravel

The email authentication file provided by laravel is located in vendor / laravel / framework / SRC / illuminate / auth / mustverifyemail.php. Because this file adopts trail declaration, we can quickly integrate the content into the user’s model through use and call it, as follows:


  namespace App\Models;
  use Illuminate\Foundation\Auth\User as Authenticatable;
  use Illuminate\Notifications\Notifiable;
  use Illuminate\Auth\MustVerifyEmail as MustVerifyEmailTrait;
  class User extends Authenticatable {
    use Notifiable, MustVerifyEmailTrait;

In order to standardize the code, we can access the email verification interface illuminate \ contracts \ auth \ mustverifyemail provided by larave, which stipulates that the model must have relevant methods. The specific methods are as follows (the methods have been defined in mustverifyemailtrail, and can be called directly):

  • Hasverifiedemail() detects whether the user email has been authenticated;
  • Markemailasverified() marks the user as authenticated;
  • Sendemailverificationnotification() sends a message notification of email authentication to trigger the sending of e-mail;
  • Getemailforverification () gets the sending email address and provides this interface to allow you to customize the mailbox field.

Send mail

We use the registercontroller provided by laravel. The controller introduces the registration function of the framework by loading the illuminate.foundation.uth.eggersusers trail. At this time, we open the trail to browse the source code and locate the register (request $request) method:

public function register(Request $request) { 
//Check whether the data submitted by the user is correct 
$this->validator($request->all())->validate(); 
//When creating a user, the event of successful user registration will be triggered, and the user will be passed as a parameter 
event(new Registered($user = $this->create($request->all()))); 
//Login user 
$this->guard()->login($user); 
//Call hook method ` registered()`
return $this->registered($request, $user) ?: redirect($this->redirectPath()); 
}

This method handles the logic after the user submits the form. We focus on event (new registered ($user = $this – > create ($request – > all());, Laravel’s event system is used here to trigger the registered event.

Open the app / providers / eventserviceprovider.php file. In the $listen attribute of this file, we can see the listener registered with the registered event:


protected $listen = [ 
  Registered::class => [ SendEmailVerificationNotification::class, ], 
];

Open the sendemailverificationnotification class and read its source code: vendor / laravel / framework / SRC / illuminate / auth / listeners / sendemailverificationnotification.php

<?php 
namespace Illuminate\Auth\Listeners;

use Illuminate\Auth\Events\Registered;
use Illuminate\Contracts\Auth\MustVerifyEmail;

class SendEmailVerificationNotification
{
  /**
   *Handling events
   *
   * @param \Illuminate\Auth\Events\Registered $event
   * @return void
   */
  public function handle(Registered $event)
  {
    //If the user inherits from mustverifyemail and has not been activated
    if ($event->user instanceof MustVerifyEmail && ! $event->user->hasVerifiedEmail()) {
      //Send mail authentication message notification (authentication mail)
      $event->user->sendEmailVerificationNotification();
    }
  }
}

It can be seen that laravel has set the mail sending logic for us by default. Next, let’s test it.

Test mail

Before testing, we first set the following email to be sent to the log for later testing:

Modify environment settings file.env


MAIL_DRIVER=log

Mail is usually sent to the log file in the storage / logs directory

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.

Recommended Today

Summary of spark conf and config configuration items

1. State configuration item summary of structured streaming Config Name Description Default Value spark.sql.streaming.stateStore.rocksdb.compactOnCommit Whether we perform a range compaction of RocksDB instance for commit operation False spark.sql.streaming.stateStore.rocksdb.blockSizeKB Approximate size in KB of user data packed per block for a RocksDB BlockBasedTable, which is a RocksDB’s default SST file format. 4 spark.sql.streaming.stateStore.rocksdb.blockCacheSizeMB The size capacity […]