尝试在Laravel 5.8中使用Auth :: attempt验证用户身份时出错

时间:2019-07-08 17:26:17

标签: php laravel laravel-5

应用程序为每个公司使用不同的数据库。

身份验证会收到一个类型为username @ company的用户名(而非电子邮件),其中爆炸后,company是一个别名,可以在应用程序的“默认”数据库中找到该公司并检索特定数据库的数据。连接到该特定数据库后,将使用用户名登录应用程序。尝试登录时,出现错误。

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

if (Auth::attempt($credentials)) { // <-- The error originates here
    //...
}

获得的错误是:

ErrorException(E_WARNING) substr()期望参数1为字符串,给定数组

在以下位置报告错误: \ vendor \ laravel \ framework \ src \ Illuminate \ Support \ Str.php第143行

我知道密码应该不带散列发送,但应用散列无效。

class UserController extends Controller
{
    public function login()
{
    return view('auth.login');
}

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

    $data = explode('@', $request->username);
    $username = $data[0];
    $company = $data[1];

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

    // database data connection
    $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('database.default', $client->database);

    \Config::set('database.connections.' . $cliente->database, $connection);

    $db = \Config::get('database.connections.' . $cliente->database);

    if($db) {
        \Config::set('database.default', $db);
    } else {
        //...
    }

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

    Up to this line, everything is fine.
    The problem comes now ...

    if (Auth::attempt($credentials)) {  // <-- The error originates here
        // Authentication passed...
        return redirect()->intended('/target');
    } else {
        //...
    }
}

我只需要让Auth :: attempt返回true或false,但是错误阻止返回。

0 个答案:

没有答案