2

I have created a Laravel 9 application with Livewire and there I have a logout button which use Auth::logout() and redirect to the login page. I have set up everything correctly as I understand but the button doesn't work.

Function in Profile.php

public function logoutUser() {

        Auth::logout();

        return redirect(route('auth.login'));

    }

Logout button in profile.blade.php

<button wire:click="logoutUser" class="block px-4 py-2 mt-2 text-sm bg-white md:mt-0 focus:text-gray-900 hover:bg-indigo-100 focus:bg-gray-200 focus:outline-none focus:shadow-outline">Logout</button>

What is the issue here? TIA!

pasindu
  • 529
  • 1
  • 4
  • 16
  • 2
    What happens when you click the button, exactly? I'm guessing it's a session issue, and that you should just make a POST request to a normal controller instead – Qirel Sep 05 '22 at 09:35
  • 1
    @Qirel It does nothing actually. So you recommend to make a normal controller and logout using that. – pasindu Sep 05 '22 at 11:02
  • 2
    Yes, because it'll have to destroy the session, and working with session in Livewire is difficult because they are api requests – Qirel Sep 05 '22 at 11:16

1 Answers1

1

As @Qirel recommended I used a regular controller instead of a Livewire controller to handle the logout. Followed this answer for implement that correctly.

pasindu
  • 529
  • 1
  • 4
  • 16