@fdm-monster/server
Version:
FDM Monster is a bulk OctoPrint, Klipper, PrusaLink and BambuLab manager to set up, configure and monitor 3D printers. Our aim is to provide neat overview over your farm.
167 lines (166 loc) • 6.59 kB
JavaScript
import { __exportAll } from "../_virtual/_rolldown/runtime.js";
import { __decorateMetadata } from "../_virtual/_@oxc-project_runtime@0.129.0/helpers/decorateMetadata.js";
import { __decorate } from "../_virtual/_@oxc-project_runtime@0.129.0/helpers/decorate.js";
import { BadRequestException } from "../exceptions/runtime.exceptions.js";
import { validateMiddleware } from "../handlers/validators.js";
import { AppConstants } from "../server.constants.js";
import { SettingsStore } from "../state/settings.store.js";
import { authenticate } from "../middleware/authenticate.js";
import { demoUserNotAllowed } from "../middleware/demo.middleware.js";
import { refreshTokenSchema } from "./validation/auth-controller.validation.js";
import { registerUserSchema } from "./validation/user-controller.validation.js";
import { GET, POST, before, route } from "awilix-express";
//#region src/controllers/auth.controller.ts
var auth_controller_exports = /* @__PURE__ */ __exportAll({ AuthController: () => AuthController });
var _ref, _AuthController;
let AuthController = _AuthController = class AuthController {
logger;
constructor(loggerFactory, authService, settingsStore, userService, roleService, configService) {
this.authService = authService;
this.settingsStore = settingsStore;
this.userService = userService;
this.roleService = roleService;
this.configService = configService;
this.logger = loggerFactory(_AuthController.name);
}
async login(req, res) {
this.logger.debug(`Login attempt from IP ${req.ip} and user-agent ${req.headers["user-agent"]}`);
const tokens = await this.authService.loginUser(req.body.username, req.body.password);
return res.send(tokens);
}
async getLoginRequired(req, res) {
const loginRequired = await this.settingsStore.getLoginRequired();
const registration = this.settingsStore.isRegistrationEnabled();
let wizardState = this.settingsStore.getWizardState();
const isDemoMode = this.configService.isDemoMode();
const instanceLabel = this.configService.instanceLabel();
wizardState = {
...wizardState,
wizardCompleted: isDemoMode ? true : wizardState.wizardCompleted
};
res.send({
loginRequired,
registration,
wizardState,
isDemoMode,
instanceLabel
});
}
async verifyLogin(req, res) {
return res.send({ success: true });
}
async needsPasswordChange(req, res) {
const registration = this.settingsStore.isRegistrationEnabled();
const isLoginRequired = await this.settingsStore.getLoginRequired();
if (!isLoginRequired) return res.send({
loginRequired: isLoginRequired,
registration,
needsPasswordChange: false,
authenticated: true
});
if (req.isAuthenticated()) return res.send({
loginRequired: isLoginRequired,
registration,
needsPasswordChange: req.user?.needsPasswordChange,
authenticated: true
});
return res.send({
loginRequired: isLoginRequired,
needsPasswordChange: null,
authenticated: false
});
}
async refreshLogin(req, res) {
const { refreshToken } = await validateMiddleware(req, refreshTokenSchema);
this.logger.debug(`Refresh login attempt from IP ${req.ip} and user-agent ${req.headers["user-agent"]}`);
const idToken = await this.authService.renewLoginByRefreshToken(refreshToken);
return res.send({ token: idToken });
}
async logout(req, res) {
if (!await this.settingsStore.getLoginRequired()) return res.end();
const jwtToken = req.headers.authorization?.replace("Bearer ", "") || void 0;
const userId = req.user.id;
await this.authService.logoutUserId(userId, jwtToken);
res.end();
}
async register(req, res) {
if (!this.settingsStore.isRegistrationEnabled()) throw new BadRequestException("Registration is disabled. Cant register user");
const { username, password } = await validateMiddleware(req, registerUserSchema);
if (username.toLowerCase().includes("admin") || username.toLowerCase().includes("root") || username.toLowerCase() === "demo") throw new BadRequestException("Username is not allowed");
const roles = await this.roleService.getAppDefaultRoleNames();
const result = await this.userService.register({
username,
password,
roles,
needsPasswordChange: false,
isDemoUser: false,
isRootUser: false,
isVerified: false
});
const userDto = this.userService.toDto(result);
res.send(userDto);
}
};
__decorate([
POST(),
route("/login"),
__decorateMetadata("design:type", Function),
__decorateMetadata("design:paramtypes", [Object, Object]),
__decorateMetadata("design:returntype", Promise)
], AuthController.prototype, "login", null);
__decorate([
GET(),
route("/login-required"),
__decorateMetadata("design:type", Function),
__decorateMetadata("design:paramtypes", [Object, Object]),
__decorateMetadata("design:returntype", Promise)
], AuthController.prototype, "getLoginRequired", null);
__decorate([
POST(),
route("/verify"),
before([authenticate()]),
__decorateMetadata("design:type", Function),
__decorateMetadata("design:paramtypes", [Object, Object]),
__decorateMetadata("design:returntype", Promise)
], AuthController.prototype, "verifyLogin", null);
__decorate([
POST(),
route("/needs-password-change"),
__decorateMetadata("design:type", Function),
__decorateMetadata("design:paramtypes", [Object, Object]),
__decorateMetadata("design:returntype", Promise)
], AuthController.prototype, "needsPasswordChange", null);
__decorate([
POST(),
route("/refresh"),
__decorateMetadata("design:type", Function),
__decorateMetadata("design:paramtypes", [Object, Object]),
__decorateMetadata("design:returntype", Promise)
], AuthController.prototype, "refreshLogin", null);
__decorate([
POST(),
route("/logout"),
before([authenticate()]),
__decorateMetadata("design:type", Function),
__decorateMetadata("design:paramtypes", [Object, Object]),
__decorateMetadata("design:returntype", Promise)
], AuthController.prototype, "logout", null);
__decorate([
POST(),
route("/register"),
before([demoUserNotAllowed]),
__decorateMetadata("design:type", Function),
__decorateMetadata("design:paramtypes", [Object, Object]),
__decorateMetadata("design:returntype", Promise)
], AuthController.prototype, "register", null);
AuthController = _AuthController = __decorate([route(AppConstants.apiRoute + "/auth"), __decorateMetadata("design:paramtypes", [
Object,
Object,
typeof (_ref = typeof SettingsStore !== "undefined" && SettingsStore) === "function" ? _ref : Object,
Object,
Object,
Object
])], AuthController);
//#endregion
export { AuthController, auth_controller_exports };
//# sourceMappingURL=auth.controller.js.map