Reputation: 103
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
Upvotes: 10
Views: 10637
Reputation: 1440
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');
}
Upvotes: 0
Reputation: 161
In your config/fortify.php
, add:
'redirects' => [
'logout' => 'login',
],
Upvotes: 16
Reputation: 779
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.
Upvotes: 0
Reputation: 8242
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
);
}
Upvotes: 19