'Laravel 8 email verification issue - Call to a member function getKey() on null

For whatever reason, I'm having issues with email verification in Laravel 8. The email verification link is sending find, but when a user clicks the link, this error is returned: "Call to a member function getKey() on null" caused by The EmailVericationRequest.php file on php. Here is the code for that file:

     <?php
    
    namespace Illuminate\Foundation\Auth;
    
    use Illuminate\Auth\Events\Verified;
    use Illuminate\Foundation\Http\FormRequest;
    
    class EmailVerificationRequest extends FormRequest
    {
        /**
         * Determine if the user is authorized to make this request.
         *
         * @return bool
         */
        public function authorize()
        {
            if (! hash_equals((string) $this->route('id'),
   /*this line is casing the issue*/ (string) $this->user()->getKey())) {
                return false;
            }
    
            if (! hash_equals((string) $this->route('hash'),
                              sha1($this->user()->getEmailForVerification()))) {
                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 void
         */
        public function withValidator($validator)
        {
            return $validator;
        }
    }

Here is the route from auth.php:

Route::get('/verify-email/{id}/{hash}', [VerifyEmailController::class, '__invoke'])
                ->middleware('guest')
                ->name('verification.verify');

Does anybody know what would be causing this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source