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

Commit

Permalink
Merge pull request #125 from Dazzuh/master
Browse files Browse the repository at this point in the history
Set cookies for guest, remove HTML Parsing from login function as guest.
  • Loading branch information
thedark1337 authored Feb 11, 2017
2 parents e97ec04 + d4435de commit 99fc562
Showing 1 changed file with 30 additions and 59 deletions.
89 changes: 30 additions & 59 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -774,55 +774,27 @@ function queueConnectSocket(roomSlug) {

/**
* Get the auth code for the user
* @param {string} roomSlug room for guests to get auth code from
* @param {Function} callback Callback function
* @private
*/
function getAuthCode(roomSlug, callback) {
if (!roomSlug) {
headers.cookie = that.jar.getCookieStringSync('https://plug.dj');
function getAuthCode(callback) {
headers.cookie = that.jar.getCookieStringSync('https://plug.dj');

got(`${baseUrl}/_/auth/token`, {
headers,
json: true
}).then(function(body) {
_authCode = body.body.data[0];

return callback();
}).catch(function(error) {
logger.error('PlugAPI', `Error getting auth code: ${error}`);

const slug = room.getRoomMeta().slug;

that.close();
that.connect(slug);
});
} else {
const url = `${baseUrl}/${typeof roomSlug === 'string' ? roomSlug : ''}`;

headers.cookie = that.jar.getCookieStringSync('https://plug.dj');

got(url, {
headers
}).then(function(body) {
if (body && body.body && body.body.indexOf('_jm') > -1 && body.body.indexOf('_st') > -1) {
_authCode = body.body.split('_jm')[1].split('"')[1];
const _st = body.body.split('_st')[1].split('"')[1];

DateUtilities.setServerTime(_st);
got(`${baseUrl}/_/auth/token`, {
headers,
json: true
}).then(function(body) {
_authCode = body.body.data[0];

return callback();
}
})
.catch(function(error) {
logger.error('PlugAPI', `Error getting auth code: ${error}`);
return callback();
}).catch(function(error) {
logger.error('PlugAPI', `Error getting auth code: ${error}`);

const slug = room.getRoomMeta().slug;
const slug = room.getRoomMeta().slug;

that.close();
that.connect(slug);
});
}
that.close();
that.connect(slug);
});
}

/**
Expand All @@ -832,15 +804,9 @@ function getAuthCode(roomSlug, callback) {
*/
function connectSocket(roomSlug) {
if (_authCode === null || _authCode === '') {
if (that.guest) {
getAuthCode(roomSlug, function() {
connectSocket(roomSlug);
});
} else {
getAuthCode(null, function() {
connectSocket(roomSlug);
});
}
getAuthCode(function() {
connectSocket(roomSlug);
});

return;
}
Expand Down Expand Up @@ -1144,9 +1110,20 @@ function performLoginCredentials(callback) {
*/
let loggingIn = true;

function setCookie(res) {
if (Array.isArray(res.headers['set-cookie'])) {
res.headers['set-cookie'].forEach(function(item) {
that.jar.setCookieSync(item, 'https://plug.dj');
});
} else if (typeof res.headers['set-cookie'] === 'string') {
that.jar.setCookieSync(res.headers['set-cookie'], 'https://plug.dj');
}
}

if (that.guest) {
got(`${baseUrl}/plugcubed`).then(function(data) {
if (data.body) {
setCookie(data);
loggingIn = false;
if (typeof callback === 'function') {
return callback(null, that);
Expand All @@ -1168,15 +1145,9 @@ function performLoginCredentials(callback) {

DateUtilities.setServerTime(serverTime);

if (Array.isArray(indexBody.headers['set-cookie'])) {
indexBody.headers['set-cookie'].forEach(function(item) {
that.jar.setCookieSync(item, 'https://plug.dj');
});
} else if (typeof indexBody.headers['set-cookie'] === 'string') {
that.jar.setCookieSync(indexBody.headers['set-cookie'], 'https://plug.dj');
}
setCookie(indexBody);

if (!csrfToken || !csrfToken[1]) {
if (!csrfToken) {
handleErrors.error("Login Error: Can't Obtain CSRF Token", callback);
}

Expand Down

0 comments on commit 99fc562

Please sign in to comment.