fix(habitica): Completed AJAX habitica account setup

Signed-off-by: Stuart McCulloch Anderson's avatarStuart McCulloch Anderson <stuart@nxfifteen.me.uk>
parent 37f83ee6
......@@ -13,22 +13,53 @@ use Core\Core;
header( 'Access-Control-Allow-Origin: https://wp.dev.psi.nxfifteen.me.uk' );
header( 'Cache-Control: no-cache, must-revalidate' );
header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' );
header( 'Content-type: application/json' );
require_once( dirname( __FILE__ ) . "/lib/autoloader.php" );
nxr(0, print_r($_POST, true));
if (array_key_exists('_nx_fb_usr', $_COOKIE) && $_COOKIE['_nx_fb_usr'] != "") {
nxr(1, "User " . $_COOKIE['_nx_fb_usr'] . " Secured");
if (array_key_exists('token', $_POST) && array_key_exists('userid', $_POST)) {
$apiUserid = $_POST['userid'];
$apiKey = $_POST['token'];
$habiticaClass = new HabitRPHPG($apiUserid, $apiKey);
$newUser = $habiticaClass->_request("get", "user", '', true);
if (!$newUser['_id']) {
http_response_code(500);
if ($newUser['message'] == "There is no account that uses those credentials.") {
echo "There is a problem with your credentials, please check the user id and api key before trying again. Or create an account bellow.";
} else {
nxr(1, print_r($newUser, true));
echo "Unknown error: " . $newUser['error'] . " :: " . $newUser['message'];
}
die();
}
} else {
$_POST['confirmPassword'] = $_POST['password2'];
unset($_POST['password2']);
$habiticaClass = new HabitRPHPG(null, null);
$newUser = $habiticaClass->_request("post", "user/auth/local/register", $_POST);
$newUser = $habiticaClass->_request("post", "user/auth/local/register", $_POST, true);
if (!$newUser['_id']) {
http_response_code(500);
if ($newUser['message'] == "Username already taken.") {
echo "Username already taken. If this is your account please use the form above to connect to it.";
} else if ($newUser['message'] == "Email address is already used in an account.") {
echo "Email address is already used in an account. If you already have an account please use the form above to connect to it.";
} else if ($newUser['message'] == "Invalid request parameters.") {
foreach ($newUser['errors'] as $error) {
echo $error['message'] . "<br />";
}
} else {
nxr(1, print_r($newUser, true));
echo "Unknown error: " . $newUser['error'] . " :: " . $newUser['message'];
}
die();
}
$apiUserid = $newUser['id'];
$apiKey = $newUser['apiToken'];
......@@ -42,15 +73,15 @@ if (array_key_exists('_nx_fb_usr', $_COOKIE) && $_COOKIE['_nx_fb_usr'] != "") {
$core->getFitbitAPI( $_COOKIE['_nx_fb_usr'] )->setForceSync( true );
$core->getFitbitAPI( $_COOKIE['_nx_fb_usr'] )->pull( $_COOKIE['_nx_fb_usr'], 'habitica' );
$cacheFile = 'cache' . DIRECTORY_SEPARATOR . '_' . $_GET[ 'user' ] . '_xp';
$cacheFile = 'cache' . DIRECTORY_SEPARATOR . '_' . $_COOKIE[ '_nx_fb_usr' ] . '_xp';
if ( file_exists( $cacheFile ) ) {
unlink($cacheFile);
}
http_response_code(200);
} else {
nxr(1, "User Unsecured");
http_response_code(403);
echo "Unauthorised Access";
}
echo "{'result':'oky'}";
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment