mercredi 10 juillet 2019

Auth::attempt Call to a member function prepare() on null

Basically I get a user of username@company type and after I explode I search the data of connection of the specific database of the client.

After changing the database at runtime, I try to log in but Auth :: attempt does not work.

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\SysClient;
use App\User;
use Config;
use Hash;
use DB;

...

public function authenticate(Request $request)
{
    $request->validate([
        'username' => 'required|string',
        'password' => 'required|string',
    ]);

    $data = explode('@', $request->username);
    $username = $data[0];
    $alias = $data[1];
    $password = $request->password;
    //$password = Hash::make($request->password);

    $client = SysClient::where('alias', $alias)->first();

    $connection = [
        "driver" => $client->driver,
        "host" => $client->host,
        "port" => $client->port,
        "database" => $client->database,
        "username" => $client->username,
        "password" => $client->password,
        "unix_socket" => $client->unix_socket,
        "charset" => $client->charset,
        "collation" => $client->collation,
        "prefix" => $client->prefix,
        "prefix_indexes" => $client->prefix_indexes,
        "strict" => $client->strict,
        "engine" => $client->engine
    ];

    $request->session()->put('current_database', $connection); // save $connection in session

    Config::set('database.connections.default', $connection); // set new connection
    DB::purge('default'); // purge connection settings
    DB::reconnect('default'); // reconnect database

    //dd(DB::connection()->getDatabaseName()); //it works!

    $credentials = [
        'username' => $username, // string
        'password' => $password, // string
        'active' => 1
    ];

    //dd(User::all()); //it works!

    //So far everything works, the error happens when executing the next line

    if (Auth::attempt($credentials)) {
        dd('Authentication passed...');
    } else {
        dd('fail');
    }

}

I expected Auth::attempt to return true or false, but it does not seem to want to work with the updated database connection.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire