Laravel Passport에서 서버의 액세스 토큰을 얻은 후 클라이언트의 사용자 정보에 액세스 할 수 있습니다. 이제 클라이언트 대시 보드로 리디렉션하는 방법이 고민 중입니까? , 대시 보드로 리디렉션하기 전에 Auth
에서 사용자 정보를 저장할 나중에 VarifyUser
미들웨어를 통해 다른 모든 경로를 확인하고 싶어 :Laravel 5.4 Passport - 액세스 토큰에서 사용자 정보를 가져온 후 클라이언트 대시 보드로 리디렉션하는 방법
Route::get('/callback', function (Illuminate\Http\Request $request) {
$http = new GuzzleHttp\Client;
$response = $http->post('http://localhost.server:8080/oauth/token', [
'form_params' => [
/* Auth code grant*/
'client_id' => '<client_id>',
'client_secret' => '<client_secret>',
'grant_type' => 'authorization_code',
'redirect_uri' => 'http://localhost.client:8000/callback',
'code' => $request->code,
],
]);
$auth_grant = json_decode((string) $response->getBody(), true);
$token_type = $auth_grant['token_type'];
$access_token = $auth_grant['access_token'];
$user_auth = $http->request('GET', 'http://localhost:8080/api/user', [
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'Authorization' => $token_type.' '.$access_token,
],
]);
$usrAuth = json_decode((string) $user_auth->getBody(), true);
});
주 :
여기 내 콜백 함수입니다. Auth::check
을 통해 사용자를 인증하는 것.
Route::group(['middleware' => ['verify_user', 'language']], function(){
// If you want to check loggining user, have to use 'verify_user' middleware
route::group(['namespace' => 'Index'], function(){
Route::get('/index', '[email protected]');
});
route::group(['namespace' => 'Group'], function(){
// Group page
Route::get('/group-registration', '[email protected]');
// Register group
Route::post('/registerGroup', '[email protected]');
});
}
VerifyUser
미들웨어 :
<?php
namespace App\Http\Middleware;
// Requirement
use Illuminate\Support\Facades\Auth;
use Closure;
class VerifyUser
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if(!Auth::check()){
return redirect('/login');
}
return $next($request);
}
}
당신이 리디렉션을 (사용할 수있는 방법의 끝에 재 ->로() 마지막 –
덕분에. 그러나 실제로 리다이렉트되기 전에, 나는 "Auth"에 사용자 정보를 저장하고 나중에 "Auth :: check"를 통해 사용자를 인증하려고합니다. – PS1212