douping3891 2017-02-11 17:11
浏览 83
已采纳

Auth :: user()返回null Laravel 5.1 oauth 2.0

I know this may seem duplicated. I have already checked these threads:

But I haven't found the solution to my problem

After successfully getting the access_token for a user using the credentials Auth::user() returns null within the controllers.

Here is my 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,
];

Here is my 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}', 'UserRelationsController@follow');
    Route::post('unfollow/{user}', 'UserRelationsController@unfollow');
    Route::post('trade/{user}', 'UserRelationsController@trade');
    Route::post('untrade/{user}', 'UserRelationsController@untrade');
    Route::post('capturetime', 'TimeCaptureController@store');

});

Any help would be appreciated

  • 写回答

1条回答 默认 最新

  • duanjiao3686 2017-02-11 19:43
    关注

    You need to use Authorizer::getResourceOwnerId() to get the user id. After that you should be able to use Auth::loginUsingId($userId) to log in the user for that request. You could set up a middleware to do this for you, would be something like this:

    /**
     * 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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料