2022-08-25 11:20:00 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const CachedManager = require('./CachedManager');
|
|
|
|
const { Error } = require('../errors/DJSError');
|
|
|
|
const Session = require('../structures/Session');
|
|
|
|
/**
|
|
|
|
* Manages API methods for users and stores their cache.
|
|
|
|
* @extends {CachedManager}
|
|
|
|
*/
|
|
|
|
class SessionManager extends CachedManager {
|
|
|
|
constructor(client, iterable) {
|
|
|
|
super(client, Session, iterable);
|
|
|
|
}
|
2023-04-03 10:52:24 +00:00
|
|
|
/**
|
|
|
|
* The cache of Sessions
|
|
|
|
* @type {Collection<string, Session>}
|
|
|
|
* @name SessionManager#cache
|
|
|
|
*/
|
|
|
|
|
2022-08-25 11:20:00 +00:00
|
|
|
/**
|
|
|
|
* Fetch all sessions of the client.
|
|
|
|
* @returns {Promise<SessionManager>}
|
|
|
|
*/
|
|
|
|
fetch() {
|
|
|
|
return new Promise((resolve, reject) => {
|
2022-10-02 14:50:23 +00:00
|
|
|
this.client.api.auth.sessions
|
2022-08-25 11:20:00 +00:00
|
|
|
.get()
|
|
|
|
.then(data => {
|
|
|
|
const allData = data.user_sessions;
|
2022-10-21 11:53:09 +00:00
|
|
|
this.cache.clear();
|
2022-08-25 11:20:00 +00:00
|
|
|
for (const session of allData) {
|
2022-10-03 11:09:10 +00:00
|
|
|
this._add(new Session(this.client, session), true, { id: session.id_hash });
|
2022-08-25 11:20:00 +00:00
|
|
|
}
|
|
|
|
resolve(this);
|
|
|
|
})
|
|
|
|
.catch(reject);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Logout the client (remote).
|
|
|
|
* @param {string | null} mfaCode MFA code (if 2FA is enabled)
|
|
|
|
* @returns {Promise<undefined>}
|
|
|
|
*/
|
2022-11-08 16:56:17 +00:00
|
|
|
logoutAllDevices(mfaCode) {
|
|
|
|
if (typeof this.client.password !== 'string') throw new Error('REQUIRE_PASSWORD');
|
2022-08-25 11:20:00 +00:00
|
|
|
return this.client.api.auth.sessions.logout({
|
|
|
|
data: {
|
|
|
|
session_id_hashes: this.cache.map(session => session.id),
|
2022-11-08 16:56:17 +00:00
|
|
|
password: this.client.password,
|
2022-08-25 11:20:00 +00:00
|
|
|
code: typeof mfaCode === 'string' ? mfaCode : undefined,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = SessionManager;
|