Skip to content

Commit

Permalink
feat: register once
Browse files Browse the repository at this point in the history
  • Loading branch information
nevo-david committed Jan 22, 2025
1 parent 9c648fe commit abc0d12
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
6 changes: 6 additions & 0 deletions apps/backend/src/api/routes/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ export class AuthController {
private _authService: AuthService,
private _emailService: EmailService
) {}

@Get('/can-register')
async canRegister() {
return {register: await this._authService.canRegister()};
}

@Post('/register')
async register(
@Req() req: Request,
Expand Down
16 changes: 16 additions & 0 deletions apps/backend/src/services/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ export class AuthService {
private _notificationService: NotificationService,
private _emailService: EmailService
) {}
async canRegister() {
if (!process.env.DISABLE_REGISTRATION) {
return true;
}

return (await this._organizationService.getCount()) === 0;
}

async routeAuth(
provider: Provider,
body: CreateOrgUserDto | LoginUserDto,
Expand All @@ -34,6 +42,10 @@ export class AuthService {
throw new Error('User already exists');
}

if (!(await this.canRegister())) {
throw new Error('Registration is disabled');
}

const create = await this._organizationService.createOrgAndUser(
body,
ip,
Expand Down Expand Up @@ -132,6 +144,10 @@ export class AuthService {
return user;
}

if (!(await this.canRegister())) {
throw new Error('Registration is disabled');
}

const create = await this._organizationService.createOrgAndUser(
{
company: body.company,
Expand Down
17 changes: 17 additions & 0 deletions apps/frontend/src/app/auth/page.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
import { internalFetch } from '@gitroom/helpers/utils/internal.fetch';

export const dynamic = 'force-dynamic';

import { Register } from '@gitroom/frontend/components/auth/register';
import { Metadata } from 'next';
import { isGeneralServerSide } from '@gitroom/helpers/utils/is.general.server.side';
import Link from 'next/link';

export const metadata: Metadata = {
title: `${isGeneralServerSide() ? 'Postiz' : 'Gitroom'} Register`,
description: '',
};

export default async function Auth() {
if (process.env.DISABLE_REGISTRATION) {
const canRegister = (
await (await internalFetch('/auth/can-register')).json()
).register;
if (!canRegister) {
return (
<div className="text-center">
Registration is disabled
<br />
<Link className="underline hover:font-bold" href="/auth/login">Login instead</Link>
</div>
);
}
}

return <Register />;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export class OrganizationRepository {
});
}

getCount() {
return this._organization.model.organization.count();
}

getUserOrg(id: string) {
return this._userOrg.model.userOrganization.findFirst({
where: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export class OrganizationService {
);
}

async getCount() {
return this._organizationRepository.getCount();
}

addUserToOrg(
userId: string,
id: string,
Expand Down

0 comments on commit abc0d12

Please sign in to comment.