Laravel Passport Auth Stuck when run on standalone project for password based token authentication

雨燕双飞 提交于 2019-12-12 19:42:15

问题


Laravel Passport Auth Stuck when run on self server and client in same project for password based token authentication

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');

In PostMan It didn't Get and Stuck Here is Output

If i will create new Project and Use that than it will work for me so what would be solution to run in same project


回答1:


If you're running your web app on the built-in PHP server / php artisan serve, then the second auth/token Passport request "kills" the first authenticaterrr HTTP request.

This is because the built-in PHP server is single threaded.

Kudos/credits to this comment.




回答2:


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());

}



回答3:


The following code works perfectly fine when you are calling your own routes/code inside project. Try this because it don't require Guzzle or curl or any other additional package.

/**
 * 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());
    }
}

this will return the ttl, refresh_token and access_token. Use as per your requirements.



来源:https://stackoverflow.com/questions/51095171/laravel-passport-auth-stuck-when-run-on-standalone-project-for-password-based-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!