이해가 하나도 안되다가 이해하기 쉬운 코드를 알게되어 정리하여 둔다.
// composer.json
"laravel/passport": "^2.0"
// api.php
Route::post('auth/token', 'Restful\TokenController@authenticate');
Route::post('auth/refresh', 'Restful\TokenController@refreshToken');
class TokenController extends Controller
{
/**
* @var object
*/
private $client;
/**
* DefaultController constructor.
*/
public function __construct()
{
// Get grant type : password...
$this->client = DB::table('oauth_clients')->where('id', 2)->first();
}
/**
* @param Request $request
* @return mixed
*/
protected function authenticate(Request $request)
{
$request->request->add([
'username' => $request->username,
'password' => $request->password,
'grant_type' => 'password',
'client_id' => $this->client->id,
'client_secret' => $this->client->secret,
'scope' => '*'
]);
$proxy = Request::create(
'oauth/token',
'POST'
);
return Route::dispatch($proxy);
}
/**
* @param Request $request
* @return mixed
*/
protected function refreshToken(Request $request)
{
$request->request->add([
'grant_type' => 'refresh_token',
'refresh_token' => $request->refresh_token,
'client_id' => $this->client->id,
'client_secret' => $this->client->secret,
]);
$proxy = Request::create(
'/oauth/token',
'POST'
);
return Route::dispatch($proxy);
}
}