-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrequestHandlers.js
61 lines (61 loc) · 2.07 KB
/
requestHandlers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
var path = require("path"),
httprequest = require("request"),
serviceUrl = {
baseURL: "http://34.239.10.155/index.php/api",
dataURL: "/v1/",
tokenURL: "/oauth2/token",
headers: {
Authorization: "Basic dGVzdGNsaWVudDp0ZXN0cGFzcw==", //username: testclient || password: testpass
}
};
//var dbConection = require("./dbConection"); //Módulo personalizado de conexión a Mongodb
function init(request, response){
if(request.reactJSAppStructure && request.reactJSAppStructure.userData)
response.sendFile(path.join(__dirname, "client/home.html"));
else
response.sendFile(path.join(__dirname, "client/index.html"));
}
function login(request, response, data){
var parsedData = JSON.parse(data);
httprequest.post({
url: serviceUrl.baseURL + serviceUrl.tokenURL,
headers: serviceUrl.headers,
form: {
grant_type: "password",
username: parsedData.user,
password: parsedData.pass
}
},
function(errorRequest, responseRequest, bodyRequest){
if(errorRequest)
console.log("Error: "+ errorRequest);
else{
var body = JSON.parse(bodyRequest);
if(body.status !== 401){
request.reactJSAppStructure.userData = {
access_token: body.access_token
};
}
response.writeHead(200, "application/json");
response.end(bodyRequest);
}
});
}
function initUserData(request, response){
if(request.reactJSAppStructure && request.reactJSAppStructure.userData){
response.writeHead(200, {"Content-Type": "json/application"}); //Escribimos cabecera (Typo de contenido, texto tipo html)
response.end(JSON.stringify({
success: true,
baseURL: serviceUrl.baseURL,
dataURL: serviceUrl.dataURL,
access_token: request.reactJSAppStructure.userData.access_token
})); //Terminamos respuesta
}
else{
response.writeHead(200, {"Content-Type": "json/application"}); //Escribimos cabecera (Typo de contenido, texto tipo html)
response.end(JSON.stringify({success: false})); //Terminamos respuesta
}
}
exports.init = init;
exports.login = login;
exports.initUserData = initUserData;