Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. const urlresolve = require('url').resolve;
  2. const needle = require('needle');
  3.  
  4. class Site {
  5. constructor(mainPageUrl, cookies) {
  6. const PHPSESSID = 'PHPSESSID';
  7. if (!(PHPSESSID in cookies)) {
  8. throw new Error('PHP Session ID not found');
  9. }
  10. this.phpsessid = cookies[PHPSESSID];
  11. this.mainPageUrl = mainPageUrl;
  12. }
  13.  
  14. static async login(mainPageUrl, username, password) {
  15. const LOGIN_PAGE_ENDPOINT = 'login.php';
  16. try {
  17. const loginPageUrl = urlresolve(mainPageUrl, LOGIN_PAGE_ENDPOINT);
  18. const response = await needle('get', loginPageUrl, { username, password });
  19. return new Site(mainPageUrl, response.cookies);
  20. } catch (error) {
  21. throw new Error(`Login error: ${error}`);
  22. }
  23. }
  24. }
  25.  
  26. module.exports = {
  27. Site
  28. };
  29.  
  30. /*
  31. // index.js
  32. ; (async function () {
  33. const site = await Site.login(CONFIG.mainPageUrl, CONFIG.user.name, CONFIG.user.password);
  34. })();
  35. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement