Jump to content

custom login from modal box, redirect after login failure


AnotherAndrew
 Share

Recommended Posts

I am using fancybox to load a custom login. And it works in the sense that if I call target='_parent' on the form action then the modal box closes and redirects to the member page.

But what I want to happen, is if there is an error, then the error shows in the fancybox modal box. In order for the error to show I have to not specify a target in the form action. However, if login info is correct, then it redirects to the members page in the fancybox modal box. What I want is for the modal box to close and the user go to the page in the parent frame.

Does anyone know how I can achieve this?

This is how I am calling the login script:

				<div id="esns-login-wrapper">
				<p><a class="iframe" href="/login/">Login</a> / <a href='./?logout=1'>Logout</a></p>
				<?php
				if($input->get->logout == 1) {
					$session->logout();
					$session->redirect("/"); // start them on a fresh page, or redirect to another
				}?>
			</div>

This is what I am using for fancybox

$('a.iframe').fancybox({
maxWidth	: 350,
maxHeight	: 300,
fitToView	: false,
width		: '350',
height		: '300',
autoSize	: false,
closeClick	: false,
openEffect	: 'none',
closeEffect	: 'none',
showCloseButton: false,
'type' : 'iframe',
padding: 0,
margin: 0,
scrolling: 'no',
showCloseButton: false		
});

And this is what I am using for my login script:

<?php

// $out is where we'll keep our output
$out = '';

// $form is where we'll keep our custom login form
$form = "
    <form action='./' method='post' id='login-form' >
        <p><label>Username <input id='input-name' class='text-input required default' type='text' name='user' title='Login name'/></label></p>
        <p><label>Password <input id='input-password' class='text-input required default' type='password' name='pass' title='Password'/></label></p>
        <p><input type='submit' name='submit_login' value='Login' id='submit'/></p>
    </form>";

if($user->isLoggedin()) { 
    // user is already logged in 

    if($input->get->logout) {
        // page was accessed with ?logout=1 GET variable, so log them out
        $session->logout();
        $session->redirect('./'); 
    } else {
        // tell them they are logged in and how to logout
        $out = "<p>You are logged in. <a href='./?logout=1'>Logout?</a></p>";
    }

} 
else if($input->post->user && $input->post->pass) {
    // user submitted the login form

    if($session->login($input->post->user, $input->post->pass)) {
        // based on user roles where to redirect them
	if($user->isSuperuser()) {
		$session->redirect('./'); 
        } else { 
        	$session->redirect('/members/');     
        }
    } else {
        // the login failed
        $out = "<p class='error'>Login failed. Try again.</p>";
        $out .= $form; 
    }

} 
else {
    // user arrived at login page for first time
    $out = $form; 
}

echo $out; 
Link to comment
Share on other sites

I'm not sure if it's the best way to go, but you could just combine what you are already doing into two steps. Have the form submit without the _parent target so that it stays in fancybox. But if login is a success, then use that auto-closing behavior that you mentioned by submitting another form to the _parent. I've not tried this, so I'm just going on trying to take advantage of the existing behavior you mentioned:

<?php

if($session->login($input->post->user, $input->post->pass)) {
    // login success

    if($user->isSuperuser()) $url = './';
        else $url = '/members/';     

    $out = "<form id='success' action='$url' target='_parent' method='get'>" . 
           "<input type='hidden' name='login' value='1' />" .
           "</form>" . 
           "<script>$(document).ready(function() { $('#success').submit(); }); </script>"; // submit the form
} else {
    // display error
}

Note I included that hidden form field called 'login' above in case you want your _parent frame to look for and display a "you logged in" message, or something like this:

<?php
if($input->get->login && $user->isLoggedin()) echo "<h2>Welcome {$user->name}!</h2>";
Link to comment
Share on other sites

Ryan, thanks for the idea.

The error works correctly in the modal box but the modal box does not close if the login is correct. If the login is correct, the modal box stays open and does not redirect to the appropriate page.

Do you have any other suggestions?

Based on your comments above, this is my code:

<?php

// $out is where we'll keep our output
$out = '';

// $form is where we'll keep our custom login form
$form = "
    <form action='./' method='post' id='login-form' >
        <p><label>Username <input id='input-name' class='text-input required default' type='text' name='user' title='Login name'/></label></p>
        <p><label>Password <input id='input-password' class='text-input required default' type='password' name='pass' title='Password'/></label></p>
        <p><input type='submit' name='submit_login' value='Login' id='submit' /></p>
    </form>";

if($user->isLoggedin()) { 
    // user is already logged in 

    if($input->get->logout) {
        // page was accessed with ?logout=1 GET variable, so log them out
        $session->logout();
        $session->redirect('./'); 
    } else {
        // tell them they are logged in and how to logout
        $out = "<p>You are logged in. <a href='./?logout=1'>Logout?</a></p>";
    }

} 
else if($input->post->user && $input->post->pass) {
    // user submitted the login form

	if($session->login($input->post->user, $input->post->pass)) {
		// login success

		if($user->isSuperuser()) {
			$url = './';
		} 
		else {
		$url = '/members/';     
		}
		$out = "<form id='success' action='$url' target='_parent' method='get'>" . 
			   "<input type='hidden' name='login' value='1' />" .
			   "</form>" . 
			   "<script>$(document).ready(function() { $('#success').submit();  }); </script>"; // submit the form
	} else {        // the login failed
        $out = "<p class='error'>Login failed. Try again.</p>";
        $out .= $form; 
    }

} 
else {
    // user arrived at login page for first time
    $out = $form; 
}

echo $out; 
Link to comment
Share on other sites

  • 1 month later...

Hey Ryan,

I've got this working just almost like I want it to. But for some reason, I can't redirect my superuser to any page except for my "members" page. My code is below. Can you or anyone else lend some advice? Thanks in advance!

<?php
// $out is where we'll keep our output
$out = '';
// $form is where we'll keep our custom login form
$form = "
   <form action='./' method='post' id='login-form' target='_parent' >
    <p><label>Username <input id='input-name' class='text-input required default' type='text' name='user' title='Login name'/></label></p>
    <p><label>Password <input id='input-password' class='text-input required default' type='password' name='pass' title='Password'/></label></p>
    <p><input type='submit' name='submit_login' value='Login' id='submit' /></p>
   </form>";
if($user->isLoggedin()) {
   // user is already logged in
   if($input->get->logout) {
    // page was accessed with ?logout=1 GET variable, so log them out
    $session->logout();
    $session->redirect('./');
   } else {
    // tell them they are logged in and how to logout
    $out = "<div class='logout-error'><p class='logged-in-out'>{$user->name}, you are already logged in. Do you want to <a href='../../?logout=1' class='logged-in-out' target='_parent'>logout?</a></p></div>";

   }
}
else if($input->post->user && $input->post->pass) {
   // user submitted the login form
 if($session->login($input->post->user, $input->post->pass)) {
  // login success

 if($user->isSuperuser()) {
  $session->redirect('../../');
    } else {
	 $session->redirect('/members/');	
    }
  $out = "<form id='success' action='$url' target='_parent' method='get'>" .
   "<input type='hidden' name='login' value='1' />" .
   "</form>" .
   "<script>$(document).ready(function() { $('#success').submit();  }); </script>"; // submit the form
 } else {	    // the login failed
    $out = "<p class='error'>Login failed. Try again.</p>";
    $out .= $form;
   }
}
else {
   // user arrived at login page for first time
   $out = $form;
}
echo $out;
Link to comment
Share on other sites

This is my mistake, I sent in you in the wrong direction with regard to $user before. (Sorry about that)

$user is actually still the one that was originally supplied to the template (guest). So you need to use the $user returned by the login function. Change your login line to be this:

$user = $session->login($input->post->user, $input->post->pass);
if($user) {
// login success
  if($user->isSuperuser()) {
	// now this should work
  }
}

The only reason this is necessary is because $user was supplied to the template before the login occurred, so that user is still 'guest' unless you change it (like above).

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...