Difference between revisions of "API/Authentication"

From aMember Pro Documentation
Jump to: navigation, search
(Created page with "There are 2 classes available to handle user and admin authentication: Am_Auth_User and Am_Auth_Admin Usage is the same: <source> Am_Auth_User::getInstance()->getUser(); // ret...")
 
 
(4 intermediate revisions by 3 users not shown)
Line 4: Line 4:
  
 
<source>
 
<source>
Am_Auth_User::getInstance()->getUser(); // returns User object or null
+
require_once '/home/me/public_html/amember/bootstrap.php'; // replace with correct path to amember/bootstrap.php
Am_Auth_User::getInstance()->getUserId() // returns user_id or null
+
 
$result = Am_Auth_User::getInstance()->login('login', 'password', '11.22.33.44');
+
$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(new Am_Auth_Adapter_Password('bob', '1111', Am_Di::getInstance()->userTable), '11.22.33.44');
 
echo $result->isValid() ? "OK" : ("failed: " . $result->getMessage());
 
echo $result->isValid() ? "OK" : ("failed: " . $result->getMessage());
  
Am_Auth_User::getInstance()->logout();
+
// logout user from API
 +
Am_Di::getInstance()->auth->logout();
 
</source>
 
</source>

Latest revision as of 02:59, 21 May 2016

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(new Am_Auth_Adapter_Password('bob', '1111', Am_Di::getInstance()->userTable), '11.22.33.44');
echo $result->isValid() ? "OK" : ("failed: " . $result->getMessage());
 
// logout user from API
Am_Di::getInstance()->auth->logout();