I'm trying to set up an ajax login for a cordova based app. I am building it into the theme functions.php
It was working, then has just stopped working. No new plugins or updates that I am aware of to the core.
So far I have switched from get to post and vice versa. I tried standard jquery ajax call and the xhr method as shown below, but I am having no luck.
Probably a stupid error, but I am not finding it.
PHP
function login()
{
$creds['user_login'] = $_GET["username"];
$creds['user_password'] = $_GET["password"];
$return = array(); //What we send back
if( !empty($_GET["username"]) && !empty($_GET["password"]) && trim($_GET["username"]) != '' && trim($_GET["password"] != '') ){
$credentials = array('user_login' => $_GET["username"], 'user_password'=> $_GET["password"], 'remember' => 'true');
$loginResult = wp_signon($credentials);
$user_role = 'null';
if ( strtolower(get_class($loginResult)) == 'wp_user' ) {
//User login successful
$current_user = $loginResult;
/* @var $loginResult WP_User */
$return['result'] = true;
$creds = array();
$auth_resp = "TRUE";
$creds_id = $current_user->id;
$img_url_temp = $current_user->id;
$creds_login = $current_user->user_login;
$creds_pass = 'correct';
$creds_email = $current_user->user_email;
$creds_role = $current_user->user_level;
$creds_fname = $current_user->user_firstname;
$creds_lname = $current_user->user_lastname;
$creds_dispname = $current_user->display_name;
$creds_imgurl = get_av_url($creds_id);
$creds_coverurl = get_cover_url($creds_id);
$creds_array = array($auth_resp,$creds_id,$current_user->user_login,$creds_pass,$creds_email,$creds_role,$creds_fname,$creds_lname,$creds_dispname,$creds_imgurl,$creds_coverurl);
array_push($creds, $creds_array);
header("Content-Type: application/json");
echo json_encode($creds);
die();
} elseif ( strtolower(get_class($loginResult)) == 'wp_error' ) {
//User login failed
/* @var WP_Error $loginResult */
$return['result'] = false;
echo $loginResult->get_error_message();
die();
} else {
//Undefined Error
$return['result'] = false;
echo 'An undefined error has ocurred';
die();
}
}else{
$return['result'] = false;
echo 'Please supply your username and password.';
die();
}
$return['action'] = 'login';
//Return the result array with errors etc.
//echo json_decode($return);
die();
}
add_action("wp_ajax_login", "login");
add_action("wp_ajax_nopriv_login", "login");
JQUERY
var xhr = new XMLHttpRequest();
xhr.open("get", ajaxurl + "?action=login&username=" + encodeURIComponent(usernameF) + "&password=" + encodeURIComponent(passwordF));
xhr.onload = function(){
var creds = [];
var user_info_storage = "";
var log = "";
var id = "";
var login_array = JSON.parse(xhr.responseText);
var creds_login = "";
var creds_pass = "";
var creds_email = "";
var creds_role = "";
var creds_fname = "";
var creds_lname = "";
var creds_dispname = "";
var creds_imgurl = "";
var creds_coverurl = "";
for(var count = 0; count < login_array.length; count++)
{
log = login_array[count][0];
id = login_array[count][1];
creds_login = login_array[count][2];
creds_pass = login_array[count][3];
creds_email = login_array[count][4];
creds_role = login_array[count][5];
creds_fname = login_array[count][6];
creds_lname = login_array[count][7];
creds_dispname = login_array[count][8];
creds_imgurl = login_array[count][9];
creds_coverurl = login_array[count][10];
}
creds.push(log);
creds.push(id);
creds.push(creds_login);
creds.push(passwordF);
creds.push(creds_email);
creds.push(creds_role);
creds.push(creds_fname);
creds.push(creds_lname);
creds.push(creds_dispname);
creds.push(creds_imgurl);
creds.push(creds_coverurl);
if(log == "FALSE")
{
// alert("Wrong Username and Password", null, "Wrong Creds", "Try Again");
myApp.addNotification({
title: 'Login Error',
subtitle: '',
message: 'Wrong login or password',
media: '<i class="trekicons trek-trekfind"></i>'
});
$('#login_loader').css('display', 'none');
$('#login_form').fadeTo('fast', 1, function () {
// Animation complete.
});
}
else if(log == "TRUE")
{
if(id == 0){
//alert("Incorrect feedback");
window.setTimeout(login, 1000);
} else {
//fetch_and_display_posts();
//$("#page_two_link").click();
//$("#testing").html(creds);
window.localStorage.setItem("user.info", JSON.stringify([creds]));
$('#user_disp_name').html(creds_dispname);
$('#login_loader').css('display', 'none');
//$('#login_form').fadeTo('fast', 1, function () {
// Animation complete.
//});
myApp.closeModal('.popup-login');
user_check();
}
}
}
xhr.send();