在独立项目上运行以基于密码的令牌认证的Laravel Passport Auth身份卡

时间:2018-06-29 05:59:02

标签: php laravel laravel-5 oauth-2.0 laravel-passport

在同一项目的自助服务器和客户端上运行以进行基于密码的令牌身份验证时,Laravel Passport Auth身份卡住

LoginController

public function authenticaterrr(Request $request)

{
     $http = new Client();

    try{
                //dd("Hello");

        $response = $http->post(url('oauth/token'), [
        'form_params' => [
            'grant_type' => 'password',
            'client_id' => '2',
            'client_secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
            // 'username' => $request->get('username'),
            // 'password' => $request->get('password'),
              'username' => 'xxxxxx@xxxxx.com',
                'password' => 'xxxxx',
            'scope'     => '*',
         ],
         ]);

       //  $apiResponse = json_decode((string) $response->getBody(), true);

       // dd($apiResponse);
         $apiResponse = json_decode((string) $response->getBody(), true);

        dd($apiResponse);
        session(['api'=> $apiResponse]);
        session(['api-token'=> $apiResponse['access_token']]);
        return json_decode($response->getBody(), true);
    }catch (ClientException $exception){
        dd("Hello");
        return json_decode($exception->getResponse()->getBody(), true);
    }
}

IN Web.php

Route::get('/auth/api/validate', 'Auth\LoginController@authenticaterrr');

在PostMan中,它没有卡住Here is Output

如果我将创建新项目并使用它,那么它将对我有用,那么在同一项目中运行将是什么解决方案

3 个答案:

答案 0 :(得分:1)

如果您在内置PHP服务器/ php artisan serve上运行Web应用,则第二个auth/token Passport请求会“杀死”第一个authenticaterrr HTTP请求。

这是因为内置的PHP服务器是单线程

this comment表示敬意/功劳。

答案 1 :(得分:0)

https://stackoverflow.com/a/51010186/8735680

function tokenRequest(Request $request){

  $request->request->add([
                "grant_type" => "password",
                "username" => $request->username,
                "password" => $request->password,
                "client_id"     => "x",
                "client_secret" => "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        ]);

        $tokenRequest = $request->create(
                env('APP_URL').'/oauth/token',
                'post'
        );

        $instance = Route::dispatch($tokenRequest);

        return json_decode($instance->getContent());

}

答案 2 :(得分:0)

当您在项目中调用自己的路由/代码时,以下代码可以正常工作。请尝试执行此操作,因为它不需要Guzzlecurl或任何其他其他程序包。

/**
 * Checking for server side OAuth authentication
 * Place this code in controller where you are performing authorization
 * @return Object
 */
public function checkOAuth(Request $req){
    try{
        $req->request->add([
            "grant_type"    => "password",
            "client_id"     => config('services.passport.client_id'),    //Get from oauth_clients table
            "client_secret" => config('services.passport.client_secret'), //Get from oauth_clients table
            "username"      => $req->username,
            "password"      => $req->password,
        ]);

        $tokenRequest = $req->create(
            config('services.passport.login_endpoint'), //'YOUR_APP_URL/oauth/token'
            'post'
        );

        $instance = \Route::dispatch($tokenRequest);

        return json_decode($instance->getContent());
    }
    catch(Exception $e){
        dd($e->getMessage());
    }
}

这将返回ttlrefresh_tokenaccess_token。根据您的要求使用。