duanfei9278 2019-05-29 21:49
浏览 34

Laravel:如何在Web应用程序中从其他数据库验证用户?

We are creating a web application for our app. Our app has a database and our web application has another DB. The decision has been made to use an API to communicate with APP database rather than using one database to do all.

So because of this I have to figure out how to authenticate a user from a different database in our web application.

I have the database connection details and below is what I have done so far.

In auth.php

    'defaults' => [
         'guard' => 'custom_guard',

     ]

    'guards' => [
        // default guards here,
        'custom_guard' => [
            'driver' => 'session',
            'provider' => 'custom_users'
        ],
    ]



    'providers' => [
        //default providers,
        'custom_users' => [
             'driver' => 'eloquent',
             'model' => App\Models\Custom_User::class,
         ]
    ]


in App\Models\Custom_User

    namespace App\Models;

    use Illuminate\Database\Eloquent\Model;
    use Illuminate\Notifications\Notifiable;
    use Illuminate\Foundation\Auth\User as Authenticatable;

    class Custom_User extends Authenticatable
    {
    use Notifiable;
    protected $guard = 'custom_guard';
    protected $connection = 'secondary_connection';
    protected $table = 'users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
            //fillable stuff
        ];

        /**
         * The attributes that should be hidden for arrays.
         *
         * @var array
         */
        protected $hidden = [
            //hidden stuff
        ];
}

In database.php

'secondary_connection' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST'),
            'port' => env('DB_PORT'),
            'database' => env('DB'),
            'username' => env('DB_USERNAME'),
            'password' => env('DB_PASSWORD'),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],

What do I need to do in addition to this, or what is the best way to authenticate based on user from another DB?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 目详情-五一模拟赛详情页
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?
    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
    • ¥15 cmd cl 0x000007b
    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line