Skip to content
This repository has been archived by the owner on Jul 3, 2021. It is now read-only.

Commit

Permalink
Added in ability to login via fb
Browse files Browse the repository at this point in the history
  • Loading branch information
thedark1337 committed Feb 17, 2017
1 parent 6e500d4 commit 68ce54e
Showing 1 changed file with 66 additions and 23 deletions.
89 changes: 66 additions & 23 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,20 @@ class PlugAPI extends EventEmitter3 {
*/
this.logger = require('./logger');

/**
* Is User a guest?
* @type {Boolean}
* @private
*/
this.guest = false;

/**
* Is User logging in via Facebook?
* @type {Boolean}
* @private
*/
this.fbLogin = false;

/**
* Slug of room, that the bot is currently connecting to
* @type {null|String}
Expand Down Expand Up @@ -607,6 +621,17 @@ class PlugAPI extends EventEmitter3 {
}
};

const handleLogin = (body) => {
if (body && body.body && body.body.status !== 'ok') {
handleErrors.error(`Login Error: ${body && body.body && body.body !== 'ok' ? `API Status: ${body.body.status}` : ''} ${body && body.statusCode !== 200 ? `HTTP Status: ${body.body.statusCode} - ${http.STATUS_CODES[body.body.statusCode]}` : ''}`, loginCallback);
} else {
loggingIn = false;
if (typeof loginCallback === 'function') {
return loginCallback(null, this);
}
}
};

if (this.guest) {
got(`${this.baseUrl}/plugcubed`).then((data) => {
if (data.body) {
Expand Down Expand Up @@ -638,28 +663,39 @@ class PlugAPI extends EventEmitter3 {
if (!csrfToken || !csrfToken[1]) {
handleErrors.error("Login Error: Can't Obtain CSRF Token", loginCallback);
}

got(`${this.baseUrl}/_/auth/login`, {
json: true,
method: 'POST',
headers: Object.assign(store(this)._headers, {
cookie: this.jar.getCookieStringSync('https://plug.dj')
}),
body: JSON.stringify({
csrf: csrfToken,
email: store(this)._auththenticationInfo.email,
password: store(this)._auththenticationInfo.password
if (this.fbLogin) {
got(`${this.baseUrl}/_/auth/facebook`, {
json: true,
method: 'POST',
headers: Object.assign(store(this)._headers, {
cookie: this.jar.getCookieStringSync('https://plug.dj')
}),
body: JSON.stringify({
csrf: csrfToken,
accessToken: store(this)._auththenticationInfo.facebook.accessToken,
userID: store(this)._auththenticationInfo.facebook.userID
})
})
}).then((body) => {
if (body && body.body && body.body.status !== 'ok') {
handleErrors.error(`Login Error: ${body && body.body && body.body !== 'ok' ? `API Status: ${body.body.status}` : ''} ${body && body.statusCode !== 200 ? `HTTP Status: ${body.body.statusCode} - ${http.STATUS_CODES[body.body.statusCode]}` : ''}`, loginCallback);
} else {
loggingIn = false;
if (typeof loginCallback === 'function') {
return loginCallback(null, this);
}
}
})
.then(handleLogin)
.catch((fbError) => {
setImmediate(() => {
handleErrors.error(`${fbError}`, callback);
});
});
} else {
got(`${this.baseUrl}/_/auth/login`, {
json: true,
method: 'POST',
headers: Object.assign(store(this)._headers, {
cookie: this.jar.getCookieStringSync('https://plug.dj')
}),
body: JSON.stringify({
csrf: csrfToken,
email: store(this)._auththenticationInfo.email,
password: store(this)._auththenticationInfo.password
})
})
.then(handleLogin)
.catch((err2) => {
setImmediate(() => {
if (err2) {
Expand All @@ -678,6 +714,7 @@ class PlugAPI extends EventEmitter3 {
});

});
}
})
.catch((err) => {
setImmediate(() => {
Expand Down Expand Up @@ -1144,16 +1181,22 @@ class PlugAPI extends EventEmitter3 {

if (typeof authenticationData !== 'object' || authenticationData === null) {
this.guest = true;
} else if (!authenticationData.hasOwnProperty('email') && !authenticationData.hasOwnProperty('email') && authenticationData.hasOwnProperty('guest')) {
} else if (!authenticationData.hasOwnProperty('email') && !authenticationData.hasOwnProperty('email') && !authenticationData.hasOwnProperty('facebook') && authenticationData.hasOwnProperty('guest')) {
this.guest = true;
} else if (authenticationData.hasOwnProperty('facebook')) {
if (typeof authenticationData.facebook.accessToken !== 'string') {
handleErrors.error('Missing Facebook Auth Token', callback);
} else if (typeof authenticationData.facebook.userID !== 'string') {
handleErrors.error('Missing Facebook UserID', callback);
}
this.fbLogin = true;
} else {
if (typeof authenticationData.email !== 'string') {
handleErrors.error('Missing Login Email', callback);
}
if (typeof authenticationData.password !== 'string') {
handleErrors.error('Missing Login Password', callback);
}
this.guest = false;
}

store(this)._auththenticationInfo = authenticationData;
Expand Down

0 comments on commit 68ce54e

Please sign in to comment.