Nicholas Ward
Member
I have everything else supported.. just can't figure out how to handle two-step for the life of me. I need to check if they have it active, then see if it's currently saved and if not, save it. Anybody have any ideas?
Thanks guys!
Code:
<?php
$ip = $_POST['ip'];
$name = $_POST['name'];
$pass = $_POST['pass'];
$email = $_POST['email'];
if (!isset($ip, $name, $pass)) {
die();
}
/**
* Create the bridge to Xenforo
**/
$fileDir = '../../';
require($fileDir . '/src/XF.php');
XF::start($fileDir);
/**
* Check if the username is registered (login and prompt for e-mail to create account)
*/
$finder = \XF::finder('XF:User');
$user = $finder->where('username', $name)->fetchOne();
if(!$user) {
/**
* Validate the username before proceeding with registration
*/
$validator = \XF::app()->validator('Username');
$username = $validator->coerceValue($name);
if (!$validator->isValid($username, $errorKey)) {
if($errorKey == 'too_long') {
die('Username too long.');
} else if($errorKey == 'disallowed' || $errorKey == 'censored') {
die('Username contained disallowed words.');
} else if($errorKey == 'regex' || $errorKey == 'comma') {
die('Username contains incorrect characters.');
} else if($errorKey == 'duplicate') {
die('Username must be unique.');
}
}
/**
* If the account isn't registered and POST doesn't include email.. request! :-)
*/
if(empty($email)) {
die('Email required.');
}
/**
* Ensure email isn't in use
*/
$emailInUse = \XF::finder('XF:User')->where('email', $email)->fetchOne();
if ($emailInUse) {
die('Email in use.');
}
/**
* Validate email
*/
$emailValidator = \XF::app()->validator('Email');
$emailValidator->setOption('check_typos', true);
if (!$emailValidator->isValid($email, $errorKey)) {
if ($errorKey == 'banned') {
die('Email banned.');
} else if ($errorKey == 'typo') {
die('Email type.');
} else {
die('Invalid email.');
}
}
/**
* Register the new user
*/
$registration = XF::service('XF:User\Registration');
$input['username'] = $name;
$input['email'] = $email;
$input['password'] = $pass;
$registration->setFromInput($input);
$user = $registration->save();
/**
* Set the user groups
*/
$user-> user_group_id = 2;
//$user->secondary_group_ids = [1, 2, 3];
$user->save();
/**
* Log the user in
*/
$columns = array("user_id", "username", "user_group_id", "secondary_group_ids");
$data = array();
foreach ($columns as $c) {
$data[$c] = $user[$c];
}
die('d=' . json_encode($data));
}
/**
* Check if the user is banned
*/
if($user->is_banned) {
die('Banned.');
}
/**
* Verify the account details
**/
$loginService = \XF::app()->service('XF:User\Login', $name, $ip);
$success = $loginService->validate($pass);
if(!$success) {
die('Incorrect password. Please try again.');
}
/**
* Verify two-step authentication
**/
/**
* Successful login
**/
$columns = array("user_id", "username", "user_group_id", "secondary_group_ids");
$data = array();
foreach ($columns as $c) {
$data[$c] = $user[$c];
}
die('d=' . json_encode($data));
Thanks guys!
Last edited: