Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to logout via link #15

Open
tinogithub opened this issue Nov 23, 2023 · 4 comments
Open

How to logout via link #15

tinogithub opened this issue Nov 23, 2023 · 4 comments

Comments

@tinogithub
Copy link

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.

@johannschopplich johannschopplich changed the title 2 questions about the hook kirby()->trigger('locked-pages.logout') How to logout via link Nov 23, 2023
@johannschopplich
Copy link
Owner

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:

$access = App::instance()->session(['long' => true])->data()->get(LockedPages::SESSION_KEY, []);

@tinogithub
Copy link
Author

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.

@johannschopplich
Copy link
Owner

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

@gbdesign2023
Copy link

gbdesign2023 commented Apr 21, 2024

@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 ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants