Programmatically Authenticate User

Permalink
Hello good people!

I am trying to make a custom block to authenticate a user but am having difficulty. So far I have devised the following code:

$ui = UserInfo::getByUserName($username);
      $u = $ui->getUserObject();
      $p1 = $ui->getUserPassword();
      $p2 = $u->getUserPasswordHasher()->HashPassword($password);
      if($p1==$p2) { echo "Good credentials!" }


...however despite the same password being used (and working when logging in via the C5 login screen), the hashed version of the password ($p2) is never the same as $p1.

Anyone ever get this working? Am I missing something? I've been searching around and am not finding much on the internets.

Many thanks in advance for any help!
Cheers,

Alex

alexaalto
View Replies: View Best Answer
alexaalto replied on at Permalink Best Answer Reply
alexaalto
...ok - I should have tried harder. Looking at Concrete5_Controller_Login, I was able to create the following code:

private function validateUser($user,$pass) {
      Loader::library('authentication/open_id');
      $u = new User($user,$pass);
      if ($u->isError()) {
         switch($u->getError()) {
            case USER_NON_VALIDATED:
               throw new Exception(t('This account has not yet been validated. Please check the email associated with this account and follow the link it contains.'));
               break;
            case USER_INVALID:
               if (USER_REGISTRATION_WITH_EMAIL_ADDRESS) {
                  throw new Exception(t('Invalid email address or password.'));
               } else {
                  throw new Exception(t('Invalid username or password.'));
               }
               break;


...and it works!