Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const urlresolve = require('url').resolve;
- const needle = require('needle');
- class Site {
- constructor(mainPageUrl, cookies) {
- const PHPSESSID = 'PHPSESSID';
- if (!(PHPSESSID in cookies)) {
- throw new Error('PHP Session ID not found');
- }
- this.phpsessid = cookies[PHPSESSID];
- this.mainPageUrl = mainPageUrl;
- }
- static async login(mainPageUrl, username, password) {
- const LOGIN_PAGE_ENDPOINT = 'login.php';
- try {
- const loginPageUrl = urlresolve(mainPageUrl, LOGIN_PAGE_ENDPOINT);
- const response = await needle('get', loginPageUrl, { username, password });
- return new Site(mainPageUrl, response.cookies);
- } catch (error) {
- throw new Error(`Login error: ${error}`);
- }
- }
- }
- module.exports = {
- Site
- };
- /*
- // index.js
- ; (async function () {
- const site = await Site.login(CONFIG.mainPageUrl, CONFIG.user.name, CONFIG.user.password);
- })();
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement