Redirect to url after login

#1

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
#2

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.

#3

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?)

#4
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:.

#5

You are right, here is the debugger:

#6

“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/”.

#7

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

#8

: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.

#9

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.

#10

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?

#11
$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);
#12

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.

#13

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.