简体   繁体   中英

Laravel 5.2 : Password reset email against username

My case is that a user's unique email can have many accounts . Means that one email is associated with many accounts on unique username's.

My current code works for mail, I want to send email on their username, Means that user will enter his username then the email associate with that username will get the password reset link

Current working code for direct email :

public function postEmail(Request $request)
{
    $validator = Validator::make(Input::get(),
        [
            'email' => 'required|email'
        ]
    );
    if ($validator->fails()) {
        return redirect()
            ->back()
            ->withErrors($validator->errors())
            ->with('message', 'Please fix your form fields.')
            ->with('form', 'recover')
            ->withInput(Input::except('password'));
    }
    $response = $this->passwords->sendResetLink($request->only('email'), function($message)
    {
        $message->subject('Password Reminder');
    });
    switch ($response)
    {
        case PasswordBroker::RESET_LINK_SENT:
            return redirect()
                ->back()
                ->with('message', 'A verification email was sent to your email inbox.')
                ->with('form', 'recover')
                ->withInput(Input::except('password'));

        case PasswordBroker::INVALID_USER:
            dd('true');
    }
}

I have added the following line :

$usernameToEmail = App\\User::where('name','=', Input::get());

And then i passed $usernameToEmail->email to

$response = $this->passwords->sendResetLink($usernameToEmail->email, 

    function($message)
       {
          $message->subject('Password Reminder');
       });

Which throws the following error :

Type error: Argument 1 passed to Illuminate\Auth\Passwords\PasswordBroker::sendResetLink() must be of the type array, string given

This is happening because the PasswordBroker is trying to retrieve a use by the credentials that you give and it will make a DB query to get a user. So if your username is unique you could do it like this:

$this->passwords->sendResetLink(
    ['username' => $username], 
    function($message){...}
);

This will not work you need to have unique email and username.

as token is generated against a unique users row in table and used when resetting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM