2
votes

I am using JWT Authentication and I am trying to setup a Reset Password api functionality using ResetsPasswords.

I have created my own controller for reseting the password to use ResetsPasswords:

namespace App\Http\Controllers\v1;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;
use App\Models\PdTlogin;
use App\Models\PdTprofessional;
use App\Models\PdTpatientPainkiller;
use App\Models\PdTprofessionalQualifcation;
use App\Models\PdTprofessionalSpeciality;
use Config;
use LbTtradesman_login;
use Auth;
use Illuminate\Foundation\Auth\ResetsPasswords;

class ResetPasswordController extends Controller
{

    use ResetsPasswords;

    public function resetPassword(request $request)
    {
        return json_encode($this->reset($request));
    }

    public function __construct()
    {
        $this->middleware('guest');
    }
}

And I am calling the reset function in ResetsPasswords, here is that full controller:

namespace Illuminate\Foundation\Auth;

use Illuminate\Support\Str;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Password;

trait ResetsPasswords
{
    use RedirectsUsers;

    /**
     * Reset the given user's password.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function reset(Request $request)
    {
        //$this->validate($request, $this->rules(), $this->validationErrorMessages());

        // Here we will attempt to reset the user's password. If it is successful we
        // will update the password on an actual user model and persist it to the
        // database. Otherwise we will parse the error and return the response.
        $response = $this->broker()->reset(
            $this->credentials($request), function ($user, $password) {
                $this->resetPassword($user, $password);
            }
        );

        // If the password was successfully reset, we will redirect the user back to
        // the application's home authenticated view. If there is an error we can
        // redirect them back to where they came from with their error message.
        return $response == Password::PASSWORD_RESET
                    ? $this->sendResetResponse($response)
            : $this->sendResetFailedResponse($request, $response);


    }

    /**
     * Get the password reset validation rules.
     *
     * @return array
     */
    protected function rules()
    {
        return [
            'token' => 'required',
            'email' => 'required|email',
            'password' => 'required|confirmed|min:6',
        ];
    }

    /**
     * Get the password reset validation error messages.
     *
     * @return array
     */
    protected function validationErrorMessages()
    {
        return [];
    }

    /**
     * Get the password reset credentials from the request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    protected function credentials(Request $request)
    {
        return $request->only(
            'email', 'password', 'password_confirmation', 'token'
        );
    }

    /**
     * Reset the given user's password.
     *
     * @param  \Illuminate\Contracts\Auth\CanResetPassword  $user
     * @param  string  $password
     * @return void
     */
    protected function resetPassword($user, $password)
    {
        $user->forceFill([
            'password' => bcrypt($password),
            'remember_token' => Str::random(60),
        ])->save();

        $this->guard()->login($user);
    }

    /**
     * Get the response for a successful password reset.
     *
     * @param  string  $response
     * @return \Illuminate\Http\RedirectResponse
     */
    protected function sendResetResponse($response)
    {
    return trans($response);
    }

    /**
     * Get the response for a failed password reset.
     *
     * @param  \Illuminate\Http\Request
     * @param  string  $response
     * @return \Illuminate\Http\RedirectResponse
     */
    protected function sendResetFailedResponse(Request $request, $response)
    {
        return ['email' => trans($response)];
    }

    /**
     * Get the broker to be used during password reset.
     *
     * @return \Illuminate\Contracts\Auth\PasswordBroker
     */
    public function broker()
    {
        return Password::broker('pd_tlogin');
    }

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

But when I run everything, my password does not get updated. Instead I get this error:

Type error: Argument 1 passed to App\Http\Controllers\v1\ResetPasswordController::resetPassword() must be an instance of Illuminate\Http\Request, instance of App\User given, called in /var/www/html/my_project/vendor/laravel/framework/src/Illuminate/Foundation/Auth/ResetsPasswords.php on line 45

I really don't understand this error or what I am doing wrong :( All I know is that my User model is App\Models\PdTlogin with the database table name of pd_tlogin

UPDATE

I have also tried this:

public function resetPassword(request $request)
     {   
        $this->validate($request, [
                'token' => 'required',
                'email' => 'required|email',
                'password' => 'required|confirmed',
        ]); 
        $credentials = $request->only(
                'email', 'password', 'password_confirmation', 'token'
        );  
        $response = $this->passwords->reset($credentials, function($user, $password) {
                $user->password = bcrypt($password);
                $user->save();
                $this->auth->login($user);
        }); 
        return json_encode($response);
     } 

But I got this error:

Undefined property: App\Http\Controllers\v1\ResetPasswordController::$passwords', '/var/www/html/my_project/app/Http/Controllers/v1/ResetPasswordController.php

3
Which version of Laravel?g4b0
Laravel 5 is the version I am usinguser979331
In resetPassword have a typo error Use Request $requestShaielndra Gupta
Fixed the typo, still does not work :(user979331
$response = $this->**passwords**->reset( passwords or password ?ThataL

3 Answers

2
votes

You are invoking resetPassword of the ResetPasswordController instead of invoking resetPassword of the ResetPasswords trait. Change the resetPassword function name in your controller or use an alias for the trait resetPassword function like this:

    use ResetsPasswords
    {
        resetPassword as protected resetUserPassword;
    }
1
votes

Well, It seems you need a json resonse from the reset method. Modify your ResetPasswordController as,

<?php 

namespace App\Http\Controllers\v1;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ResetsPasswords;
//...

class ResetPasswordController extends Controller
{
    use ResetsPasswords;

    protected function sendResetResponse($response)
    {
        return response()->json(['success' => trans($response)]);
    }

    protected function sendResetFailedResponse(Request $request, $response)
    {
        return response()->json(['error' => trans($response)], 401);
    }

    // removed min:6 validation
    protected function rules()
    {
        return [
            'token' => 'required',
            'email' => 'required|email',
            'password' => 'required|confirmed',
        ];
    }

    public function __construct()
    {
        $this->middleware('guest');
    }
}

And point your reset route to ResetPasswordController@reset.

0
votes
return response()->json(['message'=> __('labels.password_updated')]);