Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to disable user registration through environment variable #726

Open
wants to merge 8 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 21 additions & 20 deletions apps/api/src/app/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,12 @@ export class AuthController {
private resendOTP: ResendOTP
) {}

@Get('/github')
githubAuth() {
if (!process.env.GITHUB_OAUTH_CLIENT_ID || !process.env.GITHUB_OAUTH_CLIENT_SECRET) {
throw new ApiException(
'GitHub auth is not configured, please provide GITHUB_OAUTH_CLIENT_ID and GITHUB_OAUTH_CLIENT_SECRET as env variables'
);
}

return {
success: true,
};
@Post('/github')
async githubAuth(@Body() body: RegisterUserDto, @Res() response: Response) {
if (process.env.DISABLE_USER_REGISTRATION === 'true') {
response.status(403).send({ message: 'Account creation is disabled' });
return;
}
}

@Get('/github/callback')
Expand Down Expand Up @@ -115,17 +110,23 @@ export class AuthController {
response.contentType('text').send();
}

@Post('/register')
async register(@Body() body: RegisterUserDto, @Res() response: Response) {
const registeredUser = await this.registerUser.execute(RegisterUserCommand.create(body));
@Post('/register')
async register(@Body() body: RegisterUserDto, @Res() response: Response) {
if (process.env.DISABLE_USER_REGISTRATION === 'true') {
response.status(403).send({ message: 'Account creation is disabled' });
return;
}

response.cookie(CONSTANTS.AUTH_COOKIE_NAME, registeredUser.token, {
...COOKIE_CONFIG,
domain: process.env.COOKIE_DOMAIN,
});
const registeredUser = await this.registerUser.execute(RegisterUserCommand.create(body));

response.cookie(CONSTANTS.AUTH_COOKIE_NAME, registeredUser.token, {
...COOKIE_CONFIG,
domain: process.env.COOKIE_DOMAIN,
});

response.send(registeredUser);
}

response.send(registeredUser);
}

@Post('/verify')
async verifyRoute(@Body() body: VerifyDto, @UserSession() user: IJwtPayload) {
Expand Down
8 changes: 6 additions & 2 deletions apps/api/src/config/env-validator.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { port, str, url, ValidatorSpec } from 'envalid';
import { port, str, url, ValidatorSpec, bool } from 'envalid';
import * as envalid from 'envalid';
import { ENVTypesEnum } from '@impler/shared';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const validators: { [K in keyof any]: ValidatorSpec<any[K]> } = {
export const validators: { [K in keyof any]: ValidatorSpec<any[K]> } = {
JWT_SECRET: str(),
NODE_ENV: str({
choices: [ENVTypesEnum.LOCAL, ENVTypesEnum.TEST, ENVTypesEnum.PROD, ENVTypesEnum.CI, ENVTypesEnum.LOCAL],
default: ENVTypesEnum.LOCAL,
}),
DISABLE_USER_REGISTRATION: bool({
default: false,
desc: 'Flag to disable user registration',
}),
S3_LOCAL_STACK: str(),
S3_BUCKET_NAME: str(),
S3_REGION: str(),
Expand Down
1 change: 1 addition & 0 deletions docker/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ JWT_SECRET=SUPER-SECRET
NODE_ENV=local
MONGO_URL=mongodb://mongodb:27017/impler
RABBITMQ_CONN_URL=amqp://guest:guest@rabbitmq:5672
DISABLE_USER_REGISTRATION=false

# AWS
S3_LOCAL_STACK=http://localhost:4566
Expand Down
3 changes: 2 additions & 1 deletion docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ services:
environment:
NODE_ENV: ${NODE_ENV}
PORT: ${API_PORT}
DISABLE_USER_REGISTRATION: ${DISABLE_USER_REGISTRATION}
WIDGET_BASE_URL: ${WIDGET_BASE_URL}
WEB_BASE_URL: ${WEB_BASE_URL}
MONGO_URL: ${MONGO_URL}
Expand Down Expand Up @@ -129,4 +130,4 @@ services:
volumes:
mongodb: ~
networks:
impler:
impler: