Home > Mobile >  Wrong password reset link on email
Wrong password reset link on email

Time:10-03

I am using multi-auth on my project and my login pages are:

myurl.com/login

and

myurl.com/admin/login

The password reset routine on the myurl.com/login works fine. But when I try it on myurl.com/admin/login, the reset link that I am receiving on the email is still:

myurl.com/password/reset/XXXXXXXXXX

But it should be:

myurl.com/admin/password/reset/XXXXXXXXXX

Any help would be appreciated.

CodePudding user response:

Try to create a specific route for the administrators password reset, a single route does not take all the guards if you did not indicate it in the code. You must also specified it with if condition in notification in App\Http\Controllers\Auth\ForgotPasswordController class by overwriting Illuminate\Foundation\Auth\SendsPasswordResetEmails trait function

    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(
            $this->credentials($request)
        );

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

// or the response function

    protected function sendResetLinkResponse(Request $request, $response)
    {
        return $request->wantsJson()
                   ? new JsonResponse(['message' => trans($response)], 200)
                   : back()->with('status', trans($response));
    }

Verify if email belong to admin, and send the appropriate link Personnaly i would update sendResetLinkEmail function and create another function to send link to admin Like this

    $guard = 'user';
    $user = User::query()->where('email', $email)->first();
    if (!$user) {
        $user = Admin::query()->where('email', $email)->first();
        $guard = 'admin';
    }
    if (!$user) {
        // This for another case
        //    $user = 
        //    $guard =
    }
    // and then, instead of 
    //     return $response == Password::RESET_LINK_SENT
    //                ? $this->sendResetLinkResponse($request, $response)
    //                : $this->sendResetLinkFailedResponse($request,    $response);

    // i will do

    if ($guard == 'user') {
        $success_response = $this->sendResetLinkResponse($request, $response);
    } else if ($guard == 'admin') {
        $success_response = $this->createAdminResetResponseFunction($request, $response);
    } else {
        // Set one as default
        $success_response = $this->sendResetLinkResponse($request, $response);
    }

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

CodePudding user response:

Thank you for all who gave time to answer.

Here's what solved my problem.

I have the entry below on App\Notifications\AdminResetPassword:

public function toMail($notifiable)
{
  return (new MailMessage)
    ->line('You are receiving this email because we received a password reset request for your account.')
    ->action('Reset Password', url('admin/password/reset', $this->token))
    ->line('If you did not request a password reset, no further action is required.');
}

Then I defined this on my App\Models\Admin:

use App\Notifications\AdminResetPassword as ResetPasswordNotification;

public function sendPasswordResetNotification($token)
{
   $this->notify(new ResetPasswordNotification($token));
}

After this, my reset password link for both /login and admin/login are working.

  • Related