Difference between revisions of "API/Authentication"

From aMember Pro Documentation
Jump to: navigation, search
(fixed incorrect class name)
Line 6: Line 6:
 
require_once '/home/me/public_html/amember/bootstrap.php'; // replace with correct path to amember/bootstrap.php
 
require_once '/home/me/public_html/amember/bootstrap.php'; // replace with correct path to amember/bootstrap.php
  
$u = Am_Di::getInstance()->authUser->getUser(); // returns User object or null
+
$u = Am_Di::getInstance()->auth->getUser(); // returns User object or null
 
var_dump($u); // display the object
 
var_dump($u); // display the object
  
echo "\n<br>User#" . Am_Di::getInstance()->authUser->getUserId(); // returns user_id or null
+
echo "\n<br>User#" . Am_Di::getInstance()->auth->getUserId(); // returns user_id or null
  
 
// try to login with username 'bob' and password '1111' (from IP 11.22.33.44)
 
// try to login with username 'bob' and password '1111' (from IP 11.22.33.44)
$result = Am_Di::getInstance()->authUser->login('bob', '1111', '11.22.33.44');
+
$result = Am_Di::getInstance()->auth->login('bob', '1111', '11.22.33.44');
 
echo $result->isValid() ? "OK" : ("failed: " . $result->getMessage());
 
echo $result->isValid() ? "OK" : ("failed: " . $result->getMessage());
  
 
// logout user from API
 
// logout user from API
Am_Di::getInstance()->authUser->logout();
+
Am_Di::getInstance()->auth->logout();
 
</source>
 
</source>

Revision as of 01:55, 16 January 2013

There are 2 classes available to handle user and admin authentication: Am_Auth_User and Am_Auth_Admin Usage is the same:

require_once '/home/me/public_html/amember/bootstrap.php'; // replace with correct path to amember/bootstrap.php
 
$u = Am_Di::getInstance()->auth->getUser(); // returns User object or null
var_dump($u); // display the object
 
echo "\n<br>User#" . Am_Di::getInstance()->auth->getUserId(); // returns user_id or null
 
// try to login with username 'bob' and password '1111' (from IP 11.22.33.44)
$result = Am_Di::getInstance()->auth->login('bob', '1111', '11.22.33.44');
echo $result->isValid() ? "OK" : ("failed: " . $result->getMessage());
 
// logout user from API
Am_Di::getInstance()->auth->logout();