Jump to content

session after ajax login


martind
 Share

Recommended Posts

hi,

i have a login form on my page which is handled within a template called by ajax. Login works fine, but only if the site doesn't get reloaded - which isn't the intention but may occur. So is there a way to manually reset the session data from within my form-handler template after successfully calling $session->login($username, $password), to have it working after a page reload?

thanks, martin

Link to comment
Share on other sites

to further describe this a bit.

home.php

$form_login = "<form id='loginForm' class='uk-form' action='/proc/login/' method='post'>\n";
$form_login .= "<fieldset data-uk-margin>\n";
$form_login .= "<legend>Login</legend>
	<input type='text' name='login_name' placeholder='E-Mail'>
	<input type='text' name='password' placeholder='password'>
	<input type='submit' class='uk-button' value='Login' />\n";
$form_login .= "</fieldset>\n";
$form_login .= "</form>\n";

xx.js

$('#loginForm').ajaxForm({
  dataType:		'json',
  success:		processFormSuccess
});
function processFormSuccess(data) {
  // 'data' is the json object returned from ajax.php
  if(data.statusCode == 2)
  {
    $('#login_content').html(data.responseText);
    $('#reg_login').html(data.username);
    $('.hide_loggedin').hide();
    $('.show_loggedin').show();
  }else{
    $('#reg_messages').html(data.responseText).fadeIn('fast');
  }
}

ajax.php

if($input->urlSegment1 == "login"){
  $r = array(
  "username" => "",
  "userid" => "",
  "email" => "",
  "responseText" => "",
  "einreichCode" => "",
  "statusCode" => 0
  );

if ($input->post->login_name && $input->post->password) {

  $username = $input->post->login_name;
  $password = $input->post->password;				

  // find user with email
  $ufind  = $pages->get('template=user, email='.$sanitizer->email($username));
  if($ufind->id)
  {
  try{
      $u = $session->login($ufind->name, $password);
      if ($u->id) {
        $log->save('login', "Login by {$u->screen_name} ({$u->id}).");
        $r['responseText'] .= "<p>welcomoe back, {$u->screen_name}.</p>";
        $r['einreichCode'] = $password;
        $r['email'] = $u->email;
        $r['userid'] = $u->id;
        $r['username'] = $u->screen_name;
        $r['statusCode'] = 2;       

      } else {
        $log->save('login', "Login fault");
        $r['responseText'] .= "<p>Falsche E-Mail <strong>>>{$username}<<</strong> oder Einreich-ID <strong>>>{$password}<<</strong>. Login fehlgeschlagen.<br >
          Versuchen Sie es erneut oder kontaktieren Sie uns unter <a href='mailto:admin@xx.net'>admin@xx.net</a></p>";
      }
    } catch(Exception $e){ $log->save('login', $e->getMessage());  }
  }
}else{
	$log->save('login', "Fehler. Keine Login Daten.");
}
$response = json_encode($r);
echo $response;			
}

 

... so, everything of this works as expected, the js function switches my home-site to logged-In mode, even when i directly call my pw-admin, the user is logged-in, but when reloading the home-site, it is not. Any way to effect this?

thanks,
martin

Link to comment
Share on other sites

It seems like you need to handle the "online" mode on your home.php template. You only update the content after submitting the form, if you directly visit the homepage, you will still see the login form even though you have a valid session. So you need to differentiate two cases in home.php:

  1. Not logged in: Display Login form
  2. Already logged in: Display "online" mode, hiding login etc. Basically the same as you're doing in javascript after successful login

Cheers

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