peartreedigital / boilerplate

My boilerplate for multi-tenancy websites using Laravel 5.7 and Hyn/Tenancy 5.3. Highly based on the tutorial by Ashok Gelal (https://blog.usejournal.com/writing-a-full-featured-multi-tenant-laravel-app-from-scratch-a0e1a7350d9d, I've kept the name Townhouse to honor him) and on the collective work you can find on Discord's Tenancy server
14 stars 6 forks source link

laravel hyn/multi-tenancy override login method using tenant db redirect again login page try to redirect homepage #5

Open shatarupa-mca opened 4 years ago

shatarupa-mca commented 4 years ago

In LoginController override login method using tenant db redirect again login page try to redirect homepage..

protected function validateLogin(Request $request) { $messages = [ 'identity.required' => 'Email or username cannot be empty', 'email.exists' => 'Email or username already registered', 'username.exists' => 'Username is already registered', 'password.required' => 'Password cannot be empty', ];

$request->validate([
    'identity' => 'required|string',
    'password' => 'required|string',
    'email' => 'string|exists:users',
    'username' => 'string|exists:users',
], $messages);

$domain_name = $request->get('domain_name');
$usernameatacc = $request->get('identity');
$password = $request->get('password');

$hostname = DB::table('hostnames')->select('*')->where('fqdn', $domain_name)->first();
$dbname = DB::table('websites')->select('uuid')->where('id', $hostname->website_id)->first();

Config::set("database.connections.tenant", [
    "driver"   => 'mysql',
    "host" => '127.0.0.1',
    "database" => $dbname->uuid,
    "username" => 'root',
    "password" => ''
]);

Config::set('database.default', 'tenant');
DB::purge('tenant');
DB::reconnect('tenant');

}

public function login(Request $request) { $this->validateLogin($request); $user_data = User::where('email', $request->get('identity')) ->first(); $matchPwd = Hash::check($request->get('password'), $user_data->password); if ($matchPwd == '1') { // print_r($user_data); // Here What can I do????? Please help }else { return redirect()->back()->withErrors($user_data); }

}

and in login.blade.php change the form newly added domain name which match data with townhouse db and from there it will find the tenant db name and tenant db connect dynamically within login method but after login can't redirect to home page

@csrf
@if ($errors->has('domain_name')) {{ $errors->first('domain_name') }} @endif
@if ($errors->has('email')) {{ $errors->first('email') }} @endif
@if ($errors->has('password')) {{ $errors->first('password') }} @endif