kirby-locked-pages icon indicating copy to clipboard operation
kirby-locked-pages copied to clipboard

How to logout via link

Open tinogithub opened this issue 1 year ago • 4 comments

Hello,

thank you for your plugin. I am trying to implement a logout-Link in my navigation-bar but I come around to questions:

  1. How do I call kirby()->trigger('locked-pages.logout'); with a link?
  2. I want that link only to appear if user is looked in.

I assumed to do something like this but I get an error "Call to undefined function isLocked()" <?php if($page.isLocked()):?><a href="<?= kirby()->trigger('locked-pages.logout') ?>">Logout</a><?php endif?>

Do I have to register locked-pages somewhere in my templates first?

I am still new to kirby-plugins and I couldn't find an answer in the forum or kirby help pages. Thank you for help.

tinogithub avatar Nov 23 '23 16:11 tinogithub

Hello there! Sorry, I don't have time to help you. But I can point you in the right direction. You can read the Kirby documentation on session management and use the session variable to check if the user is logged in to a particular page:

https://github.com/johannschopplich/kirby-locked-pages/blob/6bcacb2be481df71ac35efade0e6f9ab5cd4f97e/LockedPages.php#L27

johannschopplich avatar Nov 23 '23 16:11 johannschopplich

This was quick. I have already stepped over this part however I am still confused. But I will have a look at it again and maybe find answers in the forum.

Thank you.

tinogithub avatar Nov 23 '23 16:11 tinogithub

Feel free to post the answer here once you find it. 🙂

johannschopplich avatar Nov 23 '23 16:11 johannschopplich

@tinogithub I was also looking for a practical example. It's a pity that it's missing from the documentation here. I am not a Full Stack Developer, but I've found a solution that works:

site/config/config.php

'routes' => [
	[
	'pattern' => 'locked-pages-logout',
	'action'  => function () {
	kirby()->trigger('locked-pages.logout');
	go('home');
	}
	]
],

Unfortunately, I was unable to redirect to the original login page. Instead, however, a corresponding page can be created that confirms the logout.

And this is the code for the logout button. It is only displayed in the navigation if a visitor is logged in. If a user is logged into the panel, these sessions are not terminated.

<?php if ($kirby->session(['long' => true])->data()->get(\JohannSchopplich\LockedPages::SESSION_KEY, [])): ?>
<a href="<?= url('locked-pages-logout') ?>">Logout</a>
<?php endif ?>

I am happy to receive suggestions for optimization from expert programmers ;-)

gbdesign2023 avatar Apr 21 '24 14:04 gbdesign2023