小编Asi*_*Paz的帖子

通过Laravel Passport获取经过身份验证的用户并授予密码

我用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似乎没有提供这种类型的防护类型......

澄清事情:

  • 我有一个API REST应用程序,它是oAuth2服务器.
  • 我有另一个使用API​​ REST的应用程序.
  • 我知道工作流程.在我的情况下,使用密码授予,我在我的消费者应用程序中获取用户凭据,然后我向/ oauth/token发出请求,指定grant_type密码,我提供用户凭据以及我的客户凭据,我相信他们用" php artisan passport:client --password "生成(注意--password选项)
  • 我可以毫无问题地获得访问令牌.我现在需要的是获取我刚刚从API REST验证的用户的JSON表示.但问题是:我只有一个访问令牌.我无法与用户联系.

或者我可以吗?也许我可以扩展验证密码授权请求的方法,以将生成的访问令牌与正在验证的用户相关联......*灯泡打开*

消费应用测试代码:

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)

php oauth-2.0 laravel-passport laravel-5.4

15
推荐指数
3
解决办法
2万
查看次数

标签 统计

laravel-5.4 ×1

laravel-passport ×1

oauth-2.0 ×1

php ×1