// SPDX-License-Identifier: AGPL-3.0-or-later /// // Token check used by docwell-server: returns the "users" record that owns the // bearer token (with its organization expanded), or 401. Unlike auth-refresh, // it does not issue a new token. routerAdd( "GET", "/api/docwell/auth/verify", (e) => { e.auth.ignoreEmailVisibility(true); e.app.expandRecord(e.auth, ["organization"], null); return e.json(200, e.auth); }, $apis.requireAuth("users"), ); // Sends one email through PocketBase's SMTP settings, so docwell-server's own // emails (invites, test mails) use the same mail configuration as the auth // emails PocketBase sends itself. routerAdd( "POST", "/api/docwell/mail", (e) => { const body = e.requestInfo().body; const to = String(body.to || "").trim(); const subject = String(body.subject || "").trim(); if (!to || !subject) { throw new BadRequestError("to and subject are required"); } const meta = e.app.settings().meta; const message = new MailerMessage({ from: { address: meta.senderAddress, name: meta.senderName }, to: [{ address: to }], subject: subject, html: String(body.html || ""), text: String(body.text || ""), }); e.app.newMailClient().send(message); return e.noContent(204); }, $apis.requireSuperuserAuth(), ); // Settings that come from the environment are applied on every start, so // changing an env var and restarting is enough. Settings without an env var // are left alone: the admin panel manages them. onBootstrap((e) => { e.next(); const env = (key) => $os.getenv(key) || ""; const settings = e.app.settings(); if (env("DOCWELL_APP_NAME")) { settings.meta.appName = env("DOCWELL_APP_NAME"); } else if (settings.meta.appName === "Acme") { // PocketBase's placeholder name on a fresh database. settings.meta.appName = "Docwell"; } settings.meta.appURL = env("DOCWELL_WEB_URL") || "http://localhost:3000"; settings.meta.senderName = settings.meta.appName; if (env("DOCWELL_MAIL_FROM")) { settings.meta.senderAddress = env("DOCWELL_MAIL_FROM"); } else if (settings.meta.senderAddress === "support@example.com") { settings.meta.senderAddress = "no-reply@docwell.localhost"; } const smtpHost = env("DOCWELL_SMTP_HOST"); if (smtpHost) { settings.smtp.enabled = true; settings.smtp.host = smtpHost; settings.smtp.port = parseInt(env("DOCWELL_SMTP_PORT") || "587", 10); settings.smtp.username = env("DOCWELL_SMTP_USERNAME"); settings.smtp.password = env("DOCWELL_SMTP_PASSWORD"); settings.smtp.tls = env("DOCWELL_SMTP_TLS") === "true"; } e.app.save(settings); });