1
0

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 3 years have passed since last update.

【Laravel8】JetStream 認証のリダイレクト先

Last updated at Posted at 2021-09-27

jetStreamを使ってみて、認証関連のリダイレクト先で戸惑ったのでメモします。

新規登録、ログイン

「RouteServiceProvider」の定数「HOME」を参照しているようです。
また、初期値には'/dashboard' が指定されていました。

app/Providers/RouteServiceProvider.php
/* 略 */
class RouteServiceProvider extends ServiceProvider
{
    /* 略 */
    public const HOME = '/dashboard';
    /* 略 */
}

ログアウト

ログアウト処理のリダイレクト処理は、デフォルトでは以下クラスのメソッドを使用しているようです。

vendor/laravel/fortify/src/Http/Responses/LogoutResponse.php
/* 略 */
class LogoutResponse implements LogoutResponseContract
{
    /* 略 */
    public function toResponse($request)
    {
        return $request->wantsJson()
                    ? new JsonResponse('', 204)
                    : redirect(Fortify::redirects('logout', '/'));
    }
}

これをオーバーライドすれば、リダイレクト先を変更可能とのこと。

今回は、参考にさせていただいたサイトのとおり、「app\Http\Responses\LogoutResponse.php」ファイルを作成します。
※Responsesディレクトリも、デフォルトでは無かったので作成します。

app\Http\Responses\LogoutResponse.php
<?php

namespace App\Http\Responses;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Laravel\Fortify\Contracts\LogoutResponse as LogoutResponseContract;
use Laravel\Fortify\Fortify;

class LogoutResponse implements LogoutResponseContract
{
    /**
     * Create an HTTP response that represents the object.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function toResponse($request)
    {
        return $request->wantsJson()
                    ? new JsonResponse('', 204)
                    : redirect('/');
    }
}

また、作成したクラスを「app\Providers\FortifyServiceProvider.php」の「boot」メソッドに記述することで、デフォルトの処理をオーバーライドできるようです。

app\Providers\FortifyServiceProvider.php
public function boot()
{
    /* 略 */
    $this->app->singleton(
        \Laravel\Fortify\Contracts\LogoutResponse::class,
        \App\Http\Responses\LogoutResponse::class
    );
}

・参考にさせていただいたサイト
https://stackoverflow.com/questions/65822861/laravel-fortify-logout-redirect

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?