Frontend login from any page (via a shared plugin or controller)?

The frontend authentication solution described in the docs works perfectly, but I’d like to allow my site’s users to log in from any page within the site (homepage, article, archive, etc.) via a modal dialog box instead of navigating to the site’s /login page first.

I’ve seen @jenstornell’s multiple posts on this topic, but the solution doesn’t seem to allow $user->login($password) to work from anywhere.

I’ve also seen this post about creating a controller for a snippet, and it’s suggested that a plugin be created instead with most of the same functionality. Unfortunately I haven’t been able to get frontend authentication to work with that method either, although my limited knowledge of PHP (outside of what Kirby has taught me) may be the cause of that.

Is there any way to log users in from any page without duplicating code for every page template/controller?

Link to the Docs solution for reference. Couldn’t post more than 2 links in one post.

I use a custom route mapped to /login. See line 171 of this config file. https://github.com/samnabi/shopkit/blob/master/site/config/config.php

Note that the frontend login doesn’t work if you have caching turned on. Kirby will just serve the cached page as it was before you logged in.

Edit: for clarity, /login is the endpoint of a form that is included as a snippet on each page. So it works with a snippet and a route.

Thank you @samnabi, that worked. Adding cache::flush(); (mentioned here) to the function seems to be a workaround, but flushing the whole cache seems wasteful. This will do for now. Thanks again!

Hello, i am just reading your answer because i also have the same issue. Unfortunately in your github file, the line 171 is not existing anymore. Would you care to post just the line of the routing here?

Thanks,
Matthias

The new location for that route is Line 9 here: https://github.com/samnabi/shopkit/blob/master/site/plugins/shopkit/registry/routes.php#L10

Thanks a lot!
You are doing the shopkit solution? Thats big on my list of improvements on my site… I still have to look into it :slight_smile:

1 Like