Welcome Guest, Not a member yet? Register   Sign In
Creating username from email with Tank Auth
#1

[eluser]Unknown[/eluser]
I use Codeigniter framework + Tank_Auth registiration library. They work great but I just need to change register form a bit.

I want to generate username from email address instead of getting it from registeration form. I added a simple function to libraries/Tank_auth.php file. Here is my username generator function:

Code:
function _create_username_from_email($email)
{
$user = strstr($email, '@', true); // As of PHP 5.3.0
return $user;
}

Please let me know where I need to run my function and write it to database.

1) I will also need to remove username field from register form. So I need how I can pass form validation for username field in my controller.

2) I will be really happy if any of you can help me about these problem.

Thanks
#2

[eluser]Go-Trex[/eluser]
Hi,

Set the following settings in config/auth.php

Code:
$config['use_username'] = FALSE;
$config['login_by_username'] = FALSE;
$config['login_by_email'] = TRUE;

When a visitor registers at your site they do not need to fill in a username and when they log in they can only log in with a valid email address and password combination.

When you want to save the email address as a username overwrite the following function in libraries/Tank_auth.php

Code:
function create_user($username, $email, $password, $email_activation)
{
  if (!$this->ci->users->is_email_available($email)) {
   $this->error = array('email' => 'auth_email_in_use');

  } else {
   // Hash password using phpass
   $hasher = new PasswordHash(
     $this->ci->config->item('phpass_hash_strength', 'tank_auth'),
     $this->ci->config->item('phpass_hash_portable', 'tank_auth'));
   $hashed_password = $hasher->HashPassword($password);

   $data = array(
    'username' => $email,
    'password' => $hashed_password,
    'email'  => $email,
    'last_ip' => $this->ci->input->ip_address(),
   );

   if ($email_activation) {
    $data['new_email_key'] = md5(rand().microtime());
   }
   if (!is_null($res = $this->ci->users->create_user($data, !$email_activation))) {
    $data['user_id'] = $res['user_id'];
    $data['password'] = $password;
    unset($data['last_ip']);
    return $data;
   }
  }
  return NULL;
}
#3

[eluser]Unknown[/eluser]
Hi,

This has solved my problem, thanks a lot Go-Trex.




Theme © iAndrew 2016 - Forum software by © MyBB