RFFRED
1 month ago
171
1
Laravel

Laravel/UI - forgotten password issue

Posted 1 month ago by RFFRED

Hi

I have been working on integrating Laravel/UI with Laravel 8. I am using a custom guard called "admin" and I have added the authentication controllers in app/Http/Controllers/Admin/Auth. I have also customised the views. I have managed to do the register and the login screens. I am however having issues with sending the link for the forgotten password. I keep getting the message "We can't find a user with that email address."

I have done a bit of digging through the laravel classes and the class responsible for retrieving the user is

/**
     * Retrieve a user by the given credentials.
     *
     * @param  array  $credentials
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
     */
    public function retrieveByCredentials(array $credentials)
    {
        if (empty($credentials) ||
           (count($credentials) === 1 &&
            Str::contains($this->firstCredentialKey($credentials), 'password'))) {
            return;
        }

        // First we will add each credential element to the query as a where clause.
        // Then we can execute the query and, if we found a user, return it in a
        // Eloquent User "model" that will be utilized by the Guard instances.
        $query = $this->newModelQuery();

        foreach ($credentials as $key => $value) {
            if (Str::contains($key, 'password')) {
                continue;
            }

            if (is_array($value) || $value instanceof Arrayable) {
                $query->whereIn($key, $value);
            } else {
                $query->where($key, $value);
            }
        }

        return $query->first();
    }

It is located in Illuminate\Auth\EloquentUserProvider

The following line keeps returning a "User" Model but I need to get an "Admin" Model

$query = $this->newModelQuery();

See below the ForgotPasswordController I have customised. I use it to display my view. I also apply the "admin" guard

<?php

namespace App\Http\Controllers\Admin\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;

class ForgotPasswordController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Password Reset Controller
    |--------------------------------------------------------------------------
    |
    | This controller is responsible for handling password reset emails and
    | includes a trait which assists in sending these notifications from
    | your application to your users. Feel free to explore this trait.
    |
    */

    use SendsPasswordResetEmails;


    /**
     * Display the form to request a password reset link.
     *
     * @return \Illuminate\View\View
     */
    public function showLinkRequestForm()
    {
        return view('admin.auth.passwords.email');
    }

    /**
     * Get the guard to be used during password reset.
     *
     * @return \Illuminate\Contracts\Auth\StatefulGuard
     */
    protected function guard()
    {
        return Auth::guard('admin');
    }

}

The above class uses the SendsPasswordResetEmails trait whose purpose is to find the user

SendsPasswordResetEmails which callls 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);
    }

$this->credentials($request) Keeps returning NULL as it does not find the user in the DB

I am a bit stuck. Not sure what to do next to fix it...

Please sign in or create an account to participate in this conversation.