Hi sir,
im not sure if this is a bug but, it hangs my app whenever i try to access my local url using guzzle
as you can see to the attach image it stuck into pending whenever i hit my button to my app:

and here is the backend controller code im not sure if i did it right or its bug:
class LoginApiController extends Controller{
public function authenticate(Client $http,Request $request)
{
$email = $request->email;
$password = $request->password;
$response = $http->post('http://localhost:8000/oauth/token/', [
'form_params' => [
'username' => $email,
'password' => $password,
'grant_type' => 'password',
'client_id' => env('API_CLIENT_ID'),
'client_secret' => env('API_CLIENT_SECRET'),
'scope' => '*'
],
]);
dd(json_decode((string) $response->getBody(), true));
}
}
i use laravel for my backend and trying to make a login authentication using laravel passport
please check thank you
Hi there,
The fact that your AJAX call hangs can be a consequence of quite a few things.
What's the URL of the AJAX call? Since it ends with token, it looks like you are trying to call the same URL from Guzzle which would mean an endless loop.
here is my ajax:
api.post('/api/auth/token', state.credentials).then((response) => {
//anything else
});
im not sure if did it right..
i tried to change my url from /api/auth/token to /api/authenticate still it hangs my ajax call..
I have exactly the same problem, what causes this?
This issue seams unrelated to Guzzle. You should investigate why http://localhost:8000/oauth/token/ does not reply.
Consider asking for help on StackOverflow or similar forums.
Hi sir @Nyholm, whenever i use guzzle such as this code:
$response = $http->post('http://localhost:8000/oauth/token/', [
'form_params' => [
'username' => $email,
'password' => $password,
'grant_type' => 'password',
'client_id' => env('API_CLIENT_ID'),
'client_secret' => env('API_CLIENT_SECRET'),
'scope' => '*'
],
]);
it hangs my ajax call but i found some code such as Route::dispatch it work perfectly!
here is my code to my back end:
$email = $request->email;
$password = $request->password;
$request->request->add([
'username' => $email,
'password' => $password,
'grant_type' => 'password',
'client_id' => env('API_CLIENT_ID'),
'client_secret' => env('API_CLIENT_SECRET'),
'scope' => '*'
]);
$tokenRequest = Request::create(
env('APP_URL').'/oauth/token',
'post'
);
$response = Route::dispatch($tokenRequest);
if($response->getStatusCode() == 200){
$this->storeAccessToken($response->getContent());
}
return $response;
im not sure why it hangs my ajax whenever i make a call with localhost though it works in postman i am getting a respond it just that it hangs in my browser.
I ended up solving it by using wamp virtualhost instead of php artisan serve. No idea why it doesn't work with localhost though.
I think this is a Guzzle related issue cause I have been stuck on this error too and using @jeimz143 suggestion helped.
This problem seems to be related with the php internal Webserver.
If you do "php -S 127.0.0.1:8000" in your public folder it wont work too. Seems like those kind of operations are stricted with the default config for php internal webserver.
I ended up solving it by using wamp virtualhost instead of php artisan serve. No idea why it doesn't work with localhost though.
Please how did you do it. Paster the code here
I ended up solving it by using wamp virtualhost instead of php artisan serve. No idea why it doesn't work with localhost though.
Please how did you do it. Paster the code here
There is no code to provide. Just set up your own virtual host for your project folder.
Easy way to do that is using something like MAMP, WampServer, XAMPP or LAMP
The reason for this is php artisan serve is a single thread application. So when we use guzzle to request from it to itself, it basically just tries to finish guzzle request (as a client) first then come to finish that request (as a server), which is impossible.
I have a way to work around it still using php artisan
// Import
use Laravel\Passport\Http\Controllers\AccessTokenController;
use Laravel\Passport\TokenRepository;
use Lcobucci\JWT\Parser as JwtParser;
use League\OAuth2\Server\AuthorizationServer;
use Psr\Http\Message\ServerRequestInterface;
// login-authen function
protected $server;
protected $tokens;
protected $jwt;
public function __construct(AuthorizationServer $server,
TokenRepository $tokens,
JwtParser $jwt)
{
$this->jwt = $jwt;
$this->server = $server;
$this->tokens = $tokens;
}
public function login(ServerRequestInterface $request)
{
$controller = new AccessTokenController($this->server, $this->tokens, $this->jwt);
$request = $request->withParsedBody($request->getParsedBody() +
[
'grant_type' => 'password',
'client_id' => config('services.passport.client_id'), //client id
'client_secret' => config('services.passport.client_secret'), //client secret
]);
return with(new AccessTokenController($this->server, $this->tokens, $this->jwt))
->issueToken($request);
}
This code will return same thing with your code but do not send any internal request
{
"token_type": "Bearer",
"expires_in": 31622400,
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6ImFiZDA3OTkxMGQzMTFhZGU2YzNjZjMxNjIzYzhiM2I4Y2FmMWE3OTcxMGEwZTkxM2UzMzBkYWIzZjRhYjY0NDM5YmU5OWNjY2UyMDViYjFjIn0.eyJhdWQiOiIyIiwianRpIjoiYWJkMDc5OTEwZDMxMWFkZTZjM2NmMzE2MjNjOGIzYjhjYWYxYTc5NzEwYTBlOTEzZTMzMGRhYjNmNGFiNjQ0MzliZTk5Y2NjZTIwNWJiMWMiLCJpYXQiOjE1NjE4MTg5NDYsIm5iZiI6MTU2MTgxODk0NiwiZXhwIjoxNTkzNDQxMzQ2LCJzdWIiOiIxMSIsInNjb3BlcyI6W119.O284KOhag3nkRGuMK_XRAv59OLFUbMKSrj6hcvObAe6JI9UN3a8JgURC56j77-bJxePFTLqiEfBOQjH8Ih29IReCzZ7CQmumfkxdivQmd8J5KjSgCNqjSJJcOI8cbxMnl41LkgeGnFoJkJAvPNfswbdi2ZuCe976X-neZeqUT9Oh6MZbJGbjKPrRYt0ItqvyGX9EilV74VNTz_5xDoALe0t64dQq0ehllEwmh8J1Qfnp6eBBpcnlNKkLrvg5amjtHaZBpdlyv9cPAgkljHZVKR745775nsSkaWNGnTsGsPJRORbTzL5iw2PJo3x_m1fqxMu1_vM3aXCOhNEaFsETCYSzYr9XmmSpNzpYXmdlSyLLRdvU-29iPLKxHQ8Dgi1aMEICrvuQolfzkEVsAXdPL7c9NQBqaXQsu76X-2Xyo-NjOMMnXOJcWbfdNnVv54DLu1xGq3S-FD45O01DTAJGHHOL3X_-ATtGxvKvLDaQnRTs14wUZyTxJ6FZMzgWLrF22IjdEM5_L3vMFYPho05-dOBd1FEQSpD5oTBCk9UVpnz1lEuNE-R8mJ2gjrPi0mpLojzPY99xF6W0eYszUt-3FYlYl2Gy5elTI1YG8zP1Np43Fyz1gMm31e1FJRlw5YLLD4W1p-Q-EYbSziZBPO-lU1AbgWS9__TIjxumCIi_oAE",
"refresh_token": "def50200f2c79b3867b3e13939d7e4a3136f8c2d6375c42cf4a11ce04d3849ae1e9a21e19430b3f18bab00b3c574b438c160e34a9ea3f69c0a4c5abe85ad87f03366e622883545a41431029c7887cce131d9598fad28027b32abe5b505b0efb2fe9500ac7562bdb64052571de7587ef5da452e134432913d20a452061b547e10cba097d067e723fa46a3d588ff5d7276da041926d530fc6f7fab34731b7ae64ef039dd38f3c8d6b79cf0381cbbdc0d913d7d05ad88ba840a59c079bfcd1f4b92d84e7d5e5f4ec7c6ceca04b99718d9c008fedece707a2139fe8c487395d03101ef1606a86dd974060309d11e81acbb23faa9ca4c21fd3c02a611957b2e5365be92870bf7fdcdaec3b40c4190cfb82e458a1870eedba2ce70109885d82ed4a98eb7dd3943b7ba2ea295669d29850fb8a776951e2d92de87724d6246a20ba6a7c71ad7b58f888c1f1e115cc1d754a322ef038de9525f6b8ab0711b5ba1adf4c483b50e"
}
@danganhvu1998 , Thank you , save my time :)
Make Virutal host and try!! localhost::8000 does not work
Most helpful comment
Hi sir @Nyholm, whenever i use guzzle such as this code:
it hangs my ajax call but i found some code such as Route::dispatch it work perfectly!
here is my code to my back end:
im not sure why it hangs my ajax whenever i make a call with localhost though it works in postman i am getting a respond it just that it hangs in my browser.