Skip to content

Commit

Permalink
fix: fix Steam auth, emit events, fix profiles
Browse files Browse the repository at this point in the history
Signed-off-by: Elijah Conners <business@elijahpepe.com>
  • Loading branch information
ElijahPepe committed Aug 27, 2022
1 parent 899387f commit c6de14b
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 25 deletions.
74 changes: 51 additions & 23 deletions src/client/Client.js
@@ -1,4 +1,5 @@
/* eslint-disable no-async-promise-executor */
const { EventEmitter } = require('node:events');
const process = require('node:process');
const SteamUser = require('steam-user');
const AccountManager = require('../managers/AccountManager');
Expand All @@ -10,19 +11,23 @@ const ProfileManager = require('../managers/ProfileManager');
const QuestManager = require('../managers/QuestManager');
const { apiKey, userAgent } = require('../util/Constants.js');
const { fetchData, handleData } = require('../util/Data');
const Events = require('../util/Events');

/**
* The base client for interacting with the MultiVersus API.
*/
class Client {
class Client extends EventEmitter {
/**
* @param {ClientOptions} options Options for the client
*/
constructor(options = {}) {
super({ captureRejections: true });
Object.defineProperty(this, 'accessToken', { writable: true });
if (options?.accessToken) {
this.client.emit(Events.ClientReady, this.client);
this.accessToken = options?.accessToken;
} else if (!this.accessToken && 'MULTIVERSUS_ACCESS_TOKEN' in process.env) {
this.client.emit(Events.ClientReady, this.client);
this.accessToken = process.env.MULTIVERSUS_ACCESS_TOKEN;
} else {
this.accessToken = null;
Expand Down Expand Up @@ -91,19 +96,16 @@ class Client {
* @example
* client.login('username', 'password');
*/
login(username, password) {
async login(username, password) {
this.ready = false;
return new Promise(() => {
this.steamUser.logOn({ accountName: username, password: password });
this.steamUser.on('error', error => {
throw new Error(error);
});
this.steamUser.on('loggedOn', async () => {
await this._getAccessToken();
});
this.ready = true;
this.emit(Events.Debug, `Provided client info username as ${username} and password as ${password}`);
try {
await this.getAccessToken(username, password);
return this.accessToken;
});
} catch (error) {
this.destroy();
throw error;
}
}

/**
Expand All @@ -114,18 +116,38 @@ class Client {
return this.ready;
}

_getAccessToken() {
/**
* Gets an access token for a user
* @param {string} username Username of the account to log in with
* @param {string} password Password of the account to log in with
* @private
* @returns {Promise<void>} A promise that either resolves the client or rejects it
* @example
* client.login('username', 'password');
*/
getAccessToken(username, password) {
if (this.ready) {
return Promise.resolve();
}
return new Promise(resolve => {
this.steamUser.getEncryptedAppTicket(1818750, async (err, appTicket) => {
this.steamTicket = appTicket.toString('hex').toUpperCase();
if (err) {
throw new Error(err);
}

const data = await this.info(appTicket.toString('hex').toUpperCase());
this.accessToken = data.token;

resolve(this);
this.emit(Events.Debug, 'Preparing to connect to Steam...');
this.steamUser.logOn({ accountName: username, password });
this.steamUser.once('loggedOn', async () => {
await this.steamUser.getEncryptedAppTicket(1818750, async (err, appTicket) => {
this.steamTicket = appTicket.toString('hex').toUpperCase();
if (err) {
throw new Error(err);
}

const data = await this.info(appTicket.toString('hex').toUpperCase());
this.ready = true;
this.accessToken = data.token;
this.steamUser.removeAllListeners();

this.client.emit(Events.ClientReady, this.client);

resolve(this);
});
});
});
}
Expand Down Expand Up @@ -186,3 +208,9 @@ module.exports = Client;
* @typedef {Object} ClientOptions
* @property {string} [accessToken] The access token of the user
*/

/**
* Emitted for general debugging information.
* @event Client#debug
* @param {string} info The debug information
*/
1 change: 1 addition & 0 deletions src/index.js
Expand Up @@ -5,6 +5,7 @@ exports.Client = require('./client/Client');
exports.CharacterData = require('./util/CharacterData');
exports.Constants = require('./util/Constants');
exports.Data = require('./util/Data');
exports.Events = require('./util/Events');

// Managers
exports.AccountManager = require('./managers/AccountManager');
Expand Down
12 changes: 10 additions & 2 deletions src/managers/ProfileManager.js
Expand Up @@ -27,7 +27,11 @@ class ProfileManager extends BaseManager {
}`,
accessToken: this.client.accessToken,
});
handleData(new Search(this.client, data), resolve, reject);
if (data.msg) {
return reject(new Error(data.msg));
} else {
handleData(new Search(this.client, data), resolve, reject);
}
});
}

Expand All @@ -45,7 +49,11 @@ class ProfileManager extends BaseManager {
url: `/profiles/${id}`,
accessToken: this.client.accessToken,
});
handleData(new Profile(this.client, data), resolve, reject);
if (data.msg) {
return reject(new Error(data.msg));
} else {
handleData(new Profile(this.client, data), resolve, reject);
}
});
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/util/Events.js
@@ -0,0 +1,16 @@
/**
* @typedef {Object} Events
* @property {string} ClientReady ready
* @property {string} Debug debug
* @property {string} Error error
*/

/**
* @type {Events}
* @ignore
*/
module.exports = {
ClientReady: 'ready',
Debug: 'debug',
Error: 'error',
};

0 comments on commit c6de14b

Please sign in to comment.