2

I need to change the default User model in my Laravel Application to Admin model. I followed the tutorial given in change Authentication model in Laravel 5.3

the Admin model must do authentication, register and reset password. This is what I did so far.

After doing php artisan make:auth, I created Admin.php model

namespace Modules\Admin\Entities;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class Admin extends Authenticatable
{
 use Notifiable;
 protected $fillable = ['name','email','password'];
 protected $hidden = [
 'password', 'remember_token',
 ];
}

This is config/auth.php

return [
 /*
 |--------------------------------------------------------------------------
 | Authentication Defaults
 |--------------------------------------------------------------------------
 |
 | This option controls the default authentication "guard" and password
 | reset options for your application. You may change these defaults
 | as required, but they're a perfect start for most applications.
 |
 */
 'defaults' => [
 'guard' => 'web',
 'passwords' => 'admins',
 ],
 /*
 |--------------------------------------------------------------------------
 | Authentication Guards
 |--------------------------------------------------------------------------
 |
 | Next, you may define every authentication guard for your application.
 | Of course, a great default configuration has been defined for you
 | here which uses session storage and the Eloquent user provider.
 |
 | All authentication drivers have a user provider. This defines how the
 | users are actually retrieved out of your database or other storage
 | mechanisms used by this application to persist your user's data.
 |
 | Supported: "session", "token"
 |
 */
 'guards' => [
 'web' => [
 'driver' => 'session',
 'provider' => 'admins',
 ],
 'api' => [
 'driver' => 'token',
 'provider' => 'users',
 'hash' => false,
 ],
 ],
 /*
 |--------------------------------------------------------------------------
 | User Providers
 |--------------------------------------------------------------------------
 |
 | All authentication drivers have a user provider. This defines how the
 | users are actually retrieved out of your database or other storage
 | mechanisms used by this application to persist your user's data.
 |
 | If you have multiple user tables or models you may configure multiple
 | sources which represent each model / table. These sources may then
 | be assigned to any extra authentication guards you have defined.
 |
 | Supported: "database", "eloquent"
 |
 */
 'providers' => [
 'users' => [
 'driver' => 'eloquent',
 'model' => \Modules\Admin\Entities\Admin::class,
 ],
 // 'users' => [
 // 'driver' => 'database',
 // 'table' => 'users',
 // ],
 ],
 /*
 |--------------------------------------------------------------------------
 | Resetting Passwords
 |--------------------------------------------------------------------------
 |
 | You may specify multiple password reset configurations if you have more
 | than one user table or model in the application and you want to have
 | separate password reset settings based on the specific user types.
 |
 | The expire time is the number of minutes that the reset token should be
 | considered valid. This security feature keeps tokens short-lived so
 | they have less time to be guessed. You may change this as needed.
 |
 */
 'passwords' => [
 'users' => [
 'provider' => 'admins',
 'table' => 'password_resets',
 'expire' => 60,
 ],
 ],
];

and this is RegisterController.php

namespace App\Http\Controllers\Auth;
use Modules\Admin\Entities\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{
 /*
 |--------------------------------------------------------------------------
 | Register Controller
 |--------------------------------------------------------------------------
 |
 | This controller handles the registration of new users as well as their
 | validation and creation. By default this controller uses a trait to
 | provide this functionality without requiring any additional code.
 |
 */
 use RegistersUsers;
 /**
 * Where to redirect users after registration.
 *
 * @var string
 */
 protected $redirectTo = '/home';
 /**
 * Create a new controller instance.
 *
 * @return void
 */
 public function __construct()
 {
 $this->middleware('guest');
 }
 /**
 * Get a validator for an incoming registration request.
 *
 * @param array $data
 * @return \Illuminate\Contracts\Validation\Validator
 */
 protected function validator(array $data)
 {
 return Validator::make($data, [
 'name' => ['required', 'string', 'max:255'],
 'email' => ['required', 'string', 'email', 'max:255', 'unique:admins'],
 'password' => ['required', 'string', 'min:8', 'confirmed'],
 ]);
 }
 /**
 * Create a new user instance after a valid registration.
 *
 * @param array $data
 * @return \App\User
 */
 protected function create(array $data)
 {
 return Admin::create([
 'name' => $data['name'],
 'email' => $data['email'],
 'password' => Hash::make($data['password']),
 ]);
 }
}

This code creates a user in admins table, but when I try to login, it throws the following error:

Argument 2 passed to Illuminate\Auth\SessionGuard::__construct() must be an instance of Illuminate\Contracts\Auth\UserProvider, null given, called in ...\vendor\laravel\framework\src\Illuminate\Auth\AuthManager.php on line 125

I can't understand why this error happens.

Udhav Sarvaiya
10.2k13 gold badges60 silver badges68 bronze badges
asked Apr 2, 2019 at 8:33
2
  • AuthServiceProvider .. Commented Apr 2, 2019 at 8:41
  • @Demonyowh the Admin.php is in Admin module. Please be more specific Commented Apr 2, 2019 at 8:45

2 Answers 2

2

Change your code on config/auth.php:

'web' => [
 'driver' => 'session',
 'provider' => 'admins',
 ],

to:

'web' => [
 'driver' => 'session',
 'provider' => 'users',
 ],
answered Apr 2, 2019 at 8:43
Sign up to request clarification or add additional context in comments.

2 Comments

Wow! it worked, the administrators are in admins table, why is this work?
Because you don't actually have a provider called admins your provider in config/auth.php is still called users see here: 'users' => [ 'driver' => 'eloquent', 'model' => \Modules\Admin\Entities\Admin::class, ] So all you actually needed to do was change your model to point at your admin model which you did! :D
1

All authentication drivers have a user provider.

This defines how the users are actually retrieved out of your database or other storage mechanisms used by this application to persist your user's data.

Change in your config/auth.php file like this:

'guards' => [
 'web' => [
 'driver' => 'session',
 'provider' => 'users',
 ]
]
answered Apr 2, 2019 at 8:57

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.