I am making project on laravel 5.7. where i have to manage login with dynamic database. where one master database and another is sub database. So when user give login detail that first check on master database if login not found on master then it will look email on App_clients table on master_db and find database name of that email id and check login .if it gone success then move to dashboard. and every time till logged in sub database ( client_2) database will active.
For Example
Database:: Master_db , Client_2_db , Client_3_db
,etc. email:abc@ex.com and password :1234 is stored in Client_2_db.
First it will check on Master_db. it will fail. then it's email will look on Master_db.App_clients and will get it database name that stored on Master_db.App_clients. So after that will try to login from Client_2_db.
So for this i am using this code on myLoginController.
if( $request->client == '1111111' )
{
Config::set('database.default', 'mysql');
DB::reconnect('mysql');
$loginCheck= Auth::attempt( ["email" =>$request->username , "password" => $request->password ] );
if( $loginCheck )
{
// Store Collage ID on session variable.
$client = SettingClient::where('client_CODE',$request->client_code)->first();
$request->session()->put('client', $client->ID );
$request->session()->put('cclient_code', $client->client_CODE );
$request->session()->put('client_name', $client->client_NAME );
$request->session()->put('database_name', 'col_master' );
return redirect('dashboard');
}
goto InvalidLoginFound;
}
elseif ( $request-> client_code != '1111111' )
{
$clientCheck = AppClient::where("client_code" , $request->client_code )->orderBy('id','desc')->first();
if( !$clientCheck )
{
goto clientCodeNotFound ;
}
DB::purge('mysql');
Config::set("database.connections.mysql", [
"driver" => "mysql",
"host" => env('DB_HOST'),
"database" => $clientCheck->database_name,
"username" => env('DB_USERNAME'),
"password" => env('DB_PASSWORD'),
"engine"=>"InnoDB",
]);
Config::set('database.default', 'mysql');
DB::reconnect('mysql');
$loginCheck= Auth::attempt( ["email" =>$request->username , "password" => $request->password ] );
if( $loginCheck )
{
// Store client ID on session variable.
$client = SettingClient::where('client_CODE',$request->client_code)->first();
if( !$client )
{
goto clientCodeNotFound;
}
$request->session()->put('client', $client->ID );
$request->session()->put('login_id', $request->username );
$request->session()->put('password', $request->password );
$request->session()->put('client_code', $client->client_CODE );
$request->session()->put('client_name', $client->client_NAME );
$request->session()->put('database_name', $clientCheck->database_name );
return redirect('dashboard');
}
$request->session()->flush();
Auth::logout();
goto InvalidLoginFound;
}
also created a middle-ware for this that manage database on each request.
class DynamicDatabaseMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if( session()->get('login_id') )
{
DB::purge('mysql');
Config::set("database.connections.mysql", [
"driver" => "mysql",
"host" => '127.0.0.1',
"database" => session()->get('database_name')?session()->get('database_name'):'',
"username" => 'root',
"password" => '',
"engine"=>"InnoDB",
]);
// Config::set('database.default', 'mysql');
DB::reconnect('mysql');
}
return $next($request);
}
}
on Karnel.php
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\App\Http\Middleware\SessionDataMiddleware::class,
\App\Http\Middleware\StoreExceptionMiddleware::class,
\App\Http\Middleware\DynamicDatabaseMiddleware::class,
],
after this is working but problem is that when i try to check login-in Auth::user() then i always get login detail from Master_db( col_master ) form from client_db. why? i do't know. please correct this code.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire