Skip to content

Commit

Permalink
login method added
Browse files Browse the repository at this point in the history
  • Loading branch information
mgrist committed Jun 23, 2022
1 parent da1eec7 commit 71d428f
Show file tree
Hide file tree
Showing 3 changed files with 210 additions and 1 deletion.
49 changes: 49 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const axios = require("axios");
const FormData = require("form-data");

let Chapter = class {
#memberList;
#loggedIn = false;
/* the CFID and CFTOKEN are required to use the api call that returns
all the chapter members. I don't know what they do, but all I know is
they are required in the URL and they change every time you log in. */
#CFID = "";;
#CFTOKEN = "";;

// returns a promise with a success or error message
async login(username, password) {
// create new form data needed to log into the admin panel
const form = new FormData();
form.append("username", username);
form.append("password", password);

/* post request sent with the form data to log into admin panel
if login is successful, extract the needed CFID and CFTOKEN */
await axios
.post("https://services.acm.org/public/chapters/login.cfm", form)
/* a successful login attempt gives the status code 301 (redirect), and
is therefor sent to the .catch block. */
.catch((response) => {
// successfully logged in
if (response.request.res.statusCode == 300) {
this.#loggedIn = true;
let res = response.request._redirectable._options.query;
this.#CFID = res.slice(5, res.indexOf("&"));
this.#CFTOKEN = res.slice(res.indexOf("CFTOKEN") + 8);
}
});

const result = new Promise((resolve, reject) => {
if (this.#loggedIn) {
resolve("Successfully logged in");
}
else {
reject("ERROR: Invalid login credentials.");
}
});

return result;
}
}

module.exports = Chapter;
157 changes: 157 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@
"bugs": {
"url": "https://github.com/mgrist/acm-roster/issues"
},
"homepage": "https://github.com/mgrist/acm-roster#readme"
"homepage": "https://github.com/mgrist/acm-roster#readme",
"dependencies": {
"axios": "^0.27.2"
}
}

0 comments on commit 71d428f

Please sign in to comment.