Authentication package of the CodeCollab project
PHP7+
Include the library in your project using composer:
{
"require-dev": {
"codecollab/authentication": "1.0.*"
}
}
Creating an instance of User
requires an instance of \CodeCollab\Http\Session\Session
from the Http Library
The logIn
function takes as arguments the password from the form, the passowrd hash from the database and the user's information (to be persisted in Session).
$user = new User($session);
if ($user->logIn($password_from_form, $hash_from_db, $user_info_from_db)) {
/** login successful **/
} else {
/** login failed */
}
Assuming there's a "remember me" feature implemented a user can simply be logged in without comparing password hashes.
if ($user->logInRememberMe($user_info_from_db)) {
/** login successful **/
} else {
/** login failed */
}
After a successful login, the user's information can be retrieved depending on what $user_info_from_db
(in above snippet) contained:
$user_name = $session->get('user_name');
$user_id = $session->get('user_id');
if ($user->isLoggedIn() {
/** User is logged in **/
}
To check for and rehash (when needed) a logged in user's password:
if ($user->needsRehash($hash_from_db)) {
$new_hash = $user->rehash($password_from_form);
//save $new_hash to database
}
$user->logOut();