laravel 多帐号登录

laravel 多帐号登录

需求

当我们使用laravel auth 组件,需要多帐号登录
例:手机和邮箱都可以作为登录的帐号

以下为我个人的解决方法

    创建自定义UserProvider
Illuminate\Contracts\Auth\UserProvider 是用来实现从持久化存储系统中获取用户信息的
在app目录下创建自定义的UserProvider
laravel 多帐号登录_第1张图片
App\UserProviders\MultiAccountUserProvider 文件内容如下:


namespace App\UserProviders;

use Illuminate\Support\Str;
use Illuminate\Auth\EloquentUserProvider;

class MultiAccountUserProvider extends EloquentUserProvider{

    /**
     * 提交的字段
     * @var string
     */
    private $requestField='';

    /**
     * 数据库对应的查询字段
     * @var array
     */
    private $accountFields=array();

    /**
     * 设置提交字段
     * @param [type]
     */
    public function setMultiAccount($request_field='',$account_fields=array()){
        $this->requestField=$request_field;
        $this->accountFields=$account_fields;
    }

    /**
     * Retrieve a user by the given credentials.
     *
     * @param  array  $credentials
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
     */
    public function retrieveByCredentials(array $credentials)
    {
        if (empty($credentials) || (count($credentials) === 1 && array_key_exists('password', $credentials))) {
            return;
        }

        // 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')) {
                if(!empty($this->requestField) && !empty($this->accountFields)){
                    if($this->requestField==$key){
                        $query->where(function($query) use($value){
                            foreach($this->accountFields as $account){
                                $query->orWhere($account,$value);
                            }
                        });
                        continue;
                    }
                }
                $query->where($key, $value);
            }
        }
        return $query->first();
    }
}
    绑定到Auth服务容器
编辑App\Providers\AuthServiceProvider 文件:


namespace App\Providers;

use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Auth;
use App\UserProviders\MultiAccountUserProvider;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
        'App\Model' => 'App\Policies\ModelPolicy',
    ];

    /**
     * Register any authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();

        Auth::provider('multiAccount', function($app, array $config) {
            // 返回一个Illuminate\Contracts\Auth\UserProvider实例...
            return new MultiAccountUserProvider($app['hash'], $config['model']);
        });
    }
}
    修改配置
编辑 config\auth.php
laravel 多帐号登录_第2张图片
    修改登录控制器
在 App\Http\Controllers\Auth\LoginController.php 里添加一个方法
laravel 多帐号登录_第3张图片

到这里就已经完成了(不喜勿喷)

你可能感兴趣的:(laravel)