Welcome Guest, Not a member yet? Register   Sign In
Ion Auth: How to display the current username in input form?
#1

[eluser]qpixo[/eluser]
I started using Ion Auth examples codes that comes with package and want to make a custom Login form. I'd like know how to keep and display the current username in this input form after user has clicked on 'Submit' button and validation message is wrong?

Ex: In situation that he doesn't have a password correct

I can't see to return any current input->post('username')

I'm running on Mac with MAMP. Is there anything to do with _POST?
#2

[eluser]rip_pit[/eluser]
Quote:when the login was un-successful, redirect the user back to the login page

Because of the redirect, you will have to save the datas in a session or in a DB.

The easiest way would be to save the username as a Flash variable (see Flash session) : it'll then be available once, in next page

if login fails, save username using :
Code:
$this->session->set_flashdata('username', set_value('identity'));

then at next load read it from flash session using :
Code:
$this->session->flashdata('username')

More info : http://ellislab.com/codeigniter/user-gui...sions.html
#3

[eluser]gbd_dee[/eluser]
Im having the same issue
#4

[eluser]qpixo[/eluser]
@rip_pit

Thanks a lot for your help! After I did the change it works great. The issue was really redirect() thing... Smile

I'd like to ask you, what is the difference between set_userdata() vs set_flashdata() for session. They seem doing the same task.
#5

[eluser]qpixo[/eluser]
[quote author="gbd_dee" date="1335823639"]Im having the same issue[/quote]

In case you need an example, here's the current code that works for me after the change:

Code:
public function login() {
  // Validated login form
  $this->form_validation->set_rules('username', 'Username', 'required');
  $this->form_validation->set_rules('password', 'Password', 'required');
  
  // Get user input form
  $username = $this->input->post('username');
  $password = $this->input->post('password');
  
  // If it can log in
  if ($this->form_validation->run() == true) {
   // User success login
   if ($this->ion_auth->login($username, $password)) {
    $this->session->set_flashdata('message', $this->ion_auth->messages());
    
    redirect($this->config->item('base_url'), 'refresh');
   } else {
    // Save username
    $this->session->set_flashdata('username', set_value('username'));
    
    // Display error
    $this->session->set_flashdata('message', $this->ion_auth->set_error('Invalid username or password.'));
    
    redirect('auth/login', 'refresh');
   }
  } else {
   $test = $this->session->flashdata('username');
  
   $data['username'] = array(
          'name' => 'username',
          'type' => 'text',
          'value' => $this->form_validation->set_value('username', $test) );
  
   $data['password'] = array(
          'name' => 'password',
            'type' => 'password' );
  
   $data['submit'] = array ( 'type' => 'submit',
          'value' => 'Login' );
  
   $data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
  
   // Display login page
   $this->load->view('login', $data);
  }
}
#6

[eluser]rip_pit[/eluser]
[quote author="qpixo" date="1335889770"]difference between set_userdata() vs set_flashdata() for session[/quote]

the main difference is that flash data will be available only one time in your next page, then it will be auto cleared.

Unlike Userdata that is a more classic session, the datas you save with it wont be destroyed unless you ask for it.

Help says :
Quote:CodeIgniter supports "flashdata", or session data that will only be available for the next server request, and are then automatically cleared. These can be very useful, and are typically used for informational or status messages (for example: "record 2 deleted").
#7

[eluser]qpixo[/eluser]
[quote author="rip_pit" date="1335892416"][quote author="qpixo" date="1335889770"]difference between set_userdata() vs set_flashdata() for session[/quote]

the main difference is that flash data will be available only one time in your next page, then it will be auto cleared.

Unlike Userdata that is a more classic session, the datas you save with it wont be destroyed unless you ask for it.

Help says :
Quote:CodeIgniter supports "flashdata", or session data that will only be available for the next server request, and are then automatically cleared. These can be very useful, and are typically used for informational or status messages (for example: "record 2 deleted").
[/quote]

Ok thanks for the explanation

See the code that I posted above is it right way to do?

#8

[eluser]rip_pit[/eluser]
[quote author="qpixo" date="1335901285"]

Ok thanks for the explanation

See the code that I posted above is it right way to do?

[/quote]
code seems fine Wink as i tweaked my ion auth, i can't test it but it seems good.

Does it work for you ?
#9

[eluser]qpixo[/eluser]
[quote author="rip_pit" date="1335915334"][quote author="qpixo" date="1335901285"]

Ok thanks for the explanation

See the code that I posted above is it right way to do?

[/quote]
code seems fine Wink as i tweaked my ion auth, i can't test it but it seems good.

Does it work for you ?[/quote]

Yes it works great!! Smile

I was just wonder if I could make it cleaner though...
#10

[eluser]bill19[/eluser]
Hi Guys,

I am using the Ion_Auth library, and I'm very new to forms, so if I may, I'd like to ask a basic question using the code you've already posted.

when I run your public function login(), I get the following output:

Quote:Login

Please login with your email/username and password below.

Email/Username:
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: identity

Filename: auth/login.php

Line Number: 13

I'm getting the same error with my forms. What am I doing wrong?

Thank you in advance,

Bill





Theme © iAndrew 2016 - Forum software by © MyBB