Mokoala v4.0

§ Authenticating Users » Authorizing Users

Authorizing users is a staple part of any web-based application and Mokoala has a set of built in functions to handle member interactions. In this example we are also using the MK_Request class, which handles all $_POST, $_FILES, $_GET and Mokoala core variables. This is covered in more detail elsewhere.

User registration, login and log out is already included with Mokoala. This guide is intended to help you understand how the process works.

In the below example let's assume that a form has been submitted posting the variables 'email' and 'password'. These are then passed onto the MK_Authorizer::authorizeByEmailPass() method, checked and if correct will return an instance of that user, stored in the $user variable. Otherwise an empty user object will be return. A successful login can be checked using the MK_RecordUser::isAuthorized() method.

$email = MK_Request::getPost('email');
$password = MK_Request::getPost('password');

$user = MK_Authorizer::authorizeByEmailPass($email, $password);

if($user->isAuthorized()){
	print 'User '.$user->getDisplayName().' logged in';
}else{
	print 'Incorrect details';
}

Keep Users Logged In

So that's pretty easy but what if you want to keep that user logged in? All you need to do is store that user's id to a session variable. To control sessions we use the MK_Session class, which stores and handles session variables.

MK_Session::start('mk');
$session = MK_Session::getInstance();

$email = MK_Request::getPost('email');
$password = MK_Request::getPost('password');

$user = MK_Authorizer::authorizeByEmailPass($email, $password);

if($user->isAuthorized()){
	$session->login = $user->getId();
	print 'User '.$user->getDisplayName().' logged in';
}else{
	print 'Incorrect details';
}

Now all we need to do is check if the $session->login variable is set and we can log that user back in using MK_Authorizer::authorizeById().

MK_Session::start('mk');
$session = MK_Session::getInstance();
if( !empty($session->login) ){
    $user = MK_Authorizer::authorizeById( $session->login );
    print 'User '.$user->getDisplayName().' logged in';
}else{
    print 'Not logged in';
}

Authenticating Users

Basic Guides

Handling Forms

Social Sign In

Using the Dashboard

Working With Data

levitra online