Redirect to url after login

Hi Everybody,

i try to redirect a client to a specific url, after he has logged in. In the cookbook, i find the solution, how to redirect a logged in user to home ("/"), but when i send my client a link to an unlisted page, which is password protected, i want him to redirect to this specific page.

i found this old topic: Authentication, private pages direct urls

But this was for Kirby 2, i think. I get the debugger, when i try to request to urlencode the clients path.

Is there a solution for Kirby 3?

Thank you very very much for your help.

Roland

1 Like

See this suggestion: Retain url parameter with routes - Kirby v2

It didn’t work for the TO because of redirects, but should work if you don’t have routes that remove the parameters.

Thank you Texnixe. Same Error as before.

<?php if (!$kirby->user()) go('/login') ?>

This code from the cookbook works fine, but

<?php if (!$kirby->user()) go('login?location=' . urlencode(kirby()->request()->path())); ?>

brings the debugger in front. Can it be, that my multilanguage-setup is the problem?

And what do you mean with: “if you don’t have routes that remove the parameters.”?

When i delete all my routes from config.php, the error is the same. (Or do you mean something other?)

go('login?location=' . urlencode(kirby()->request()->path()))

In a multilang environment, you would have to pass the language code in the URL before login. Do you use the language code for all languages including the default language?

Well, it would be helpful to know what the debugger wants to tell us :wink:.

You are right, here is the debugger:

“Do you use the language code for all languages including the default language?”

Do you mean, that in every url is the language embedded? Like “http://…/de/…” Yes, it is. I use english and german, and every url is with a “/de/” or “/en/”.

Oops, sorry, that was a Kirby 2 thread… Anyway, you need to get the current path…

:upside_down_face: It´s only 4 days young, but its Kirby 2. But no Problem.

What do others think about this “Security-Feeling”? (#poll) Do you need passwords for protected pages? Or do you use 500-signs-long-Urls?

I am a little bit old school, and i need passwords, to think, that my site is protected. But maybe i have to change my mind.

The correct way to get the path in Kirby 3:

$kirby->path()

Keep in mind that this string include the language code.

Other than that, you can go via the request object:

$path = $kirby->request()->url()->path()->nth(2)

nth(2) because the first part of the path is the language code.

Thank you, but where do i have to write this code? When i write it in the first line of my protected site,

<?php if (!$kirby->user()) go('login?location=' . $kirby->path()); ?>

no URL is given to the login-form.

input class=“input is-radiusless” name=“location” value=“<?php if(isset($_POST['location'])) { echo urldecode($_POST['location']);} ?>”

is empty (this input field is only shown, to proove, that it has the correct url)

Or do i have to write it in the controller?

$langCode = $kirby->language()->code();
go($langcode . '/login?location=' . urlencode(kirby()->path()->nth(2));

Then in the controller, get the location query and replace the redirect to the home page with the $redirect variable.

$redirect = get('location') ?? $site->url();
  if($site->user()) go($redirect);

Thank you Texnixe, you are great.

I have to try it in a clean Kirby-installation. On my Testsite now, this solution ends again with the debugger.

I think, i understood the way it should work, but maybe i have a mistake, anywhere in my code, or in the routes (?! i dont know). i have to find it first.

Thank you so much!!

I will report, if i find the way, how this could work.

OK, it works with this codes:
First lines in the site, which is protected:

<?php
$langCode = $kirby->language()->code();
$path = $kirby->request()->url()->path()->nth(1);
if (!$kirby->user()) go($langCode . '/login?location=' . $path); ?>

In the form of the login.page is a hidden field, that reads the $location:

<input type=“hidden” id=“locat” name=“locat”
value=“<?php if(isset($_GET['location'])) { echo urldecode($_GET['location']);} ?>” />

And in the controller, i read out the location and put it in the variable $redirect:

if ($kirby->request()->is(‘POST’) && get(‘login’)) {

$redirect = get(‘locat’); }

if ($kirby->user()) { go($redirect) ; }

And yes, this code can be optimized :wink: But for the moment, this works for me.

1 Like

Hello,

I’m also trying to to redirect to a specific URL after login and I’m facing a curious behaviour. This code opens the root page in a modal :

'hooks' => [
      'user.login:after' => function (Kirby\Cms\User $user, Kirby\Session\Session $session) {
        if ($user->isLoggedIn()) {
          go("/");
        };
      }
    ]

Hi @Adrieng, are you on Kirby 3.6.2 already? There was an issue (related PR and discussion here) that caused this kind of behaviour when redirecting to non-panel URLs up to 3.6.1.

Hi @sebastiangreger and thank you for answering.

Indeed I’m using Kirby 3.6.2. The fix is still on a development branch (I’m not very used to git / github workflows) ? No way to do it for the moment ?

Thanks

@Adrieng Hmm. That particular fix got merged into main before the release of 3.6.2, and was already released in that latest stable version. So there has to be another reason for your problem – sorry that wasn’t the solution!

I hope somebody else can point you in the right direction…

One more thought on this: would using the home option of the user blueprint be an alternative for your use case instead of employing a hook? (I somehow have a hunch this issue might have to do with the hook.)

The home option is the “official” way to do such redirects after login, and that one works in 3.6.2 as I have tested myself …anecdotally, that’s how we initially discovered that bug I referred to earlier.

1 Like

Interesting.
The problem is that I want to redirect to a variable url. I guess this way it’s not possible to get a data from the localStorage, isn’t it ?

That should be possible by using a custom site method. We had a discussion around such a use case a little while ago, maybe that can serve as an inspiration:

In a nutshell: You can set the home option to call a custom site method (e.g. home: "{{ site.logintarget }}"), where the site method logintarget should return the desired URL. In my solution for that specific scenario, I store the variable URL in the Kirby session, but you could of course use any other source for that URL to be returned.

2 Likes