##症状
laravel5.8でmulti authでadmin用の認証をつくった。
けど、adminユーザーでapi通信したら、laravel側でAuth::guard('admin')->user()->id
で取得しようとしてもid
がとれない。
##解決法
StartSession.phpを追加
App/Http/Middleware/StartSession.php
<?php
namespace App\Http\Middleware;
use \Illuminate\Session\Middleware\StartSession as BaseStartSession;
use \Illuminate\Http\Request;
use Closure;
class StartSession extends BaseStartSession
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if($request->url() == "api/*"){
return $next($request);
}
return parent::handle($request, $next);
}
}
SessionServiceProvider.phpを追加
App/Providers/SessionServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Session\SessionServiceProvider as BaseSessionServiceProvider;
use App\Http\Middleware\StartSession;
class SessionServiceProvider extends BaseSessionServiceProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->registerSessionManager();
$this->registerSessionDriver();
$this->app->singleton(StartSession::class);
}
}
Kernel.phpを編集
App/Http/Kernel.php
protected $middleware = [
\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\TrustProxies::class,
\App\Http\Middleware\ForceHttpProtocol::class,
\App\Http\Middleware\StartSession::class, //これを追加
];