Merge pull request #569 from gitroomhq/feat/register-once

Register once
This commit is contained in:
Nevo David 2025-01-22 15:46:31 +07:00 committed by GitHub
commit fc60ed41aa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 47 additions and 0 deletions

View File

@ -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,

View File

@ -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,
@ -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,
@ -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,

View File

@ -1,9 +1,11 @@
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`,
@ -11,5 +13,20 @@ export const metadata: Metadata = {
};
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 />;
}

View File

@ -30,6 +30,10 @@ export class OrganizationRepository {
});
}
getCount() {
return this._organization.model.organization.count();
}
getUserOrg(id: string) {
return this._userOrg.model.userOrganization.findFirst({
where: {

View File

@ -27,6 +27,10 @@ export class OrganizationService {
);
}
async getCount() {
return this._organizationRepository.getCount();
}
addUserToOrg(
userId: string,
id: string,