Custom activation url with Login not working

Hey all,

I’m trying to generate a confirmUrl link within my custom snippet to send to a user to activate his/her account. I’m not using the snippet ‘Register’ to create the user’s account but I have coded it within my snippet. I have successfully created the account and assigned the user to it correct resource group but the confirmUrl does not activate the account. I’m not sure where I’m going wrong, here is my code below.

$count = $modx->getCount('modUser', array ('username' => $employee_email));                              
if ($count> 0 ) {
    // this user exists! 
    $modx->setPlaceholder('fi.validation_error_message','Error!!! This user has enrolled by another employer.');
    return false;
}
$user = $modx->newObject('modUser');
// set the username and password 
$user->set('username', $employee_email );
$password = $ms->randomPassword();
$hook->setValue("password",$password);
//$user->set('password', $password );
$modx->getService('registry', 'registry.modRegistry');
$modx->registry->addRegister('login', 'registry.modFileRegister');
$modx->registry->login->connect();
$modx->registry->login->subscribe('/useractivation/');
$modx->registry->login->send('/useractivation/',array($user->get('username') => $password),array(
'ttl' => (180*60),
));


$user->set('cachepwd',md5($password));
// save
$user->save();

// create a profile 
$profile = $modx->newObject('modUserProfile');
// initialize the fields 
$profile->set('fullname',$employee_fullname);
$profile->set('email' ,$employee_email);
$profile->set('dob',$dob);
$profile->set('gender',$gender);
$profile->set('phone' ,$phone);
$extended['fname'] = $employee_fname;
$extended['lname'] = $employee_lname;
$extended['enrolled'] = 1;
$profile->set('extended', $extended);
// add a profile to the user
$user->addOne($profile);
$user->set('active', 0);
// save
$profile->save();
$user->save();

$group = $modx->getObject('modUserGroup', array('name' => 'Employees'));
$groupMember = $modx->newObject('modUserGroupMember');
$groupMember->set('user_group', $group->get('id'));
$groupMember->set('role',1); // 1 is a membership with the Member role
$groups[] = $groupMember;

// add user to groups
$user->addMany($groups);
$user->save();  

$confirmParams['lp'] = urlencode(base64_encode($password));
$confirmParams['lu'] = urlencode(base64_encode($employee_email));
$confirmUrl = $modx->makeUrl(20,'',$confirmParams,'full');
$hook->setValue("confirmUrl",$confirmUrl);

What snippet do you use on the confirm page? The standard ConfirmRegister snippet from the Login extra?

If that is the case, then the most probable reason your code fails, is that Login uses a slightly different way to encode the parameters:

1 Like

I am using the standard ConfirmRegister snippet from the Login extra. and yes you are correct about how I encoded the paramters. It is now working as it should. Thanks and cheers.

FYI, the Subscribe extra wraps the Register snippet and provides some options like letting users manage their interests.

It also meshes with the Notify snippet to send notifications of new or updated content to selected groups of users.

Even if you don’t use those features, Subscribe provides a more secure way of registering users. IIRC, the Register snippet uses an MD5 hash that processes the concatenated username and email address in the registration link. Subscribe does not include any sensitive data in its hashed link.

Hmmm…interesting :thinking:

I definitely have to check out Subcribe then. Those features will come in handy TBH.

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.