Skip to content

Commit

Permalink
feat: allow configuration of whether registration is enabled server-side
Browse files Browse the repository at this point in the history
Signed-off-by: Thaddeus Kuah <[email protected]>
  • Loading branch information
thaddeuskkr committed Sep 2, 2024
1 parent 52d0421 commit 3b1ffe3
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ PROHIBITED_SLUGS=""
# Characters that are prohibited from being used in slugs. Defaults to "/".
PROHIBITED_CHARACTERS_IN_SLUGS=""

# Whether registration is enabled. Defaults to "true".
REGISTRATION_ENABLED=""

# Log level for the server, defaults to "info".
# Possible values: "trace", "debug", "info", "warn", "error", "fatal".
# Set to "silent" to disable logging. If the value is invalid, Nova will probably not start.
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const baseUrlRedirect = process.env['BASE_URL_REDIRECT'] || '';
const allowedHosts = process.env['ALLOWED_HOSTS']?.split(',').filter((element) => element.length > 0) || [];
const prohibitedSlugs = process.env['PROHIBITED_SLUGS']?.split(',').filter((element) => element.length > 0) || ['api'];
const prohibitedCharacters = process.env['PROHIBITED_CHARACTERS_IN_SLUGS'] || '/';
const registrationEnabled = process.env['REGISTRATION_ENABLED'] ? process.env['REGISTRATION_ENABLED'] == 'true' : true;

const $ = pino({ level });
const fastify = Fastify({ logger: false });
Expand Down Expand Up @@ -83,6 +84,7 @@ for (const route of readFiles(path.join(__dirname, 'routes'))) {
prohibitedSlugs,
prohibitedCharacters: [...prohibitedCharacters],
baseDirectory: __dirname,
registrationEnabled,
} as ConfigObject,
});
$.debug(`Registered ${route}`);
Expand Down
6 changes: 5 additions & 1 deletion src/routes/users/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import bcrypt from 'bcrypt';
import { User } from '../../models.js';
import type { Route } from '../../types.js';

export const routes: Route = (fastify, { $ }, done) => {
export const routes: Route = (fastify, { $, config }, done) => {
fastify.route({
method: ['POST'],
url: '/api/users/register',
Expand All @@ -22,6 +22,10 @@ export const routes: Route = (fastify, { $ }, done) => {
reply.code(400).send({ error: true, message: 'Missing required fields' });
return;
}
if (!config.registrationEnabled) {
reply.code(403).send({ error: true, message: 'Registration is disabled on this instance' });
return;
}
if (await User.exists({
$or: [
{ username: body.username.toLowerCase() },
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export type ConfigObject = {
prohibitedSlugs: string[];
prohibitedCharacters: string[];
baseDirectory: string;
registrationEnabled: boolean;
};

0 comments on commit 3b1ffe3

Please sign in to comment.