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?