I am trying to build a login into a mobile app.
I am doing this with Titanium Appcelerator and I am doing the Ajax POST Request to a method in my Laravel 5 Web Application that will handle the data to log in.
The problem: The Mobile app sends the request. The Laravel app receives the ajax request, BUT, the request comes empty! No data.
This is the code I use to receive the data.
Route::post("mobile/auth/login", function(\Illuminate\Http\Request $request){
return json_encode($request->all());
});
This is the code Alloy View used for the form in Titanium Appcelerator
<Alloy>
<Window id="loginForm" fullscreen="true">
<Label id="appTitle">FaztCash</Label>
<Label id="appInstruction">Inicia sesión</Label>
<View id="loginView" layout="vertical">
<TextField id="inputUsername" />
<TextField id="inputPassword" passwordMask="true" />
<Button id="buttonLogin" onClick="performLogin" />
<ActivityIndicator id="activityIndicator" />
</View>
</Window>
</Alloy>
This is the Ajax call used to send the params
var main_domain = "http://127.0.0.1/";
Ti.API.info("Performing login process..");
// Comprobamos que los datos del formulario no estén vacios
if( !$.inputUsername.value || !$.inputPassword.value ){
// Arrojamos alerta
var dialog = Ti.UI.createAlertDialog({
message: L('formMissingFields', 'Debes ingresar tu usuario y contraseña'),
ok: 'OK',
title: L('actionRequired', 'Acción requerida')
}).show();
}else{
Ti.API.info("Obteniendo CSRF...");
var csrfHTTP = Ti.Network.createHTTPClient({
onload : function() {
Ti.App.Properties.setString("csrf", this.responseText);
},
onerror:function(){
Ti.API.info("Hubo un error");
}
});
csrfHTTP.open("GET", main_domain + "getCSRF");
csrfHTTP.send();
Alloy.Globals.CSRF = Ti.App.Properties.getString("csrf");
Ti.API.info("CSRFToken: " + Alloy.Globals.CSRF);
//
Ti.API.info("Creating HTTP Client...");
var loginReq = Titanium.Network.createHTTPClient();
Ti.API.info("Opening address...");
loginReq.open("POST", main_domain + "mobile/auth/login");
loginReq.setRequestHeader("enctype", "multipart/form-data");
loginReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
loginReq.setRequestHeader("X-CSRF-Token", Alloy.Globals.CSRF);
Ti.API.info("Sending params...");
var params = {
"username": $.inputUsername.value,
"password": $.inputPassword.value
};
loginReq.send(params);
Ti.API.info("Loading form...");
loginReq.onload = function()
{
Ti.API.info("Onload succeded!");
Titanium.API.info("Response headers: " + JSON.stringify(loginReq.getResponseHeaders()));
Titanium.API.info("Response body: " + this.responseText);
var json = this.responseText;
var response = JSON.parse(json);
if (response.logged == true)
{
Ti.API.info("Logged in");
//alert("Welcome " + response.name + ". Your email is: " + response.email);
}
else
{
Ti.API.info("Llamada falló: " + response.message);
alert(response.message);
}
};
loginReq.onerror = function(){
Titanium.API.info("Response error headers: " + JSON.stringify(loginReq.getResponseHeaders()));
Titanium.API.info("Response error body: " + JSON.stringify(this.responseText));
};
}
And this is the Log when the request is done
[INFO] : Performing login process..
[INFO] : Obteniendo CSRF...
[INFO] : CSRFToken: 7LMkXF8Dv5pfBVS4QPs2OC8FeafnY6szchpkHy1u
[INFO] : Creating HTTP Client...
[INFO] : Opening address...
[INFO] : Sending params...
[INFO] : Loading form...
[INFO] : Onload succeded!
[INFO] : Response headers: {"Keep-Alive":"timeout=5, max=100","Server":"Apache/2.4.16 (Unix) PHP/5.5.27","Content-Type":"text/html; charset=UTF-8","X-Powered-By":"PHP/5.5.27","Content-Length":"2","Date":"Fri, 16 Oct 2015 19:49:42 GMT","Cache-Control":"no-cache","Set-Cookie":"XSRF-TOKEN=eyJpdiI6ImtvMTdkTFFPc0thaVRpSFwvQlB6cENBPT0iLCJ2YWx1ZSI6IjZpUkhDWG9VakljZGpFekZOVG5tMEs4Zzk1TkVMNVRRNGFjT1dPNG5ZamhXN0JCRkVzZStyc2ZmTDA5eVVwakw2TkQ2cVwvZkZLTWhodU5CRDhoZFk4dz09IiwibWFjIjoiNmJkNzM1NmM2ZDBlNTUwMjE3ODVmNjJiNGMyMjJlYjBjMmQ1ZGFjMTgyNjZiOWMxMjc0ODQ5YjNiY2JmNjhiZSJ9; expires=Fri, 16-Oct-2015 21:49:44 GMT; Max-Age=7200; path=/, laravel_session=eyJpdiI6ImtKYktCNjZoUDVOVTJJdUNoZGxhTmc9PSIsInZhbHVlIjoiQ1BUbFpoWHpDV0Z5ZlVNQlVVZGY1ejdcLzR1Y3hUNm5LSzNGZGJ2REhQZ3RSWDVOQU1SSXBPOWJpZG45VWN3WW03T3JZaW5yZjhTNEE5TDZTYmllcFNBPT0iLCJtYWMiOiI0M2I4MzM5MTI4NGUzZjE0YzgzZjRhZjZmM2E2NjZlYjdlMGJmOTE2Zjg1ZDQzODRkMjlkN2VhMjcwZmY5OTNjIn0%3D; expires=Fri, 16-Oct-2015 21:49:44 GMT; Max-Age=7200; path=/; httponly","Connection":"Keep-Alive"}
[INFO] : Response body: []
[INFO] : Llamada falló: undefined
My thoughts:
When I was setting up the CSRF Protection for the form (mandatory on Laravel 5+) I noticed that it received the token and so on.. I think that it might be an issue with a cross domain ajax post call, but have no idea how to implement it on Titanium Appcelerator and I couldn't find any info in the Internet about this particular issue.
Thank you for your time everyone.
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1OJtBHy
via IFTTT
Aucun commentaire:
Enregistrer un commentaire