Skip to content

Commit

Permalink
feat: make it possible to disable user registration
Browse files Browse the repository at this point in the history
This includes using user features as well
  • Loading branch information
bjarneo committed Sep 8, 2022
1 parent 81ac0bd commit 7ab2ca1
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 22 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Have a look at the Dockerfile for a full example of how to run this application.
- `SECRET_REDIS_TLS` Default: false - If the redis instance is using tls
- `SECRET_REDIS_USER` Default: "" - You redis user name
- `SECRET_REDIS_PASSWORD` Default: "" - Your redis password
- `SECRET_MAX_TEXT_SIZE` Default: "256" - The max text size for the secret. Is set in kb. i.e. 256 for 256kb.
- `SECRET_JWT_SECRET` Default: good_luck_have_fun - Override this for the secret signin JWT tokens for log in
- `SECRET_FILE_SIZE` Default: 4 - Set the total allowed upload file size in mb.
- `SECRET_ENABLE_FILE_UPLOAD` Default: true - Enable or disable file upload
Expand All @@ -77,7 +78,7 @@ Have a look at the Dockerfile for a full example of how to run this application.
- `SECRET_DO_SPACES_SECRET` Default: "" - The Spaces/s3 secret
- `SECRET_DO_SPACES_BUCKET` Default: "" - The Spaces/s3 bucket name
- `SECRET_DO_SPACES_FOLDER` Default: "" - The Spaces/s3 folder for uploading
- `SECRET_MAX_TEXT_SIZE` Default: "256" - The max text size for the secret. Is set in kb. i.e. 256 for 256kb.
- `SECRET_USER_DISABLE` Default: false - Disable user registration

## Run locally

Expand Down
7 changes: 6 additions & 1 deletion config/default.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const {
SECRET_REDIS_PORT = 6379,
SECRET_REDIS_USER = null,
SECRET_REDIS_PASSWORD = null,
SECRET_REDIS_TLS = false,
SECRET_REDIS_TLS = 'false',
SECRET_JWT_SECRET = 'good_luck_have_fun',
SECRET_ENABLE_FILE_UPLOAD = 'true',
SECRET_FILE_SIZE = 4, // 4 mb
Expand All @@ -17,6 +17,7 @@ const {
SECRET_DO_SPACES_BUCKET = 'hemmelig',
SECRET_DO_SPACES_FOLDER = 'localhost.hemmelig.app',
SECRET_MAX_TEXT_SIZE = 256, // 256 kb
SECRET_USER_DISABLE = 'false',
NODE_ENV = 'development',
} = process.env;

Expand Down Expand Up @@ -58,6 +59,9 @@ const config = {
// /var/tmp files can live up to 30 days
folder: `/var/tmp/hemmelig/upload/files/`,
},
user: {
disabled: JSON.parse(SECRET_USER_DISABLE),
},
logger: true,
cors: '*',
__client_config: {
Expand All @@ -66,6 +70,7 @@ const config = {
},
settings: {
enableFileUpload: JSON.parse(SECRET_ENABLE_FILE_UPLOAD),
disableUsers: JSON.parse(SECRET_USER_DISABLE),
},
},
};
Expand Down
19 changes: 11 additions & 8 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import keyGeneration from './src/server/decorators/key-generation.js';

import authenticationRoute from './src/server/controllers/authentication.js';
import accountRoute from './src/server/controllers/account.js';
import downloadROute from './src/server/controllers/download.js';
import downloadRoute from './src/server/controllers/download.js';
import secretRoute from './src/server/controllers/secret.js';
import statsRoute from './src/server/controllers/stats.js';
import healthzRoute from './src/server/controllers/healthz.js';
Expand All @@ -42,14 +42,17 @@ fastify.register(attachment);
fastify.register(keyGeneration);

// Register our routes before the static content
fastify.register(authenticationRoute, {
prefix: '/api/authentication',
});
if (!config.get('user.disabled')) {
fastify.register(authenticationRoute, {
prefix: '/api/authentication',
});

fastify.register(accountRoute, {
prefix: '/api/account',
});
fastify.register(downloadROute, { prefix: '/api/download' });
fastify.register(accountRoute, {
prefix: '/api/account',
});
}

fastify.register(downloadRoute, { prefix: '/api/download' });
fastify.register(secretRoute, { prefix: '/api/secret' });
fastify.register(statsRoute, { prefix: '/api/stats' });
fastify.register(healthzRoute, { prefix: '/api/healthz' });
Expand Down
3 changes: 1 addition & 2 deletions src/client/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ import {
Group,
} from '@mantine/core';
import { ModalsProvider } from '@mantine/modals';
import { useTranslation } from 'react-i18next';

import HeaderContent from './components/header';

import { useTranslation } from 'react-i18next';

// Code-splitting is automated for `routes` directory
import Home from './routes/home';
import Secret from './routes/secret';
Expand Down
8 changes: 4 additions & 4 deletions src/client/components/header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { useEffect, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { Anchor, Button, Container, Group, Grid } from '@mantine/core';
import { IconLockOff, IconLogin } from '@tabler/icons';
import { useTranslation } from 'react-i18next';
import { userLoginChanged } from '../../actions/';
import Logo from './logo.js';

import { hasToken, removeToken } from '../../helpers/token';
import config from '../../config';

import style from './style.module.css';
import { useTranslation } from 'react-i18next';

const Header = () => {
const { t } = useTranslation();
Expand Down Expand Up @@ -43,7 +43,7 @@ const Header = () => {
</Anchor>
</Grid.Col>

{isLoggedIn && (
{isLoggedIn && !config.get('settings.disableUsers') && (
<Grid.Col span={6}>
<Group position="right">
<Button
Expand All @@ -66,7 +66,7 @@ const Header = () => {
</Grid.Col>
)}

{!isLoggedIn && (
{!isLoggedIn && !config.get('settings.disableUsers') && (
<>
<Grid.Col span={3}>
<Group position="right">
Expand Down
1 change: 1 addition & 0 deletions src/client/config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ export default {
},
settings: {
enableFileUpload: true,
disableUsers: false,
},
};
10 changes: 7 additions & 3 deletions src/client/routes/signin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import { Button, Container, TextInput, Stack, Title, Text, PasswordInput } from
import { IconLock, IconUser, IconLogin } from '@tabler/icons';

import Success from '../../components/info/success';

import { signIn } from '../../api/authentication';

import { setToken } from '../../helpers/token';
import config from '../../config';

const Secret = () => {
const [username, setUsername] = useState('');
Expand All @@ -17,6 +16,8 @@ const Secret = () => {
const [token, setTokenState] = useState('');
const [success, setSuccess] = useState(false);

const userDisabled = config.get('settings.disableUsers');

useEffect(() => {
if (token) {
setToken(token);
Expand Down Expand Up @@ -52,7 +53,7 @@ const Secret = () => {
<Container size="xs">
<Stack>
<Title order={1} align="center">
Sign in
{userDisabled ? 'User creation has been disabled' : 'Sign in'}
</Title>

<Text size="sm" align="center">
Expand All @@ -66,6 +67,7 @@ const Secret = () => {
onChange={onUsernameChange}
required
error={error}
disabled={userDisabled}
/>

<PasswordInput
Expand All @@ -75,6 +77,7 @@ const Secret = () => {
onChange={onPasswordChange}
required
error={error}
disabled={userDisabled}
/>

<Button
Expand All @@ -90,6 +93,7 @@ const Secret = () => {
})}
leftIcon={<IconLogin size={14} />}
onClick={onSignIn}
disabled={userDisabled}
>
Sign in
</Button>
Expand Down
11 changes: 8 additions & 3 deletions src/client/routes/signup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import { Button, Container, TextInput, Stack, Title, Text, PasswordInput } from
import { IconLock, IconUser, IconLogin, IconAt } from '@tabler/icons';

import Success from '../../components/info/success';

import { signUp } from '../../api/authentication';

import { setToken } from '../../helpers/token';
import config from '../../config';

const Secret = () => {
const [username, setUsername] = useState('');
Expand All @@ -18,6 +17,8 @@ const Secret = () => {
const [token, setTokenState] = useState('');
const [success, setSuccess] = useState(false);

const userDisabled = config.get('settings.disableUsers');

useEffect(() => {
if (token) {
setToken(token);
Expand Down Expand Up @@ -57,7 +58,7 @@ const Secret = () => {
<Container size="xs">
<Stack>
<Title order={1} align="center">
Sign up
{userDisabled ? 'User creation has been disabled' : 'Sign up'}
</Title>

<Text size="sm" align="center">
Expand All @@ -71,6 +72,7 @@ const Secret = () => {
onChange={onEmailChange}
required
error={error?.type === 'email' ? error?.error : ''}
disabled={userDisabled}
/>

<TextInput
Expand All @@ -80,6 +82,7 @@ const Secret = () => {
onChange={onUsernameChange}
required
error={error?.type === 'username' ? error?.error : ''}
disabled={userDisabled}
/>

<PasswordInput
Expand All @@ -89,6 +92,7 @@ const Secret = () => {
onChange={onPasswordChange}
required
error={error?.type === 'password' ? error?.error : ''}
disabled={userDisabled}
/>

<Button
Expand All @@ -104,6 +108,7 @@ const Secret = () => {
})}
leftIcon={<IconLogin size={14} />}
onClick={onSignUp}
disabled={userDisabled}
>
Sign up
</Button>
Expand Down

0 comments on commit 7ab2ca1

Please sign in to comment.