Logging In/Logging Out
$this->bitauth->logged_in()
Returns TRUE if a user is currently logged in, FALSE if not.
if( ! $this->bitauth->logged_in())
{
// Save current URL for redirect after successful login
$this->session->set_userdata('redir', current_url());
// Redirect to login form
redirect('path/to/login_form');
}
$this->bitauth->login($username, $password, $remember = FALSE, $extra = array())
Tries to authenticate the user, and sets the "Remember Me" token if appropriate. Returns FALSE and sets an error message retrieved with get_error() if authentication fails. The following code is from the example that comes with BitAuth:
$username = $this->input->post('username');
$password = $this->input->post('password');
$remember_me = $this->input->post('remember_me');
if($this->bitauth->login($username, $password, $remember_me))
{
// Redirect
if($redir = $this->session->userdata('redir'))
{
$this->session->unset_userdata('redir');
}
redirect($redir ? $redir : 'bitauth_example');
}
else
{
$data['error'] = $this->bitauth->get_error();
}
Once logged in, all userdata for the user is assigned directly to the BitAuth object in the same manner as get_users(), with the exception of the password:
echo $this->bitauth->username;
For information on all of the available fields, see Fetching Users.
Requiring Additional Login Fields
Username is always required. You can, however, add any number of additional fields which will be checked at login. This is a 2-step process. First, you must tell BitAuth to expect the field(s):
$this->bitauth->add_login_field('email');
Once this is done, you simply have to pass the data to login() as an associative array. So, we would update our earlier example:
$username = $this->input->post('username');
$password = $this->input->post('password');
$remember_me = $this->input->post('remember_me');
$extra = array('email' => $this->input->post('email'));
$this->bitauth->add_login_field('email');
if($this->bitauth->login($username, $password, $remember_me, $extra))
{
// Redirect
if($redir = $this->session->userdata('redir'))
{
$this->session->unset_userdata('redir');
}
redirect($redir ? $redir : 'bitauth_example');
}
else
{
$data['error'] = $this->bitauth->get_error();
}
In order to get an error message for these fields if they fail validation, you must add a line to the language file, bitauth_invalid_{field} (ie, bitauth_invalid_email).
$lang['bitauth_invalid_email'] = 'Email does not match this username';
This function also accepts an array for multiple fields:
$this->bitauth->add_login_field(array('email', 'pin'));
Logging in with something besides username
To login with something besides username; to use email as a username, for example, simply change the bitauth_username line in the language file to whatever label you like, and setup the form_validation for your login form appropriately.
$this->bitauth->login_from_token()
If a valid "Remember Me" token is found, but not an active login, BitAuth will try to login from that token. If the login is invalid, the token is destroyed. This can be seen in BitAuth's __construct():
if($this->logged_in())
{
$this->get_session_values();
}
else if($this->input->cookie($this->_remember_token_name))
{
$this->login_from_token();
}
$this->bitauth->logout()
Destroys the session, deletes the "Remember Me" token if one exists, and updates the users table to clear the "remember me" token there as well.
$this->bitauth->delete_remember_token()
Called by $this->bitauth->logout(), deletes the "Remember Me" token and updates the users table as described above.
$this->bitauth->password_almost_expired($user = NULL)
Returns TRUE if the user is within pwd_max_age days of the maximum password age config setting. If no $user is given, the currently logged in user is assumed.
$this->bitauth->password_is_expired($user = NULL)
Returns TRUE if the user's password is expired. If no $user is given, the currently logged in user is assumed.