Skip to content

Commit fc60ed4

Browse files
authored
Merge pull request #569 from gitroomhq/feat/register-once
Register once
2 parents 9c648fe + abc0d12 commit fc60ed4

File tree

5 files changed

+47
-0
lines changed

5 files changed

+47
-0
lines changed

apps/backend/src/api/routes/auth.controller.ts

+6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ export class AuthController {
2828
private _authService: AuthService,
2929
private _emailService: EmailService
3030
) {}
31+
32+
@Get('/can-register')
33+
async canRegister() {
34+
return {register: await this._authService.canRegister()};
35+
}
36+
3137
@Post('/register')
3238
async register(
3339
@Req() req: Request,

apps/backend/src/services/auth/auth.service.ts

+16
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ export class AuthService {
2020
private _notificationService: NotificationService,
2121
private _emailService: EmailService
2222
) {}
23+
async canRegister() {
24+
if (!process.env.DISABLE_REGISTRATION) {
25+
return true;
26+
}
27+
28+
return (await this._organizationService.getCount()) === 0;
29+
}
30+
2331
async routeAuth(
2432
provider: Provider,
2533
body: CreateOrgUserDto | LoginUserDto,
@@ -34,6 +42,10 @@ export class AuthService {
3442
throw new Error('User already exists');
3543
}
3644

45+
if (!(await this.canRegister())) {
46+
throw new Error('Registration is disabled');
47+
}
48+
3749
const create = await this._organizationService.createOrgAndUser(
3850
body,
3951
ip,
@@ -132,6 +144,10 @@ export class AuthService {
132144
return user;
133145
}
134146

147+
if (!(await this.canRegister())) {
148+
throw new Error('Registration is disabled');
149+
}
150+
135151
const create = await this._organizationService.createOrgAndUser(
136152
{
137153
company: body.company,

apps/frontend/src/app/auth/page.tsx

+17
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
1+
import { internalFetch } from '@gitroom/helpers/utils/internal.fetch';
12

23
export const dynamic = 'force-dynamic';
34

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

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

1315
export default async function Auth() {
16+
if (process.env.DISABLE_REGISTRATION) {
17+
const canRegister = (
18+
await (await internalFetch('/auth/can-register')).json()
19+
).register;
20+
if (!canRegister) {
21+
return (
22+
<div className="text-center">
23+
Registration is disabled
24+
<br />
25+
<Link className="underline hover:font-bold" href="/auth/login">Login instead</Link>
26+
</div>
27+
);
28+
}
29+
}
30+
1431
return <Register />;
1532
}

libraries/nestjs-libraries/src/database/prisma/organizations/organization.repository.ts

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ export class OrganizationRepository {
3030
});
3131
}
3232

33+
getCount() {
34+
return this._organization.model.organization.count();
35+
}
36+
3337
getUserOrg(id: string) {
3438
return this._userOrg.model.userOrganization.findFirst({
3539
where: {

libraries/nestjs-libraries/src/database/prisma/organizations/organization.service.ts

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ export class OrganizationService {
2727
);
2828
}
2929

30+
async getCount() {
31+
return this._organizationRepository.getCount();
32+
}
33+
3034
addUserToOrg(
3135
userId: string,
3236
id: string,

0 commit comments

Comments
 (0)