Laravel:使用Guzzle使用自定义Passport登录的解决方案

时间:2018-07-26 20:19:17

标签: laravel guzzle

我正在尝试在控制器内进行路由 / api / user / signin 和控制器,以将Guzzle HTTP发布到 / oauth / token 。很好,但是服务器停滞了。我发现了这个:https://stackoverflow.com/a/46350397/5796307

那我该怎么办?如何在没有HTTP请求的情况下调用/ oauth / token?我可以“创建”请求类并传递给该函数吗?

1 个答案:

答案 0 :(得分:2)

无需使用Guzzle或file_get_contents,无需在控制器功能内创建新的HTTP请求,并通过框架进行路由:

public function signin (Request $request) {

    // get an appropriate client for the auth flow
    $client = Client::where([
        'password_client' => true,
        'revoked'         => false
    ])->first();

    // make an internal request to the passport server
    $tokenRequest = Request::create('/oauth/token', 'post', [
            'grant_type'    => 'password',
            'client_id'     => $client->id,
            'client_secret' => $client->secret,
            'username'      => $request->input('email'),
            'password'      => $request->input('password')
    ]);

    // let the framework handle the request
    $response = app()->handle($tokenRequest);

    // get the token from the response if authenticated, other wise redirect to login
}