Redirect to home page instead of login page after logout


#1

Hi,

My question is in the title :wink:

How to redirect the user on the hompage instead of the login page when the user logout from the panel?


#2

You’d have to modify the core files. This is the logout method where the redirect happens:

  public function logout() {

    if($user = panel()->user()) {
      $user->logout();
    }

    $this->redirect('login');

  }

(/panel/app/controllers/auth.php)


#3

May I ovrerwrite this function with a plugin? I would prefer not to directly modify the Kirby core.


#4

Hm, you can’t just overwrite the method, because it belongs to a class, so the only thing you could do is extend the class, but that would not help you.

I wonder if hijacking the logout route would be a way to achieve that, see this example of form hijacking https://github.com/lukaskleinschmidt/kirby-sortable-events/tree/modal/actions/event


#5

Thanks Sonja, I will check this out :slight_smile:


#6

Hi there @gillesvauvarin did you find a workaround for this? If so I am curious if you can share it?


#7

Nop sorry, I didn’t find solution :-/


#8

This seems to work:

<?php
if(!function_exists('panel')) return;

panel()->routes(array(
  array(
    'pattern' => '(logout)', // the trick here is the parens around the route pattern
    'method'  => 'GET|POST',
    'filter'  => array('auth'),
    'action'  => function() {
      if($user = panel()->user()) {
        $user->logout();
      }
      go('/');
    
    },
  ),
));

Thanks to @lukaskleinschmidt’s post


#9

Thank you @texnixe and for that you are always on here in the forum, this solution worked like a perfectly . :smile: :star_struck: :sunglasses:

Just add this to your site/config/config.php

Check out this solution @gillesvauvarin