9

Hello guys is there any ways to redirect the logout function of Fortify?

<div class="nav-link" id="nav-bar-logoutbutton">                       
        <form method="POST" action="{{ route('logout') }}">
            @csrf
            <button class="btn btn-secondary btn-sm" type="submit">Logout</button>
        </form>
    </div>

this is my blade logout

Kimpy Doto
  • 93
  • 1
  • 4

4 Answers4

18

You can do the following:

Create a new LogoutResponse class and implement your redirect logic into the toResponse method:

"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 Symfony\Component\HttpFoundation\Response;

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

Now you can bind the new response into the service container in the boot method of your FortifyServiceProvider:

"app/Providers/FortifyServiceProvider.php"

public function boot()
{
    $this->app->singleton(
        \Laravel\Fortify\Contracts\LogoutResponse::class,
        \App\Http\Responses\LogoutResponse::class
    );
}
Remul
  • 7,874
  • 1
  • 13
  • 30
  • 1
    Thanks! Anyone know the reason why they done it this way? Why not use an optional config parameter in fortify config... A whole class just to redirect different seems like a lot of overhead. – Tim van Uum May 11 '21 at 13:37
  • 3
    I agree with @TimvanUum with a clarification on why to do it in this way. – AleksanderGD May 12 '21 at 10:52
  • I also agree with the comments . There should have been a class for logout method to be in. And we could easily customize it. Thanks anyway! I can confirm that it works with Laravel 8.80. – Mycodingproject Feb 05 '22 at 20:04
10

In your config/fortify.php, add:

    'redirects' => [
        'logout' => 'login',
    ],
Yvan Burrie
  • 101
  • 1
  • 4
  • I am searching for so long without any result, and your answer saved my day. This also works for anyone who use jetstream as its using fortify for the auth – Mario Ariyanto Jun 20 '23 at 23:42
0

Just create a new post request in your routes/web.php

Route::post('logout', [ClientController::class, 'logout'])->name('logout');

Now in your controller, create a function to handle the request, make sure to include the Auth class at the top.

use Auth;

/* Process the logout request */
public function logout(Request $request) {
        Auth::logout();
        return redirect('/login')->with(['msg_body' => 'You signed out!']);
}

Instead of /login, you can redirect to anywhere.

Vinny
  • 597
  • 3
  • 11
  • 26
0

One can, alternatively, set redirect url after logout changing AuthenticatedSessionController#destroy.

App/Http/Controllers/Auth/AuthenticatedSessionController.php

public function destroy(Request $request){
    ...

    return redirect('your_after_logout_page');
}
Nowdeen
  • 1,401
  • 14
  • 20