Jump to content

stuck redirecting url after login


benbyf
 Share

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;
        }
    }

 

Link to comment
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
Link to comment
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
 Share

×
×
  • Create New...