laravelblockederror-messaging

Laravel passwordreset error message


I want the blocked user can not perform a password reset link, receive an error message and be forwarded to a page. If a user is blocked, a 2 is stored in the table user, active. How can I do that?

I found thiy code from laravel:

/**
     * Send a reset link to the given user.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
     */
    public function sendResetLinkEmail(Request $request)
    {
        $this->validateEmail($request);

        // We will send the password reset link to this user. Once we have attempted
        // to send the link, we will examine the response then see the message we
        // need to show to the user. Finally, we'll send out a proper response.
        $response = $this->broker()->sendResetLink(
            $request->only('email')
        );

        return $response == Password::RESET_LINK_SENT
                    ? $this->sendResetLinkResponse($response)
                    : $this->sendResetLinkFailedResponse($request, $response);
    }

Solution

  • No need to overwrite sendResetLinkEmail function, you can just overwrite the validateEmail like this

    protected function validateEmail(Request $request)
    {
        $this->validate($request,   
    
            ['email' => ['required','email',
                          Rule::exists('users')->where(function ($query) {
                            $query->where('active', 1);
                          })
                        ] 
            ]
    
        );
    }
    

    OR

    if you want to redirect to custom url then overwrite sendResetLinkEmail function with manual validation like this

    public function sendResetLinkEmail(Request $request)
    {
    
         $validator = Validator::make($request->all(), [
                'email' => ['required', 'email',
                             Rule::exists('users')->where(function ($query) {
                                 $query->where('active', 1);
                             })
                           ]
                 ]);
    
         if ($validator->fails()) {
            return redirect('some_other_url')
                   ->with('fail', 'You can not request reset password, account is block');
         }
    
        // We will send the password reset link to this user. Once we have attempted
        // to send the link, we will examine the response then see the message we
        // need to show to the user. Finally, we'll send out a proper response.
        $response = $this->broker()->sendResetLink(
            $request->only('email')
        );
    
        return $response == Password::RESET_LINK_SENT
                    ? $this->sendResetLinkResponse($response)
                    : $this->sendResetLinkFailedResponse($request, $response);
    }