Skip to content

Instantly share code, notes, and snippets.

@good-orbit
Created November 27, 2012 20:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save good-orbit/da5e01c3cb53e9b15f21 to your computer and use it in GitHub Desktop.
Save good-orbit/da5e01c3cb53e9b15f21 to your computer and use it in GitHub Desktop.
//USER ENTERS THEIR EMAIL AND THEY GET A EMAIL WITH A UNIQUE TOKEN THAT INSERTS THAT TOKEN IN A NEW TABLE ALONG WITH EMAIL ASSOCIATED WITH ACCOUNT
--CONTROLLER
public function password()
{
$data['main_content'] = 'auth/password';
$this->load->view('includes/templates/main_page_template', $data);
}
public function retrieve()
// REQUEST PASSWORD RESET
// LOADED WHEN THE FORM IS SUBMITTED OFF THE PASSWORD PAGE AND SENDS THE EMAIL WITH TOKEN AND INSTRUCTIONS
{
$this->load->library('form_validation');
$this->load->library('session');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
$this->load->model('user_model', 'um');
$this->load->library('encrypt');
$this->load->helper('url');
$submit = $this->input->post('submit');
$salt = $this->_salt();
if($submit)
// IF THE SUBMIT BUTTON IS SET
{
// START PROCESS TO CREATE $USER VARIABLE THAT HOLDS WHAT THE USER ENTERED IN THE FORM AND THAT CAN GET CHECKED AGAINST THE DB IN THE MODEL
$user = $this->um->validate_retrieve(array('email' => $this->input->post('email')));
// IF THE USER IS CREATED AND CHECKS OUT AND ALL OF THE ERRORS ARE CLEARED ON THE FORM
if( $user && $this->form_validation->run() == TRUE ) {
$domain = "clci.dev/index.php";
// CREATE A TOKEN LINK TO SEND TO THE USERS EMAIL THAT EXIST IN THE DB AND WAS ENTERED
$token = $this->encrypt->sha1($salt . $this->encrypt->sha1($this->input->post('email')));
$link = "http://www.".$domain."/auth/reset/?token=$token";
$this->load->library('email');
$this->email->from('noreply@cysticlife.org', 'CysticLife');
$this->email->to($this->input->post('email'));
$this->email->subject('Reset Password');
$this->email->message("Please go to the following web address to reset your password:\n\n$link\n\n-Your friends at CysticLife\n\nPlease remember to add the cysticlife.org domain to your address book to ensure that you receive your CysticLife e-Notifications as requested.");
$this->email->send();
redirect('auth/success');
exit;
}
$this->form_validation->run() == FALSE;
$data['main_content'] = 'auth/password';
$this->load->view('includes/templates/main_page_template', $data);
$data['email_error'] = 'This email is invalid';
}
}
--MODEL
public function validate_retrieve($data) {
$query = $this->db->where($data)->get('users', '1');
foreach ($query->result() as $user)
{
$user->email;
$user->salt;
$user->id;
}
$reset_token = array(
'token' => sha1($user->email.$user->salt).dechex($user->id),
'email' => $user->email
);
$insert = $this->db->insert('reset', $reset_token, '1');
return $reset_token;
}
//WHEN USER HITS THE LINK FROM EMAIL TAKES THEM TO PAGE THAT RESETS PASSWORDS AND IDENTIFIES THEM MATCHING THE SENT TOKEN AGAINST THE ONE STORED IN THE RESET TABLE
--CONTROLLER
public function reset()
{
$data['main_content'] = 'auth/reset';
$this->load->view('includes/templates/main_page_template', $data);
}
//RESET PASSWORD FUNCTIONALITY
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 = $_GET['token'];
$user = $this->um->reset_password(array('token' => $_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;
}
}
--MODEL
public function reset_password($data)
{
$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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment