2017-02-11 13 views
1

これは重複しているようです。Auth :: user()がnullを返すLaravel 5.1 oauth 2.0

成功し取得した後、私は私の問題への解決策を見つけていない

しかし:私はすでに、これらのスレッドをチェックしていますユーザのアクセストークンg認証情報Auth :: user()は、コントローラ内でnullを返します。

はここにここに私のroutes.php

Route::group(['prefix' => $api_prefix, 'middleware' => 'oauth', 'namespace' => 'Api'], function() { 
    Route::resource('user', 'UserController', ['except' => ['create', 'store']]); 
    Route::resource('post', 'PostController'); 

    Route::post('follow/{user}', '[email protected]'); 
    Route::post('unfollow/{user}', '[email protected]'); 
    Route::post('trade/{user}', '[email protected]'); 
    Route::post('untrade/{user}', '[email protected]'); 
    Route::post('capturetime', '[email protected]'); 
がある私のKernel.php

protected $middleware = [ 
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class, 
    \App\Http\Middleware\EncryptCookies::class, 
    \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, 
    \Illuminate\Session\Middleware\StartSession::class, 
    \Illuminate\View\Middleware\ShareErrorsFromSession::class, 
    \LucaDegasperi\OAuth2Server\Middleware\OAuthExceptionHandlerMiddleware::class, 
]; 

protected $routeMiddleware = [ 
    'auth' => \App\Http\Middleware\Authenticate::class, 
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, 
    'oauth' => \LucaDegasperi\OAuth2Server\Middleware\OAuthMiddleware::class, 
    'oauth-user' => \LucaDegasperi\OAuth2Server\Middleware\OAuthUserOwnerMiddleware::class, 
    'oauth-client' => \LucaDegasperi\OAuth2Server\Middleware\OAuthClientOwnerMiddleware::class, 
    'check-authorization-params' => \LucaDegasperi\OAuth2Server\Middleware\CheckAuthCodeRequestMiddleware::class, 
    'csrf' => \App\Http\Middleware\VerifyCsrfToken::class, 
]; 

ある

});

すべてのヘルプは、ユーザーIDを取得するにはオーソ:: getResourceOwnerId()を使用する必要が

答えて

2

をいただければ幸いです。その後、Auth :: loginUsingId($ userId)を使用して、そのリクエストのためにユーザーにログインできるはずです。あなたのためにこれを行うミドルウェアを設定することができます:

/** 
* Handle an incoming request. 
* 
* @param \Illuminate\Http\Request $request 
* @param \Closure $next 
* @return mixed 
*/ 
public function handle($request, Closure $next) 
{ 
    $userId = Authorizer::getResourceOwnerId(); 
    if($userId) { 
     Auth::loginUsingId($userId); 
    } 
    return $next($request); 
} 
関連する問題