0
votes

I am using this function to send a request to oauth/token path using Laravel Passport but when I send a request to function which has the code given below from Postman, it stucks when its get there and response nothing until I cancel the request. I used dd() function everywhere to figure out where the code stucks and its this part.

    $response = $http->post(url('oauth/token'), [
        'form_params' => [
            'grant_type' => 'password',
            'client_id' => $client->id,
            'client_secret' => $client->secret,
            'username' => $request->name,
            'password' => $request->password,
            'scope' => ''
        ]
    ]);

I tried this function using 4 different computer. 2 of them has Vagrant and worked perfectly on these machines. But I couldnt run this function using Mac Computers.

I tried to send a request to different path using GuzzleHttp\Client and it worked on Mac.

I tried to send a request to oauth/token path using Postman on Mac, it worked again.

Here is my complete login function:

/*
 * Sends a POST request to "/login" with these parameters to login:
 * domain, name, password.
 * It returns a token, save it somewhere locally to access other routes.
 */
public function login(Request $request){
    $request->validate([
        'domain' => 'required',
        'name' => 'required',
        'password' => 'required',
    ]);

    /*
     * Gets the domain information such as database name, database ip etc.
     * Then, connects to database with these informations to check if user exist or not.
     */
    $domain = Domain::where('DOMAIN', $request->domain)->first();
    if(!$domain){
        return response([
            'status' => 'error',
            'message' => 'The credentials do not match with our records'
        ], 400);
    }

    setDatabase($domain);

    /*
     * Checks the existence of the user
     */
    $user = Kullanici::where('KULLANICI', $request->name)->first();
    if(!$user || bcrypt($request->password) != $user->SIFRE){
        return response([
                'status' => 'error',
                'message' => 'The credentials do not match with our records'
        ], 400);
    }


    /*
     * Sends a request to given url for the access token. If successfull,
     * returns the token
     */
    $http = new Client;
    $client = ClientApp::where('id',2)->first();
    DB::setDefaultConnection('mysql');

    $response = $http->post(url('oauth/token'), [
        'form_params' => [
            'grant_type' => 'password',
            'client_id' => $client->id,
            'client_secret' => $client->secret,
            'username' => $request->name,
            'password' => $request->password,
            'scope' => ''
        ]
    ]);

    /*
     * encode the domain information in JWT format and return this token also.
     * This token gets decoded in every request and updates the database with this info.
     * You can check the details in Domain Middleware
     */
    $payload = [
        "domain" => $request->domain
    ];
    $jwt_domain = JWT::encode($payload);

    return response([
        'auth' => json_decode((string)$response->getBody(), true),
        'domain' => $jwt_domain
    ], 200);
}
1

1 Answers

1
votes

I could solve this error by adding a Virtual DNS to my web server. Apparently, being connected to a network, the request is confused and fails to recognize the localhost to which it should be directed. You add the DNS to the folder of your project.