Slav

Need help with expired session redirect...

Recommended Posts

Slav    2

Hey Guys! Im new to PW and am working on a website built on PW. Im trying to understand how sessions work in PW. Specifically what exactly is happening when session expires. The thing is that my client wants to be redirected to homepage whenever session expires, so basically he doesnt want to be redirected to admin login page when he's in the admin environment of PW(he doesnt want his clients to see the admin login page for whatever reason). Is it possible to hook to the session expiration and redirect to a specific url? And what is the correct way to do it in PW?

I would appreciate the help!

Cheers!

Share this post


Link to post
Share on other sites
kixe    808

@Slav Welcome to the forum. Take this as a first approach

 

$homeUrl = $pages->get(1)->url;
// Prevent redirect to admin login. Redirect to homepage instead
if (!$user->isLoggedin() && isset($input->get->login)) $session->redirect($homeUrl);
if ($user->isLoggedin() && isset($input->get->login)) $session->redirect($customLoginUrl);

// set custom login/ logout urls
$login = $modules->get('ProcessLogin');
$login->setLoginURL($customLoginUrl);
$login->setLogoutURL($homeUrl);

 

  • Like 1

Share this post


Link to post
Share on other sites
szabesz    1,377

Welcome Slav!

You might also be interested in this one:

Or this one:

Hope this helps.

  • Like 1

Share this post


Link to post
Share on other sites
Slav    2

Thank you guys for the replies!

@kixe

Okey this looks interesting. Where exactly should I use this snipet? And can you explain the logic little bit.. why is $customLoginUrl important there?

@szabesz

I checked the posts that you provided. I am already using this (in site/ready.php file):

wire()->addHookBefore("ProcessLogin::executeLogout", null, "setRedirect");
function setRedirect(HookEvent $event) {
   $event->object->setLogoutURL(wire('pages')->get('/')->httpUrl);
}

And it works great, but it only works when you perform Logout procedure (I needed that aswell though) not when the session expires.

In the other post they talk more about redirecting particular user to home page when he is performing login but its again not exactly what I am looking for. There is some interesting info about templates-admin "default.php" file... how does it work? Do I have to use default.php file? Whats the reason? Can anyone explain to a beginner?

 

Cheers!

Share this post


Link to post
Share on other sites
kixe    808
On 28.4.2017 at 9:02 AM, szabesz said:

Maybe @kixe is talkign about somthing like this (but let's ask him to clarify...):

The code I have posted is taken from a frontend login template. The frontend has a complete suite of register, login and logout templates. I use core processes in the frontend. To prevent redirects to the backend in case of successfull login/ logout or an expired session I set login and logout URLs before I execute the Process. The customers are PW users with very limited permissions.

I gave the admin area a very strange name (default: processwire) like '6gt0klw5a14' to hide it from guests or frontend users.
You can additionally protect the login with a custom cookie or IP filter in your .htaccess file.


Calling any target in the admin area without a valid session will cause render of the Login page instead. Its not recommended to change this.

  • Like 2

Share this post


Link to post
Share on other sites
Robin S    2,487

@Slav, another option is to set a redirect URL in the "Access" tab of the template that has restricted access. So if I user attempts to access a page with that template when they are not logged in (for any reason - either they logged out or their session expired) they are redirected to a page of your choosing.

2017-05-01_112745.png.b291e057204d987e39dd2f4df75357b7.png

  • Like 4

Share this post


Link to post
Share on other sites
kixe    808
4 hours ago, Robin S said:

@Slav, another option is to set a redirect URL

... but not in admin template. :P

  • Like 1

Share this post


Link to post
Share on other sites
Slav    2
On 1.5.2017 at 2:29 AM, Robin S said:

@Slav, another option is to set a redirect URL

Yeah... I would need to redirect out of the admin template... :/ 

 

On 30.4.2017 at 9:14 PM, kixe said:

The code I have posted is taken from a frontend login template.

I dont think that this is what I need... All I need is not to show the admin login page to the users (ever) if they dont use the direct link to the admin login page. So basically in frontend when users login, they can direct themselves to the admin environment page edit and when they logout from admin environment it will redirect them to frontend. So basically the admin login page is not needed (at least not needed to show to users). But the problem right now is that if user is in admin environment and is editing their page and if the session expires it redirects automatically to admin login page, but what I would need is when the session expires (in admin environment) then redirect to frontend. Usually it is possible to intercept or hook session expiration and do stuff according to that but I don't know how to do that in Processwire...

to sum it all up I need: When session expires redirect to frontend... I have an idea that I am going to try, so I will let you know if it works...

Share this post


Link to post
Share on other sites
Slav    2
On 30.4.2017 at 9:14 PM, kixe said:

Calling any target in the admin area without a valid session will cause render of the Login page instead. Its not recommended to change this.

@kixe Where is it rendered (what file)? I want to explore that part! Thanks!

Share this post


Link to post
Share on other sites
Slav    2

Ok so I managed to get the result that I was searching for! This is what I did:

In site/ready.php file I added this snippet:

wire()->addHookBefore('Page::loaded', function(HookEvent $event) {
    // Get the object the event occurred on, if needed
    $page = $event->object;
    if($page->template->name == "admin" && wire('user')->id === wire('config')->guestUserPageID){
        wire('session')->redirect(wire('pages')->get('/')->httpUrl);
    }
});

I'm not sure if it is the best way to do that, but I was unable to find a way through sessions so desided to try another approach! For me it does exactly what I needed because the website I'm working on has frontend login where you can navigate to page edit according to your permissions, so the admin login page is not necessary. Well at least that's what client wanted!

Anyway thanks guys for help!

  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By prestoav
      Now solved, see reply at the bottom.
      Hi all.
      PW version: 3.0.42
      Despite the usual speediness of PW, a site I'm working on at the moment is suffering an extremely slow load and save time in admin. We have the same site running on localhost (a brand new MacBook Pro with 3.1GHz processor / 16GB RAM) and a Media Temple VPS host both installations suffer in the same way.
      All pages open and save in admin very quickly with the exception of pages using one template which just happens to be the most used one (for products the site owner manufacturers).
      Opening the page to edit, or saving the page, can take more than a minute! The whole front-end of the site slows while this is happening. I should add that no caching is turned on at the moment, save the standard PW system. We are, however using the multi-language support with 6 languages installed.
      The template consists of the following number/type of fields:
      1x PageTitleLanguage  3x TextLanguage 4x Page 4x Repeaters (2-3 fields, typically text / image / textarea / page, in each - up to 10 entries per repeater) 1x RepeaterMatrix (3 fields in each, up to 3 entries per repeater) 2x TextArea 1x TextAreaLanguage 3x Image 8x Text Has anyone had an experience like this before? Is this expected behaviour with this number of fields in a page (it's the first time I've used this many on one template)?
      As always, any help would be gratefully received!
    • By mr-fan
      Like title says i've some domains with umlauts öäüß and i'd like to redirect them to the non umlaut version
      example:
      http://my-domän.de to http://my-domaein.de
      i've somethings like this in my htacess:
      ##redirect umlautdomain (not my domain) RewriteCond %{HTTP_HOST} ^xn--mhldorf-nsb.d$ [NC] RewriteRule ^(.*)$ http://muehldorf.de//$1 [R=301,L] in my config.php i only have muehldorf.de in my hosts list...
      is there something that i've misunderstood? so far it doesn't work....but may the htaccess rules take some time to exicute...i tried since about an hour some different rules.
      regards mr-fan
    • By hellomoto
      I have reduced the module to this and the same error persists:
      <?php class PortalDashboard extends Process implements Module { public static function getModuleInfo() { return array( 'title' => "Portal Dashboard", 'version' => "0.1", //'permission' => array("page-lister"), 'autoload' => false, 'singular' => true, 'permanent' => false, 'requires' => array("PHP>=5.4.0", "ProcessWire>=3.0", "CatCoCustoms"), ); } public function init() {} In the red:
      Furthermore
      When I uninstall the aforementioned module this doesn't happen, oddly enough. What could be the problem?
    • By noelboss
      This module hooks into the login method to provide the possibility to login with the user's email address. This is my first module and it's very simple. Please provide feedback if you have any suggestions. Thanks
      The module can be found in the module repository:
      https://modules.processwire.com/modules/login-with-email/
      or on github:
      https://github.com/noelboss/LoginWithEmail
       
    • By hellomoto
      Pressing problem at initial posting:  I am working on a module that runs/previews (for pub/unpub) imports on importer page view. The template has to be in site/templates, so far as I know, so one is created there on install which includes a file to run from within the module, but then when it's run and tries to read the importer csv file it cannot, since it's in the admin, I guess... Is it possible to set the template page to a file in the module directory? Will that work?
      https://bitbucket.org/chimmel/pw-import-everything