File tree Expand file tree Collapse file tree 5 files changed +47
-0
lines changed
libraries/nestjs-libraries/src/database/prisma/organizations Expand file tree Collapse file tree 5 files changed +47
-0
lines changed Original file line number Diff line number Diff 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 ,
Original file line number Diff line number Diff 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 ,
Original file line number Diff line number Diff line change 1+ import { internalFetch } from '@gitroom/helpers/utils/internal.fetch' ;
12
23export const dynamic = 'force-dynamic' ;
34
45import { Register } from '@gitroom/frontend/components/auth/register' ;
56import { Metadata } from 'next' ;
67import { isGeneralServerSide } from '@gitroom/helpers/utils/is.general.server.side' ;
8+ import Link from 'next/link' ;
79
810export const metadata : Metadata = {
911 title : `${ isGeneralServerSide ( ) ? 'Postiz' : 'Gitroom' } Register` ,
1012 description : '' ,
1113} ;
1214
1315export 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}
Original file line number Diff line number Diff 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 : {
Original file line number Diff line number Diff 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 ,
You can’t perform that action at this time.
0 commit comments