Files
factorio-clusterio/plugins/player_auth/instance.js
T
Hornwitser bfe119eaa3 Flatten lib namespace
Move all functions and classes currently available through lib/name and
libName to be under a single flat namespace in lib.  This make
reorganising the lib namespace much simpler as the location of symbols
can move without breaking the consumers of lib.

The multi-entrypoint design was already problematic for the web plugin
system which couldn't effectively use it due to how webpack's shared
module system is awkward to set up with multiple entrypoints per package.
2023-07-28 16:03:29 +02:00

54 lines
1.4 KiB
JavaScript

/**
* @module
*/
"use strict";
const lib = require("@clusterio/lib");
const { FetchPlayerCodeRequest, SetVerifyCodeRequest } = require("./messages");
class InstancePlugin extends lib.BaseInstancePlugin {
async init() {
if (!this.instance.config.get("factorio.enable_save_patching")) {
throw new Error("player_auth plugin requires save patching.");
}
this.instance.server.on("ipc-player_auth", event => this.handleEvent(event).catch(
err => this.logger.error(`Error handling event:\n${err.stack}`)
));
}
async handleEvent(event) {
if (event.type === "open_dialog") {
if (!this.host.connector.connected) {
await this.sendRcon(`/web-login error ${event.player} login is temporarily unavailable`);
return;
}
let response;
try {
response = await this.instance.sendTo("controller", new FetchPlayerCodeRequest(event.player));
} catch (err) {
await this.sendRcon(`/web-login error ${event.player} ${err.message}`);
return;
}
await this.sendRcon(`/web-login open ${event.player} ${response.controllerUrl} ${response.playerCode}`);
} else if (event.type === "set_verify_code") {
try {
await this.instance.sendTo("controller", new SetVerifyCodeRequest(event.player, event.verify_code));
} catch (err) {
await this.sendRcon(`/web-login error ${event.player} ${err.message}`);
return;
}
await this.sendRcon(`/web-login code_set ${event.player}`);
}
}
}
module.exports = {
InstancePlugin,
};