From 03fa036e3836922cf3dcce0a2e65f57ff8435603 Mon Sep 17 00:00:00 2001 From: linkability <146661751+linkability@users.noreply.github.com> Date: Thu, 5 Oct 2023 23:31:52 +0100 Subject: [PATCH] change nickname added to apiv1 --- src/server/routes/api/v1/account.ts | 53 +++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/server/routes/api/v1/account.ts b/src/server/routes/api/v1/account.ts index 3703067..7da3afa 100644 --- a/src/server/routes/api/v1/account.ts +++ b/src/server/routes/api/v1/account.ts @@ -11,6 +11,7 @@ import * as Accounts from '../../../lib/accounts' import * as Authentication from '../../../lib/auth' import { assertAPI, getAccount, noAPIAccess, requiresAccount, requiresPermissions } from "../../../lib/middleware"; import ServeError from "../../../lib/errors"; +import { sendMail } from '../../../lib/mail'; const Configuration = require(`${process.cwd()}/config.json`) @@ -173,5 +174,57 @@ module.exports = function(files: Files) { } ) + router.put("/me/name", + requiresAccount, + noAPIAccess, + parser, + (req, res) => { + const Account = res.locals.acc as Accounts.Account + + const newUsername = req.body.username + + if ( + typeof newUsername != "string" + || + newUsername.length < 3 + || + req.body.username.length > 20 + ) { + ServeError(res, 400, "username must be between 3 and 20 characters in length") + return + } + + if (Accounts.getFromUsername(newUsername)) { + ServeError(res, 400, "account with this username already exists") + } + + if ( + ( + newUsername.match(/[A-Za-z0-9_\-\.]+/) + || + [] + )[0] != req.body.username + ) { + ServeError(res, 400, "username contains invalid characters") + return + } + + Account.username = newUsername + Accounts.save() + + if (Account.email) { + sendMail( + Account.email, + `Your login details have been updated`, + `Hello there! Your username has been updated to ${newUsername}. Please update your devices accordingly. Thank you for using monofile.` + ).then(() => { + res.send("OK") + }).catch((err) => {}) + } + } + ) + + + return router } \ No newline at end of file