【问题标题】:Can't get token and user data at the same time while using Vue.js and laravel/passport and GuzzleHttp使用 Vue.js 和 laravel/passport 和 GuzzleHttp 时无法同时获取令牌和用户数据
【发布时间】:2020-07-17 14:30:33
【问题描述】:

我无法同时获取用户数据和不记名令牌。使用 vue 2.5 和 laravel 7.2.2

我正在使用 GuzzleHttp 向服务器发出请求以获取令牌。

为了请求通过,我运行了第二个php artisan serve --port 8006 命令。

用户存在于数据库中 - 已验证。

我的 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());
    }
}

回复是:

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

如果我将方法设为return $response->getBody(),我只会得到不记名令牌。

如果我将方法设为return $user,我只会得到用户

尝试使用 GuzzleHttp\Promise\Promise 如下:

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

每次尝试都返回“当时之外”

【问题讨论】:

    标签: php laravel vue.js guzzle laravel-passport


    【解决方案1】:

    所以基本上你是在向自己发送一个 http 请求来登录。你为什么不做一个正常的检查并手动创建访问令牌
    我知道这不是大吃大喝,但我认为它可能会有所帮助

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

    【讨论】:

    • 已修复!非常感谢您的帮助
    猜你喜欢
    • 2020-07-12
    • 2017-09-10
    • 2017-11-19
    • 2021-03-14
    • 2021-06-12
    • 1970-01-01
    • 2016-07-20
    • 1970-01-01
    • 2020-01-25
    相关资源
    最近更新 更多