Laravel 5.2 error in user authentication

I get an error when creating user authentication for my laravel 5.2, but this code works fine on my laravel 5.1 My config / auth.php file

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

My CustomUserProvider.php file (Auth / CustomUserProvider)

  <?php namespace App\Auth; use Illuminate\Contracts\Auth\UserProvider; use Illuminate\Contracts\Hashing\Hasher as HasherContract; use Illuminate\Contracts\Auth\Authenticatable as UserContract; class CustomUserProvider implements UserProvider { protected $model; public function __construct(UserContract $model) { $this->model = $model; } public function retrieveById($identifier) { } public function retrieveByToken($identifier, $token) { } public function updateRememberToken(UserContract $user, $token) { } public function retrieveByCredentials(array $credentials) { } public function validateCredentials(UserContract $user, array $credentials) { } } 

My CustomAuthProvider.php File

  <?php namespace App\Providers; use App\User; use Auth; use App\Auth\CustomUserProvider; use Illuminate\Support\ServiceProvider; class CustomAuthProvider extends ServiceProvider { /** * Bootstrap the application services. * * @return void */ public function boot() { $this->app['auth']->extend('custom',function() { return new CustomUserProvider(); }); } /** * Register the application services. * * @return void */ public function register() { // } } 

Now this works fine in laravel 5.1 in 5.2. I get an error like

 InvalidArgumentException in CreatesUserProviders.php line 40: Authentication user provider [custom] is not defined. 
+6
source share
4 answers

The only thing to use

 $this->app['auth']->provider(... 

instead

 $this->app['auth']->extend(... 

The latter is used in 5.1, the former should be used in 5.2.

+5
source

app / Models / User.php

 namespace App\Models; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { protected $connection='conn'; protected $table='users-custom'; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'login', 'passwd' ]; /** * The attributes excluded from the model JSON form. * * @var array */ protected $hidden = [ 'passwd', ]; public function getAuthPassword(){ //your passwor field name return $this->passwd; } public $timestamps = false; 

}

create application /Auth/CustomUserProvider.php

  namespace App\Auth; use Illuminate\Support\Str; use Illuminate\Contracts\Auth\Authenticatable as UserContract; use Illuminate\Contracts\Auth\UserProvider; /** * Description of CustomUserProvider * */ class CustomUserProvider implements UserProvider { /** * The hasher implementation. * * @var \Illuminate\Contracts\Hashing\Hasher */ protected $hasher; /** * The Eloquent user model. * * @var string */ protected $model; /** * Create a new database user provider. * * @param \Illuminate\Contracts\Hashing\Hasher $hasher * @param string $model class name of model * @return void */ public function __construct($model) { $this->model = $model; } /** * Retrieve a user by their unique identifier. * * @param mixed $identifier * @return \Illuminate\Contracts\Auth\Authenticatable|null */ public function retrieveById($identifier) { return $this->createModel()->newQuery()->find($identifier); } /** * Retrieve a user by their unique identifier and "remember me" token. * * @param mixed $identifier * @param string $token * @return \Illuminate\Contracts\Auth\Authenticatable|null */ public function retrieveByToken($identifier, $token) { $model = $this->createModel(); return $model->newQuery() ->where($model->getAuthIdentifierName(), $identifier) ->where($model->getRememberTokenName(), $token) ->first(); } /** * Update the "remember me" token for the given user in storage. * * @param \Illuminate\Contracts\Auth\Authenticatable $user * @param string $token * @return void */ public function updateRememberToken(UserContract $user, $token) { $user->setRememberToken($token); $user->save(); } /** * Retrieve a user by the given credentials. * * @param array $credentials * @return \Illuminate\Contracts\Auth\Authenticatable|null */ public function retrieveByCredentials(array $credentials) { // 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->createModel()->newQuery(); foreach ($credentials as $key => $value) { if (!Str::contains($key, 'password')) { $query->where($key, $value); } } return $query->first(); } /** * Validate a user against the given credentials. * * @param \Illuminate\Contracts\Auth\Authenticatable $user * @param array $credentials * @return bool */ public function validateCredentials(UserContract $user, array $credentials) { //your method auth $plain = $credentials['password']; return md5($plain)==md5($user->getAuthPassword()); } /** * Create a new instance of the model. * * @return \Illuminate\Database\Eloquent\Model */ public function createModel() { $class = '\\' . ltrim($this->model, '\\'); return new $class; } /** * Gets the hasher implementation. * * @return \Illuminate\Contracts\Hashing\Hasher */ public function getHasher() { return $this->hasher; } /** * Sets the hasher implementation. * * @param \Illuminate\Contracts\Hashing\Hasher $hasher * @return $this */ public function setHasher(HasherContract $hasher) { $this->hasher = $hasher; return $this; } /** * Gets the name of the Eloquent user model. * * @return string */ public function getModel() { return $this->model; } /** * Sets the name of the Eloquent user model. * * @param string $model * @return $this */ public function setModel($model) { $this->model = $model; return $this; } } 

in config / auth.php

 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users_office', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'users_office' => [ 'driver' => 'customUser', 'model' => App\Models\User::class, ], // 'users' => [ // 'driver' => 'database', // 'table' => 'users', // ], ], 

in \ vendor \ laravel \ framework \ src \ Illuminate \ AuthCreatesUserProviders.php

  public function createUserProvider($provider) { $config = $this->app['config']['auth.providers.'.$provider]; if (isset($this->customProviderCreators[$config['driver']])) { return call_user_func( $this->customProviderCreators[$config['driver']], $this->app, $config ); } switch ($config['driver']) { case 'database': return $this->createDatabaseProvider($config); case 'eloquent': return $this->createEloquentProvider($config); case 'customUser': return $this->createCustomUserProvider($config); default: throw new InvalidArgumentException("Authentication user provider [{$config['driver']}] is not defined."); } } protected function createCustomUserProvider($config){ return new \App\Auth\CustomUserProvider($config['model']); } 

add App \ Providers \ CustomUserAuthProvider.php

  namespace App\Providers; use Auth; use App\Models\User; use App\Auth\CustomUserProvider; use Illuminate\Support\ServiceProvider; /** * Description of CustomAuthProvider * */ class CustomUserAuthProvider extends ServiceProvider { /** * Bootstrap the application services. * * @return void */ public function boot() { Auth::extend('customUser', function($app) { // Return an instance of Illuminate\Contracts\Auth\UserProvider... return new CustomUserProvider(new User); }); } /** * Register the application services. * * @return void */ public function register() { // } } 
+2
source

Try replacing the boot function as follows:

 public function boot() { Auth::provider('custom', function($app, array $config) { // Return an instance of Illuminate\Contracts\Auth\UserProvider... return new CustomUserProvider($app['custom.connection']); }); } 
+1
source

Replace the boot function as shown below:

 public function boot() { Auth::provider('customUser', function($app, array $config) { return new CustomUserProvider($config['model']); }); } 
+1
source

All Articles