Laravel Passport:仅使用用户名登录,无需密码

And*_*ter 5 php laravel laravel-passport

在我的项目中,我将通过使用短信发送确认码来验证用户电话号码。确认后,我必须向我的 Laravel 应用程序发送登录请求。在Laravel Passport文档中有登录用户的示例请求:

$response = $http->post('http://your-app.com/oauth/token', [
    'form_params' => [
        'grant_type' => 'password',
        'client_id' => 'client-id',
        'client_secret' => 'client-secret',
        'username' => 'taylor@laravel.com',
        'password' => 'my-password',
        'scope' => '',
    ],
]);
Run Code Online (Sandbox Code Playgroud)

我如何在这里设置为username用户phone并删除password

如果password是 Laravel Passport 中的必填字段,那么我可以从客户端发送任何密码并passwordattempt用户凭据之前发送请求用户。但是我必须在哪里输入密码?

我已经厌倦了改变LoginController但没有工作。

登录控制器

protected function validateLogin(Request $request)
{
    $request->validate([
        $this->username() => 'required|string'
    ]);
}

protected function credentials(Request $request)
{
    $params = [
        'password' => '12345678',
    ];
    $request->request->add($params);
    return $request->only($this->username(), 'password');
}

public function username()
{
    return 'phone';
}
Run Code Online (Sandbox Code Playgroud)

Kar*_*ana 2

findForPassport在您的用户模型中使用方法。

它以用户名作为参数,并返回用户模型

这里已经回答了

class User extends Authenticatable
{
    use Notifiable, HasApiTokens;

    // Set as username any column from users table
    public function findForPassport($username) 
    {
        $customUsername = 'phone';
        return $this->where($customUsername, $username)->first();
    }
    // Owerride password here
    public function validateForPassportPasswordGrant($password)
    {
        $owerridedPassword = 'password';
        return Hash::check($password, $this->password);
    }
}
Run Code Online (Sandbox Code Playgroud)