master

laravel/framework

Last updated at: 29/12/2023 09:22

EmailVerificationRequest.php

TLDR

This file contains the EmailVerificationRequest class, which extends the FormRequest class. The class is responsible for handling requests related to email verification. It includes methods for authorizing the request, defining validation rules, fulfilling the email verification request, and configuring the validator instance.

Methods

authorize

This method determines if the user is authorized to make the email verification request. It checks if the user's key matches the id route parameter and if the hash of the user's email matches the hash route parameter. If the conditions are met, the method returns true, otherwise it returns false.

rules

This method returns an empty array. It is used to define the validation rules that apply to the request. In this case, no specific rules are defined.

fulfill

This method fulfills the email verification request. If the user's email is not already verified, it marks the email as verified using the markEmailAsVerified method of the user object. It also fires the Verified event.

withValidator

This method is used to configure the validator instance. It takes a Validator object as a parameter and returns the same object. In the provided code, no modifications are made to the validator instance.

<?php

namespace Illuminate\Foundation\Auth;

use Illuminate\Auth\Events\Verified;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Validator;

class EmailVerificationRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        if (! hash_equals((string) $this->user()->getKey(), (string) $this->route('id'))) {
            return false;
        }

        if (! hash_equals(sha1($this->user()->getEmailForVerification()), (string) $this->route('hash'))) {
            return false;
        }

        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            //
        ];
    }

    /**
     * Fulfill the email verification request.
     *
     * @return void
     */
    public function fulfill()
    {
        if (! $this->user()->hasVerifiedEmail()) {
            $this->user()->markEmailAsVerified();

            event(new Verified($this->user()));
        }
    }

    /**
     * Configure the validator instance.
     *
     * @param  \Illuminate\Validation\Validator  $validator
     * @return \Illuminate\Validation\Validator
     */
    public function withValidator(Validator $validator)
    {
        return $validator;
    }
}