いつもお世話になっております。
前回の質問で回答に倣って公式サイトを読んでみたのですが、分からないことがあるので、こちらでお伺いしたいです。
https://teratail.com/questions/190886
現在Laravelを勉強しており、マルチ認証を実装したいため、
まずは以下のサイトを参考にマルチ認証(user権限とadmin権限)を実装してみました。
前回の質問の内容を検証しているなかで、新しく分からない箇所が出てきました。
それは、
・admin権限でログインをしている際に、サイトのトップページ(localhost:8890)でAuth::guard()->check()を行うとfalseとなるが、adminの管理画面、つまりlocalhost:8890/admin/homeではtrueが返ってくる
・Auth::guard("admin")->check()ではもちろん両方ともtrueが返ってくる
なぜ、同じログイン状態でも開いているページによって、条件分岐の返り値が変わってしまうのでしょうか。
ちなみにauth.phpは以下のようにしております。
php
1<?php 2# config/auth.php 3 4return [ 5 6 /* 7 |-------------------------------------------------------------------------- 8 | Authentication Defaults 9 |-------------------------------------------------------------------------- 10 | 11 | This option controls the default authentication "guard" and password 12 | reset options for your application. You may change these defaults 13 | as required, but they're a perfect start for most applications. 14 | 15 */ 16 17 'defaults' => [ 18 'guard' => 'web', 19 'passwords' => 'users', 20 ], 21 22 /* 23 |-------------------------------------------------------------------------- 24 | Authentication Guards 25 |-------------------------------------------------------------------------- 26 | 27 | Next, you may define every authentication guard for your application. 28 | Of course, a great default configuration has been defined for you 29 | here which uses session storage and the Eloquent user provider. 30 | 31 | All authentication drivers have a user provider. This defines how the 32 | users are actually retrieved out of your database or other storage 33 | mechanisms used by this application to persist your user's data. 34 | 35 | Supported: "session", "token" 36 | メモ : https://qiita.com/washio12/items/59f5cde23b4205973c6b 37 */ 38 39 'guards' => [ 40 'web' => [ 41 'driver' => 'session', 42 'provider' => 'users', 43 ], 44 45 'api' => [ 46 'driver' => 'token', 47 'provider' => 'users', 48 'hash' => false, 49 ], 50 51 'user' => [ 52 'driver' => 'session', 53 'provider' => 'users', 54 ], 55 56 'admin' => [ 57 'driver' => 'session', 58 'provider' => 'admins', 59 ], 60 ], 61 62 /* 63 |-------------------------------------------------------------------------- 64 | User Providers 65 |-------------------------------------------------------------------------- 66 | 67 | All authentication drivers have a user provider. This defines how the 68 | users are actually retrieved out of your database or other storage 69 | mechanisms used by this application to persist your user's data. 70 | 71 | If you have multiple user tables or models you may configure multiple 72 | sources which represent each model / table. These sources may then 73 | be assigned to any extra authentication guards you have defined. 74 | 75 | Supported: "database", "eloquent" 76 | 77 */ 78 79 'providers' => [ 80 'users' => [ 81 'driver' => 'eloquent', 82 'model' => App\Models\User::class, 83 ], 84 85 'admins' => [ 86 'driver' => 'eloquent', 87 'model' => App\Models\Admin::class, 88 ], 89 ], 90 91 /* 92 |-------------------------------------------------------------------------- 93 | Resetting Passwords 94 |-------------------------------------------------------------------------- 95 | 96 | You may specify multiple password reset configurations if you have more 97 | than one user table or model in the application and you want to have 98 | separate password reset settings based on the specific user types. 99 | 100 | The expire time is the number of minutes that the reset token should be 101 | considered valid. This security feature keeps tokens short-lived so 102 | they have less time to be guessed. You may change this as needed. 103 | 104 */ 105 106 'passwords' => [ 107 'users' => [ 108 'provider' => 'users', 109 'table' => 'password_resets', 110 'expire' => 60, 111 ], 112 'admins' => [ 113 'provider' => 'admins', 114 'table' => 'password_resets', 115 'expire' => 60, 116 ], 117 ], 118 119];
bladeでは以下のようなじょうけん分岐を行なっております。
html
1@if(Auth::guard()->check()) 2 adminでログイン中です<br> 3@endif 4 5# localhost:8890 ではfalse 6# localhost:8890/admin/home ではtrue
ちなみに、デバッグで処理を追っていったところ、
php
1# vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php 2public static function __callStatic($method, $args) 3 { 4 $instance = static::getFacadeRoot(); 5 6 if (! $instance) { 7 throw new RuntimeException('A facade root has not been set.'); 8 } 9 10 return $instance->$method(...$args); 11 }
上記コードのstatic::getFacadeRoot();が始まる前に、
・localhost:8890では、guardsにweb(デフォルト値)が格納され
・localhost:8890/admin/homeではguardsにadminとuser
がそれぞれ配列の形で格納されていました。
一応自分でもソースを追ったり、公式サイトを見るようにして調べてはいるのですが、
まだ知識が浅いため、どの部分で条件分岐(Auth::guard()->check())の値が変わってしまうのかがわかりません。
もともとLaravel自体がそういった仕様であるのか、そもそもどこか見落としているのかすら判断がつきません。
一応下記のようにそれぞれの権限に対して条件分岐をすれば、もんだいはないのですが、
php
1@if(Auth::guard('web')->check()) 2 <a href="{{ route('logout') }}"><i class="fas fa-chevron-right"></i>ログアウト</a> 3@elseif(Auth::guard('admin')->check()) 4 <a href="{{ route('admin.logout') }}"><i class="fas fa-chevron-right"></i>ログアウト</a> 5@endauth
とても見栄えが悪く、user権限、admin権限問わず、ログイン状態であれば、
以下のように条件分岐ができるのが理想です。
php
1@if(Auth::guard()->check()) 2 <a href="{{ route('logout') }}"><i class="fas fa-chevron-right"></i>ログアウト</a> 3@endauth
どなたかわかる方がいらっしゃればご教授お願いいたします。
追記1
@dd(Auth::guard())をした際に、adminでログインしているにも関わらず
**・localhost:8890 : SessionGuard が "web" **
**・localhost:8890/admin/home : SessionGuard が "admin" **
のようになっているようです。

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。