Skip to content

Commit cebb841

Browse files
committed
feat(server): add flag to disable new sign ups (toeverything#6752)
1 parent 91ee5e0 commit cebb841

File tree

5 files changed

+17
-1
lines changed

5 files changed

+17
-1
lines changed

packages/backend/server/src/config/affine.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ AFFiNE.port = 3010;
5353
// AFFiNE.metrics.enabled = true;
5454
//
5555
// /* Authentication Settings */
56+
// /* Whether allow anyone signup */
57+
// AFFiNE.auth.allowSignup = true;
58+
//
5659
// /* User Signup password limitation */
5760
// AFFiNE.auth.password = {
5861
// minLength: 8,

packages/backend/server/src/core/auth/controller.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
import type { Request, Response } from 'express';
1616

1717
import {
18+
Config,
1819
PaymentRequiredException,
1920
Throttle,
2021
URLHelper,
@@ -43,7 +44,8 @@ export class AuthController {
4344
private readonly url: URLHelper,
4445
private readonly auth: AuthService,
4546
private readonly user: UserService,
46-
private readonly token: TokenService
47+
private readonly token: TokenService,
48+
private readonly config: Config
4749
) {}
4850

4951
@Public()
@@ -74,6 +76,10 @@ export class AuthController {
7476
} else {
7577
// send email magic link
7678
const user = await this.user.findUserByEmail(credential.email);
79+
if (!user && !this.config.auth.allowSignup) {
80+
throw new BadRequestException('You are not allows to sign up.');
81+
}
82+
7783
const result = await this.sendSignInEmail(
7884
{ email: credential.email, signUp: !user },
7985
redirectUri

packages/backend/server/src/core/auth/resolver.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ export class AuthResolver {
8787
@Args('email') email: string,
8888
@Args('password') password: string
8989
) {
90+
if (!this.config.auth.allowSignup) {
91+
throw new ForbiddenException('You are not allowed to sign up.');
92+
}
93+
9094
validators.assertValidCredential({ email, password });
9195
const user = await this.auth.signUp(name, email, password);
9296
await this.auth.setCookie(ctx.req, ctx.res, user);

packages/backend/server/src/fundamentals/config/def.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ export interface AFFiNEConfig {
214214
* authentication config
215215
*/
216216
auth: {
217+
allowSignup: boolean;
218+
217219
/**
218220
* The minimum and maximum length of the password when registering new users
219221
*

packages/backend/server/src/fundamentals/config/default.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ export const getDefaultAFFiNEConfig: () => AFFiNEConfig = () => {
147147
playground: true,
148148
},
149149
auth: {
150+
allowSignup: true,
150151
password: {
151152
minLength: node.prod ? 8 : 1,
152153
maxLength: 32,

0 commit comments

Comments
 (0)