Ad

Can't Get Token And User Data At The Same Time While Using Vue.js And Laravel/passport And GuzzleHttp

I cannot get the user data and bearer token at the same time. Using vue 2.5 and laravel 7.2.2

I am using GuzzleHttp to make a request to the server in order to get the token.

In order to the request to go through I run a second php artisan serve --port 8006 command.

User exists in the database - have verified.

My AuthController:

use App\User;
use App\Shop;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use \GuzzleHttp\Client;
use DB;

public function login(Request $request)
{

    $user = User::where('email', $request->email)->get();

    $client = new Client([
        'base_uri' => 'http://127.0.0.1:8006',
        'timeout' => 5
    ]);

    try {

        $response = $client->request('POST', 'oauth/token', [
            'form_params' => [
                'grant_type' => 'password',
                'client_id' => 2,
                'client_secret' => DB::table('oauth_clients')->where('id', 2)->first()->secret,
                'username' => $request->email,
                'password' => $request->password,
            ]
        ]);

        $user->token = $response->getBody();

        return $user;
    } catch (\GuzzleHttp\Exception\BadResponseException $e) {
        if ($e->getCode() === 400 || $e->getCode() == 401) {
            return response()->json(['message' => 'Грешно потребителко име или парола'], $e->getCode());
        }

        return response()->json('Something went wrong on the server.', $e->getCode());
    }
}

The response is:

  [
  {
    "id": 1,
    "name": "Nelly",
    "email": "[email protected]",
    "email_verified_at": null,
    "created_at": "2020-04-05T11:18:30.000000Z",
    "updated_at": "2020-04-05T11:18:30.000000Z"
  }
]

If I make the method to return $response->getBody() I get the bearer token only.

If I make the method to return $user I get the user only

Tried using GuzzleHttp\Promise\Promise as follows:

public function login(Request $request)
{

    $user = User::where('email', $request->email)->get();

    $client = new Client([
        'base_uri' => 'http://127.0.0.1:8006',
        'timeout' => 5
    ]);

    try {
        $promise = $client->requestAsync('POST', 'oauth/token', [
            'form_params' => [
                'grant_type' => 'password',
                'client_id' => 2,
                'client_secret' => DB::table('oauth_clients')->where('id', 2)->first()->secret,
                'username' => $request->email,
                'password' => $request->password,
            ]
        ]);

        $promise->then(
            // $onFulfilled
            function ($value) {
                dd('The promise was fulfilled.');
            },
            // $onRejected
            function ($reason) {
                dd('The promise was rejected.');
            }
        );
        dd('outside of the then');
    }
}

Every attempt returns "outside of the then"

Ad

Answer

So basically you are sending an http request to yourself to get logged in. why don't you just do a normal check and create access token manually
I know this is not guzzle but I thought it might be helpful

Source

if (! Auth::attempt($request->only(['email', 'password']))) {
    throw new \Exception('User not found');
}

/** @var User $user */
$user = Auth::user();

$token = $user->createToken('Token Name')->accessToken;;
dd($token);
Ad
source: stackoverflow.com
Ad