0
0

More than 1 year has passed since last update.

【Laravel】Session store not set on request.【Socialite】

Posted at

環境

Laravel v9.5.1 (PHP v8.1.3)

状況

Socialiteを使ってGoogleログイン機能を実装し、デプロイ後/auth/google/redirectにアクセスした際下記のエラー。

RuntimeException
Session store not set on request.
api.php
Route::get('auth/google/redirect', function () {
  return Socialite::driver('google')->redirect();
});

Route::get('auth/google/callback', [OAuthController::class, 'authGoogleCallback']);

解決法

web.phpにルーティングを記載する。
Socialiteはセッション認証なのでapi.phpではなくてweb.phpに記載しないと動かない。

web.php
Route::get('auth/google/redirect', function () {
  return Socialite::driver('google')->redirect();
});

Route::get('auth/google/callback', [OAuthController::class, 'authGoogleCallback']);

web.phpとapi.phpの違い

api.phpにはVerifyCsrfTokenミドルウェアがないので動かなかった。

app/Http/Kernel.php
   /**
     * The application's route middleware groups.
     *
     * @var array<string, array<int, class-string|string>>
     */
    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,
        ],

        'api' => [
            // \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
            'throttle:api',
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],
    ];

参考

0
0
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
0
0