UNPKG

nobloxmmc.js

Version:

A Node.js wrapper for ROBLOX. (original from sentanos)

82 lines (76 loc) 2.36 kB
// Includes var options = require('../options.js') var getGeneralToken = require('./getGeneralToken.js').func var getVerification = require('./getVerification.js').func var getCurrentUser = require('./getCurrentUser.js').func var http = require('./http.js').func var cookieFile = './cookie' var fs = require('fs') // Args exports.required = ['cookie'] exports.optional = [] var day = 86400000 // Define const relog = (cookie) => { if (!cookie) throw new Error('no cookie supplied?') options.jar.session = cookie return getVerification({ url: 'https://www.roblox.com/my/account#!/security' }) .then((ver) => { if (!ver.header) console.log(`Bad cookie.`) return getGeneralToken({}).then((token) => { return http({ url: 'https://www.roblox.com/authentication/signoutfromallsessionsandreauthenticate', options: { method: 'POST', resolveWithFullResponse: true, verification: ver.header, jar: null, headers: { 'X-CSRF-TOKEN': token }, form: { __RequestVerificationToken: ver.inputs.__RequestVerificationToken } } }).then((res) => { var cookies = res.headers['set-cookie'] if (cookies) { options.jar.session = cookies.toString().match(/\.ROBLOSECURITY=(.*?);/)[1] fs.writeFile(cookieFile, JSON.stringify({ cookie: options.jar.session, time: Date.now() }), (err) => { if (err) { console.error('Failed to write cookie') } return true }) } }) }) }) } module.exports = c async function c (cookie) { // Check for file if (fs.existsSync(cookieFile)) { var json = JSON.parse(fs.readFileSync(cookieFile)) // Check its new enough if (json.time + day > Date.now()) { // Its recent enough. Try it. try { await relog(json.cookie) return getCurrentUser({}) } catch (e) { console.log(`Stored relog failed. Trying with given.`) } } } if (cookie) { // Try the user's cookie try { await relog(cookie) return getCurrentUser({}) } catch (e) { console.error(e) } } throw new Error('No cookie supplied and no cookie file available.') }