我用Laravel做了一个API REST,现在我正在尝试使用它.问题是我需要在API中验证用户,我正在使用密码授予方法.我可以正确地验证用户,我可以获得访问令牌,但从那时起,我没有看到在我的消费应用程序中使用访问令牌检索经过身份验证的用户的方法.
我尝试使用这样的路由API:
Route::get('/user', function(Request $request) {
$user = $request->user();
// Even with
$user = Auth::user();
return $user;
});
Run Code Online (Sandbox Code Playgroud)
没有骰子.我正在阅读Passport代码,但我无法弄明白.我的猜测是我需要指定一个新的防护类型或类似东西,因为Laravel Passport似乎没有提供这种类型的防护类型......
澄清事情:
或者我可以吗?也许我可以扩展验证密码授权请求的方法,以将生成的访问令牌与正在验证的用户相关联......*灯泡打开*
消费应用测试代码:
try {
$client = new Client();
$result = $client->post('https://myapi.com/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => '5',
'client_secret' => 'my_secret',
'username' => 'user_I_am_authenticating',
'password' => 'the_user_password',
'scope' => '',
] …Run Code Online (Sandbox Code Playgroud)