http://localhost:8000/password/email for laravel Default Auth

Published 2 weeks ago by babind

UnexpectedValueException in PasswordBroker.php line 193: User must implement CanResetPassword interface.

How to solve this issue

Best Answer (As Selected By babind)
babind

config/auth.php

return [

'defaults' => [
    'guard' => 'web',
    'passwords' => 'users',
],


'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'token',
        'provider' => 'users',
    ],
],


'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\Models\Users\User::class,
    ],
   
    // 'users' => [
    //     'driver' => 'database',
    //     'table' => 'users',
    // ],
],

'passwords' => [
    'users' => [
        'provider' => 'users',
        'table' => 'password_resets',
        'expire' => 60,
    ],
  ],

  ];
thepassenger

Post code.

Seems like you changed the default user model and it no longer extends the

Illuminate\Foundation\Auth\User.php

class.

This is the default User model:

use Illuminate\Foundation\Auth\User as Authenticatable;

....

class User extends Authenticatable //<---- this part

thepassenger

Yes. As I wrote above import the laravel auth user by doing use Illuminate\Foundation\Auth\User as Authenticatable; and then extend your custom model class YourModel extends Authenticatable

Honestly I would just use the default user model but without code can't really say what you are trying to achieve.

thepassenger

Post the code. Your model code, the full error, the controller throwing the error. A bit of structure if you changed default file locations. Anything really, it's hard to throw guesses where the error might be.

thepassenger

The code you posted seems fine to me. The error must be somewhere else. Post the full error trace

babind

The view was generated using php artisan make:auth https://laravel.com/docs/5.4/authentication#included-routing

ResetPasswordController.php

namespace App\Http\Controllers\Auth;

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

class ResetPasswordController extends Controller
{

    use ResetsPasswords;

    protected $redirectTo = '/home';

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

Controller.php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

class Controller extends BaseController
{
        use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}

   
babind

RegisterController.php

 namespace App\Http\Controllers\Auth;

 use App\Models\Users\User as User; 
 use App\Http\Controllers\Controller;
 use Illuminate\Support\Facades\Validator;
 use Illuminate\Foundation\Auth\RegistersUsers;

 class RegisterController extends Controller
 {

 use RegistersUsers;

 protected $redirectTo = '/home';

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

 protected function validator(array $data)
 {
    return Validator::make($data, [
        'name' => 'required|string|max:255',
        'email' => 'required|string|email|max:255',
        'password' => 'required|string|min:6|confirmed',
    ]);
 }

protected function create(array $data)
{
    return User::create([
        'name' => $data['name'], 
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
    ]);
}

}

babind

LoginController.php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use App\Models\Users\User as User;
use Auth;

class LoginController extends Controller
{

    use AuthenticatesUsers;

    protected $redirectTo = '/home';

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

HomeController.php

 namespace App\Http\Controllers;

 use Illuminate\Http\Request;

class HomeController extends Controller
{

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

 public function index()
 {
    return view('home');
 }

}

babind

ForgotPasswordController.php

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

    class ForgotPasswordController extends Controller
    {

        use SendsPasswordResetEmails;

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

config/auth.php

 'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\Users\User::class,
    ],
babind

App\Models\Users\User.php

 namespace App\Models\Users;
 use Illuminate\Notifications\Notifiable;
 use Illuminate\Foundation\Auth\User as Authenticatable;

 class User extends Authenticatable
 {
 use Notifiable;
 protected $fillable = [
     'email', 'password', 'name', 
 ];
  protected $hidden = [
     'password', 'remember_token',
  ];
}
babind

ResetPasswordController.php

namespace App\Http\Controllers\Auth;

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

class ResetPasswordController extends Controller
{ 
    use ResetsPasswords;
    protected $redirectTo = '/home';
    public function __construct()
    {
      $this->middleware('guest');
    }
}
babind

Web.php

 Auth::routes();
 Route::get('/home', [email protected]')->name('home');
thepassenger

By your error in the your thread post seems like laravel is using a user model without that interface. In the code you posted the user model is using the interface so I guess there has to be some kind of namespacing problem. Have you tried to restart the server? Maybe doing a composer dump-autoload would help? The full error trace would help a lot. Also when you get the error?

Sign In or create a forum account to participate in this discussion.