Skip to content

Commit

Permalink
Add Reddit authentication error logging
Browse files Browse the repository at this point in the history
  • Loading branch information
haykam821 committed Feb 2, 2020
1 parent 2ef53ff commit 15d7507
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
],
"dependencies": {
"cosmiconfig": "^6.0.0",
"debug": "^4.1.1",
"form-data": "^3.0.0",
"got": "^10.2.2",
"sendbird": "^3.0.117",
Expand Down
18 changes: 18 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const explorer = cosmic.cosmiconfigSync("redditchatrelay", {
});
const config = explorer.search().config;

const debug = require("debug");
const log = debug("reddit-chat-relay:main");

const { version } = require("../package.json");

const Snoowrap = require("snoowrap");
Expand Down Expand Up @@ -61,6 +64,8 @@ function getSnoomojiURL(message) {
* Launch the relay.
*/
async function launch() {
log("launching reddit chat relay");

const form = new FormData();
form.append("user", config.credentials.username);
form.append("passwd", config.credentials.password);
Expand All @@ -72,6 +77,19 @@ async function launch() {
url: "https://ssl.reddit.com/api/login",
});

if (res.body.json.errors.length > 0) {
const errors = res.body.json.errors.map(error => error[0]);
if (errors.includes("RATELIMIT")) {
return log("could not authenticate due to ratelimit being reached");
} else if (errors.includes("INCORRECT_USERNAME_PASSWORD")) {
return log("could not authenticate due to incorrect username/password combination");
} else if (errors.includes("WRONG_PASSWORD")) {
return log("could not authenticate due to incorrect password");
} else {
return log("could not authenticate for reason: '%s'", errors[0]);
}
}

const cookieJar = new CookieJar();
cookieJar.setCookieSync("reddit_session=" + encodeURIComponent(res.body.json.data.cookie), "https://s.reddit.com");

Expand Down

0 comments on commit 15d7507

Please sign in to comment.