8
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Laravel】自作のミドルウェア内でログインユーザー情報が取得できなかった原因

Last updated at Posted at 2019-10-09

ログインしているユーザー情報(ユーザーID)をログに出力したいなと思い、ミドルウェアを作成したのですがAuth::id()でうまく取得できなかった。

作ってみた

環境

  • Laravel 5.8

ミドルウェアを作成

UserIdLogMiddleware.php
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;

class UserIdLogMiddleware
{

    /**
     *
     *
     * @param $request
     * @param Closure $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        try{
            Auth::check()
                ?Log::info('ユーザーID:'.Auth::id())
                :Log::info('未ログイン');
        }catch(\Exception $exception){
            //ログ出力できなくてもエラーにしない
            Log::warning('ユーザーIDの取得に失敗');
        }

        return $next($request);
    }
}

ミドルウェアを登録

今回は全部のリクエスト時に適用できればいいから$middlewareに設定しよう。(今思えばこれが間違いの始まりだった・・・)

/app/Http/Kernel.php
    protected $middleware = [
        \App\Http\Middleware\TrustProxies::class,
        \App\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
        \App\Http\Middleware\UserIdLogMiddleware::class,    //ここに追加
    ];

実行結果

local.INFO: 未ログイン

・・・ログインIDが取得できていない。

原因

ブレークポイント貼って色々と処理を辿ってみたところミドルウェアの実行順番は、

  1. グローバルミドルウェア($middleware)
  2. ルートミドルウェア($routeMiddleware)

となっていた。なるほど。

と、ここで、ふと思いました。

ログインユーザ情報ってなんで取得できるんだろう??

そういえば、ログイン済みユーザだけのルーティングってauthミドルウェアを適用しているし、

Route::get('profile', function() {
    // 認証済みのユーザーのみが入れる
})->middleware('auth');

(Laravel日本語ページから引用)

その中のコントローラとかでAuth::id()ってやるし、、、

もしや、認証情報ってauthミドルウェア(\App\Http\Middleware\Authenticate)を実行した後じゃないと、Authファサードで取れないのでは?
考えてみれば、そりゃそうだ。

と、ここでKernelクラスをもう一度見てみると、

/app/Http/Kernel.php
    protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
    ];

うん、authミドルウェアはルートミドルウェア($routeMiddleware)に書いてある。

何がダメだったのか

authミドルウェアが動く前に実行されていたから!

修正

  • グローバルミドルウェア → ルートミドルウェアに変更
  • そしてauthミドルウェアの後に動かす

修正結果はこちら

/app/Http/Kernel.php
<?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    /**
     * The application's global HTTP middleware stack.
     *
     * These middleware are run during every request to your application.
     *
     * @var array
     */
    protected $middleware = [
        \App\Http\Middleware\TrustProxies::class,
        \App\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
//        \App\Http\Middleware\UserIdLogMiddleware::class,    //ここに追加
    ];

    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
            'user.log',  //グループに入れて全リクエストに適用する
        ],

        'api' => [
            'throttle:60,1',
            'bindings',
        ],
    ];

    /**
     * The application's route middleware.
     *
     * These middleware may be assigned to groups or used individually.
     *
     * @var array
     */
    protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
        'user.log' => \App\Http\Middleware\UserIdLogMiddleware::class,  //こっちに移動
    ];

    /**
     * The priority-sorted list of middleware.
     *
     * This forces non-global middleware to always be in the given order.
     *
     * @var array
     */
    protected $middlewarePriority = [
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\Authenticate::class,
        \App\Http\Middleware\UserIdLogMiddleware::class,  //実行の順番を指定しておく
        \Illuminate\Session\Middleware\AuthenticateSession::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
        \Illuminate\Auth\Middleware\Authorize::class,
    ];
}

修正後の実行結果

local.INFO: ユーザーID:2

出力された!

8
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?