This repository was archived by the owner on Aug 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.js
More file actions
41 lines (38 loc) · 1.32 KB
/
index.js
File metadata and controls
41 lines (38 loc) · 1.32 KB
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
'use strict';
module.exports = {};
const request = require('request-promise'),
cheerio = require('cheerio'),
Session = module.exports.Session = require('./lib/Session.js');
module.exports.login = (options) => {
var postData = {
'cid': options.custcode,
'uid': options.usercode,
'pwd': options.password,
'pin': options.pin
},
req = {
method: 'POST',
uri: 'https://web.spaggiari.eu/auth/app/default/AuthApi2.php?a=aLoginPwd',
form: postData,
json: true,
resolveWithFullResponse: true,
};
return request(req)
.then(res => {
var result = {};
var cookies = parseCookies(res.caseless.dict['set-cookie']);
if (cookies.PHPSESSID) {
result.sessionId = cookies.PHPSESSID;
} else throw new Error('Invalid session token');
if (res.body.data.auth.verified && res.body.data.auth.loggedIn) {
return new Session(result.sessionId)
} else throw new Error(res.body.error[0] || res.body.data.auth.errors[0] || 'Failed to authenticate')
})
};
function parseCookies(cookies) {
var r = {};
cookies.forEach(cookie => {
r[cookie.substr(0, 9)] = cookie.substr(10, 32)
});
return r;
}