Welcome Guest, Not a member yet? Register   Sign In
Cleaning up reset password module
#1

[eluser]RalphLeMouf[/eluser]
Hello -

I am creating a reset password module for when a user forgets their password. I am actually able to get it to work properly, however I am still getting notices and errors and am a little suspicious of the durability of it all.

I am also not sure I'm retrieving the get value (token) properly from the url as I am trying to compare that to what is stored in the users row in the db.

Here is a link to my module.

I am still fairly new to codeigniter and really appreciate any help.

Thanks in advance
#2

[eluser]apodner[/eluser]
What kind of notices are you getting? More often than not if I get one that is related to a model it is because I forgot to declare a property in the class. If it is a notice in a view or controller, I didn't initialize the variable in the controller (like it is set in the if clause, but not in the else, and the notice gets thrown when the view loads.

for the token:
Code:
$this->input->get('token')

This is better than directly accessing the super global.

Have you considered using the CI form helper class?
http://ellislab.com/codeigniter/user-gui...elper.html
It meshes very well with the form validation class and also automates CSRF protection if you have it turned on, which for a password function, is probably the way to go.

if you are getting notices in the "retrieve" page, it looks like you are setting the value of the $data['email_error'] after the view loads, so the view is not receiving that array element. If you are echoing that element, that could be where your trouble is.
#3

[eluser]RalphLeMouf[/eluser]
Hey thanks so much for your response first off!

These are the notices I am getting:

A PHP Error was encountered
Severity: Warning
Message: Missing argument 1 for Auth::reset_password()
Filename: controllers/auth.php
Line Number: 193
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: user
Filename: controllers/auth.php
Line Number: 210
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: controllers/auth.php
Line Number: 210

And this is the reset_password controller with the changes since the original gist I showed you

Code:
public function reset_password($data)
  {
   $this->load->library('form_validation');
   $this->load->library('session');
   $this->load->model('user_model', 'um');
   $this->load->library('encrypt');
   $this->load->helper('url');
   $this->form_validation->set_rules('password', 'Password', 'trim|required');
   $this->form_validation->set_rules('password2', 'Confirm Password', 'trim|required|matches[password]');
   $salt = $this->_salt();
   $submit = $this->input->post('submit');
   $token = $this->input->get('token');
  

   if($submit)
   {

    if($this->form_validation->run() == TRUE && $token == $user->token)
    {

    $this->um->reset_password(array('password' => $this->input->post('password', $salt)));


    $data['main_content'] = 'auth/success';
    $this->load->view('includes/templates/home_page_template', $data);

    }
    $this->form_validation->run() == FALSE;
   }
  }

Still being new at codeigniter and MVC all together, I guess I'm wondering about the relationship of communication that models and controllers have with eachother. I've noticed some of the errors / notices I've gotten are undefined variables or objects I've created on the model and I try to use on the controller and are not coming through as defined.
#4

[eluser]apodner[/eluser]
Ok,

So your reset_password function is declared with a parameter ($data). It appears that the data coming into the method originates from the $_POST superglobal, and the only thing you are doing with the $data variable is setting the value of the 'main_content' element and passing it to the view. That being the case, you should be able to take $data out of the declaration:

Code:
public function reset_password()

Where you have $user->token, I cannot see in the code where you have instantiated a class and assigned it to the $user object variable. Maybe you meant $this->um->token?

The fact that $user is not an instantiated object is causing both of the other errors.

Hope this helps.

As far as relationship, think of it this way. All the processing and work is done in the models. From the controller, you send a model the parameters, call its method, and it send you back the processed data. Then you take that processed data and send it to the view.

Think of it this way, if the model was an artist, like a sculptor or a painter, and the view was like an art gallery where people look at the paintings, then the controller would be the street that connects the studio to the gallery and the truck that picks up and drops off the painting. The controller will ideally just shuffle information back and forth between views and models, selecting which views and models are appropriate given the data coming from either direction. That is really a crude & simplified version of it, but it is the best way I can think of to illustrate it at the moment.
#5

[eluser]RalphLeMouf[/eluser]
That REALLY helps. I really appreciate your valuable, informative, helpful responses. You rule!

So if I instantiate my $user class in the model ( which I thought I did,if I didn't how do I do that correctly? ) Then I can use it on the controller without having to define $user in a new way on the said controller?

#6

[eluser]apodner[/eluser]
Is your user class the same thing as the user model?
#7

[eluser]RalphLeMouf[/eluser]
Not that I am aware of. I'm not really sure what you mean by that either. I'm trying to create the user class IN my user model ( albeit being created in function ) to be able to use on my controller and not be undefined.
#8

[eluser]apodner[/eluser]
Can you do me a favor and post the code of your user_model class? It is the class being referred to in $this->load->model('user_model', 'um').
#9

[eluser]RalphLeMouf[/eluser]
so I've gone and made those changes you suggested and this is what I have now and the errors it's posting:

A PHP Error was encountered
Severity: Warning
Message: Missing argument 1 for Auth::reset_password()
Filename: controllers/auth.php
Line Number: 193

A PHP Error was encountered
Severity: Notice
Message: Undefined property: Auth::$token
Filename: core/Model.php
Line Number: 51

//MODEL
Code:
public function reset_password()
   {
    $salt = $this->_salt();
    $query = $this->db->get('reset', 1);

    foreach ($query->result() as $row)
    {
        echo $row->token;
     echo $row->email;
     echo $row->id;

    }

    $data = array(
                   'password' => $this->encrypt->sha1($salt . $this->encrypt->sha1($this->input->post('password'))),
                   'salt' => $salt
                );
    $this->db->where('email', $row->email);
    $this->db->update('users', $data);
   }

//CONTROLLER

Code:
public function reset_password($data)
  {
   $this->load->library('form_validation');
   $this->load->library('session');
   $this->load->model('user_model', 'um');
   $this->load->library('encrypt');
   $this->load->helper('url');
   $this->form_validation->set_rules('password', 'Password', 'trim|required');
   $this->form_validation->set_rules('password2', 'Confirm Password', 'trim|required|matches[password]');
   $salt = $this->_salt();
   $submit = $this->input->post('submit');
//I AM TRYING TO GET THE TOKEN IN THE URL THAT IS SENT WITH THE LINK TO COMPARE TO THE TOKEN IN THE RESET DATABASE THAT THE USERS EMAIL IS ASSOCIATED WITH ( via $this->um->token in the below if statement)
   $token = $this->input->get('token');
  
  

   if($submit)
   {

    if($this->form_validation->run() == TRUE && $token == $this->um->token)
    {

    $this->um->reset_password(array('password' => $this->input->post('password', $salt)));


    $data['main_content'] = 'auth/success';
    $this->load->view('includes/templates/home_page_template', $data);

    }
    $this->form_validation->run() == FALSE;
   }
  }
#10

[eluser]apodner[/eluser]
what is the name of the model? Auth?




Theme © iAndrew 2016 - Forum software by © MyBB