benbyf

stuck redirecting url after login

Recommended Posts

Sure this has been asked a million times but cant find a simple answer.

Trying to set the page to redirect to after login in the front end - i.e. not on the admin login page.

some reason this works for super users but not normal users redirecting to a page which is viewable by members not guest role users.

 

$showLoginError = false;
    if($input->post->user && $input->post->pass) {
    	$username = $sanitizer->username($input->post->user);
    	$pass = $input->post->pass;

        try {

			$session->login($username, $pass);

			// redirect user
			$session->redirect("/events/");

        } catch (\Exception $e) {
            $showLoginError = true;
        }
    }

 

Share this post


Link to post
Share on other sites

But what happens with the normal users?

Can normal users view the page "/events/" ? Is the url correct? Can you get the url by API instead?

Share this post


Link to post
Share on other sites

In my _init.php file, the first thing I include is _login.php:

<?php

// Handle logouts
if(isset($input->get->logout)) {
    $session->logout();
    $session->redirect($page->path);
}

// If they aren't logged in, then show the login form
if(!$user->isLoggedin()) {

    // check for login before outputting markup
    if($input->post->user && $input->post->pass) {

        $user = $sanitizer->username($input->post->user);
        $pass = $input->post->pass;

        if($session->login($user, $pass)) {
            // login successful
            $session->redirect($page->path);
        } else {
            $session->login_error = 'Login Failed. Please try again, or use the forgot password link below.';
        }
    } ?>

    <!DOCTYPE HTML>
    <html lang="en">
    <head>
        <title>Login</title>
        <link rel="stylesheet" href="/build/css/app.css">
    </head>
    <body>
        <div class="container py-5">
            <form action='./' method='post'>
                <div class="row">
                    <div class="col-12 col-sm-6 offset-sm-3">
                        <h1>You must be logged in.</h1>
                        <div class="login">
                            <? if($input->post->user && $input->post->pass) {
                                echo "<p class='error'>" . $session->login_error . "</p>";
                            }?>
                            <p><input class="form-control" type='text' id="user" name='user' placeholder='Username'/></p>
                            <p><input class="form-control" type='password' id="pass" name='pass' placeholder="Password" /></p>
                            <p><input type='submit' class="btn btn-primary" name='submit' value='Login' /></p>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </body>
    </html>

    <? die(); // don't go any further if not logged in

}

If I go to site.dev/whatever, I am met with a login. After logging in, it takes me straight to the URL I intended to go to before needing to log in.

You could change the $page->path to $pages->get('/events/')->httpUrl; - would that work?

  • Like 1

Share this post


Link to post
Share on other sites

thanks @ethanbeyer. its so unbelievebly strange, it must be server settings as it works fine as I have currently on dev but not on live. Thanks anyways though!

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 cosmicsafari
      Hi all,
      This is my first foray into a multi lingual PW site, what i'm unsre about is whether the frontend output is translated automatically or do I have to provide the content in another language also?
      As it stands I have installed the following modules:

      And have created and uploaded the Chinese language pack.

      At this point I can successfully change my profile and the backend successfully displays in Chinese.
      However I am confused about how I now get that translation to work on the frontend?
      I have setup the alternative url for the page I wish to view in Chinese

      So as a basic test I tried adding the following into the template used for the page above.

      However both urls still display the message in English?
      I take it I have missed something, do I then need to enter the frontend output in Chinese in another field within the backend to enable Chinese url to output the content?
      Any ideas would be greatly appreciated.
       
    • By Marco Angeli
      Hi there,
      I added a ssl certificate to my site and I'd like to redirect every single http url to its new https version
      So I added this code in the .htacces file, after the RewriteEngine On :
      Redirect 301 /about https://www.mysite.it/about
      Unfortunately this is now working: I get the "too many redirects" error.
      The following code works, but it's a bulk redirection to the home page, something I don't want for SEO reasons (https://moz.com/blog/save-your-website-with-redirects😞
      RewriteCond %{HTTP_HOST} mysite\.it [NC]
      RewriteCond %{SERVER_PORT} 80
      RewriteRule ^(.*)$ https://www.mysite.it/$1 [R,L]
      Any suggestions?
    • By matsn0w
      Hey all,
      I am working on a website and I want to style the login page, but I'm a bit confused. 
      I want either the existing login page styled in my own way using some CSS (I guess I prefer that) or I want to create a custom page with a form to login. (Which I could style too).
      I used the code from Ryan and Renobird posted here - which works great - but that doesn't replace the original login page. 
      Is there a way to some sort of 'disable' the original login?
      I hope my question is clear and thanks in advance,
      matsn0w
    • By louisstephens
      So, I really don't know where to begin with this. I have the thought in my head (the basics), but not really sure how to set it up.
      The desired effect:
      The homepage is set up with a foreach loop that outputs all the child pages (basically just a bunch of pages with pdfs, docs etc stored in a file field). When A user is actually logged in, they can "star" an item or drag and drop (using Dragula) the page to a sidebar. Also, they would be able to reorder the pages in the sidebar.
      The question(s):
      I guess I have multiple questions regarding how to achieve this.
      Would it be best to create a page field in the user template to save the page(s)? How would you actually save the page state (sidebar output, reordering, etc) once the drag and drop has been used? Thanks in advance for any help! A lot of this was just what was in my head, so it might not make full sense. However, I will update this if I can actually wrap my head around everything.
    • By Lex Sanchez
      Hi everyone:
      I do not know if someone before using ProcessWire with AWS CloudFront, currently I have problems with the login, it does not work for any reason, when I check in the logs generated by ProcessWire, it only indicates This request was aborted because it appears to be forged. (in /wire/core/SessionCSRF.php line 190).
      I have allowed CloudFront to forward all headers, cookies and allow all methods (GET, POST, PUT).
      When I perform the same process from the ip server if it works or from the balancer.