dt102282 2015-09-03 14:31
浏览 278

在laravel 5中验证用户之前更改数据库连接

I want to manually authenticate a user in Laravel 5. My application uses 2 databases, which are located on the same server. In my application i already switch connections in the models or query builder, which works perfect.

But now i want to use laravels Auth::attempt method to login a user. But the credentials for this user are stored in the second database.

In the user model i have: protected $connection = 'first_database'; This is the database it normally should use. But for this 'special' login, i want the user model to use the second_database.

Is this possible?

What i tried:

     \Config::set('database.default', 'second_database');

    // Login this user while using the second_database
    if(\Auth::attempt(['email'=>\Request::get('email'), 'password'=>\Request::get('password')]))
    {
        dd('Success');
    }

But unfortunately, this doesn't work. I`m using Laravel 5.1

  • 写回答

1条回答 默认 最新

  • dongwen7187 2018-06-11 20:34
    关注

    Notice that you can do the following to attempt an authentication:

    $this->guard()->attempt([ 'email' => \Request::get('email'), 'password' => \Request::get('password'), ]);

    And in a nutshell, your solution will need to utilize the guard() part:

    $this->guard('my-second-database-guard')->attempt([ 'email'=>\Request::get('email'), 'password'=>\Request::get('password') ]);


    The steps:

    If you take a look at auth.php you will notice 'provider', and where does provider comes from?

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

    Provider comes in this section, same auth.php:

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

    So, we could add another User class that utilizes a different connection and use it as a provider for our guard.

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\User::class,
        ],
    
        'master-users' => [
            'driver' => 'eloquent',
            'model' => App\Master\Models\User::class,
        ],
    ]
    

    And in the guards section:

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

    Of course, we have to duplicate the User class:

    <?php
    
    namespace App\Master\Models;
    
    use App\Models\User as UserClass;
    
    class User extends UserClass
    {
         protected $connection = 'second_database';
    
        /**
         * Get the guard to be used during authentication.
         *
         * @return \Illuminate\Contracts\Auth\StatefulGuard
         */
        protected function guard()
        {
            return Auth::guard('master-web')
        }
    }
    

    And then we can attempt to authenticate the users like:

    $this->guard('master-web')->attempt([ 'email' => \Request::get('email'), 'password' => \Request::get('password'), ]);

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体