Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.2 Error In Custom Authentication

I am getting error while making custom authentication for my laravel 5.2 however 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 (Auth/CustomUserProvider) file

    <?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 am getting error like

InvalidArgumentException in CreatesUserProviders.php line 40:
Authentication user provider [custom] is not defined.
like image 280
ujwal dhakal Avatar asked Feb 17 '16 09:02

ujwal dhakal


2 Answers

The only point is to use

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

instead of

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

The last one is used in 5.1, the first one should be used in 5.2.

like image 66
shukshin.ivan Avatar answered Oct 10 '22 07:10

shukshin.ivan


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's JSON form.
 *
 * @var array
 */
protected $hidden = [
    'passwd',
];

public function getAuthPassword(){
    //your passwor field name
    return $this->passwd;
}

public $timestamps = false;

}

create app/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()
{
    //
}

}
like image 27
Alexey Korolyev Avatar answered Oct 10 '22 09:10

Alexey Korolyev