自定义 Laravel 8 种护照方式

Customize Laravel 8 Passport methods

我使用的登录验证不是 Laravel 的默认设置。如何自定义 Passport 方法?

以下代码是我用来验证 web 中间件的。

$username = $request->username;
$password = strtoupper(md5($request->password));

$system = env("CODE_SYSTEM", 12);

$sql = "SELECT user.validate( '$system' , '$username', '$password')";
$stmt = DB::select(DB::raw($sql));
$result = $stmt[0]->validation;

if ($result == "ok") {
    $user = new User();
    $id = DB::table('users')->select('id')
        ->where('username' , $username)->first();
    $user = User::find($id->id);

    return $user;
}

要自定义您需要在用户方法中添加 validateForPassportPasswordGrant(),例如:

public function validateForPassportPasswordGrant($password)
{
    $password = strtoupper(md5($request->password));
    $system = env("CODE_SYSTEM", 12);

    $sql = "SELECT user.validate( '$system' , '$this->username', '$password')";

    $stmt = DB::select(DB::raw($sql));
    $result = $stmt[0]->validation;

    if ($result == "ok") {
        return true;
    }

    return false;
}

如果需要更改护照搜索用户名所在的用户栏名:

public function findForPassport($username)
{
    return $this->where('the_username_column', $username)->first();
}