Skip to content

Open Source Wealth Management Software. Angular + NestJS + Prisma + Nx + TypeScript 🤍

Handle user signup for OAuth and Internet Identity (#1515)

Thomas Kaul60f52bb209f8
diff --git a/apps/api/src/app/auth/auth.module.ts b/apps/api/src/app/auth/auth.module.ts
index b25e4c18b9..dfdec56557 100644
--- a/apps/api/src/app/auth/auth.module.ts
+++ b/apps/api/src/app/auth/auth.module.ts
@@ -4,6 +4,7 @@ import { SubscriptionModule } from '@ghostfolio/api/app/subscription/subscriptio
import { UserModule } from '@ghostfolio/api/app/user/user.module';
import { ConfigurationModule } from '@ghostfolio/api/services/configuration.module';
import { PrismaModule } from '@ghostfolio/api/services/prisma.module';
+import { PropertyModule } from '@ghostfolio/api/services/property/property.module';
import { Module } from '@nestjs/common';
import { JwtModule } from '@nestjs/jwt';
@@ -21,6 +22,7 @@ import { JwtStrategy } from './jwt.strategy';
signOptions: { expiresIn: '180 days' }
}),
PrismaModule,
+ PropertyModule,
SubscriptionModule,
UserModule
],
diff --git a/apps/api/src/app/auth/auth.service.ts b/apps/api/src/app/auth/auth.service.ts
index 3178ce9ac1..0be9f2877b 100644
--- a/apps/api/src/app/auth/auth.service.ts
+++ b/apps/api/src/app/auth/auth.service.ts
@@ -1,5 +1,6 @@
import { UserService } from '@ghostfolio/api/app/user/user.service';
import { ConfigurationService } from '@ghostfolio/api/services/configuration.service';
+import { PropertyService } from '@ghostfolio/api/services/property/property.service';
import { Injectable, InternalServerErrorException } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { Provider } from '@prisma/client';
@@ -11,6 +12,7 @@ export class AuthService {
public constructor(
private readonly configurationService: ConfigurationService,
private readonly jwtService: JwtService,
+ private readonly propertyService: PropertyService,
private readonly userService: UserService
) {}
@@ -50,6 +52,13 @@ export class AuthService {
});
if (!user) {
+ const isUserSignupEnabled =
+ await this.propertyService.isUserSignupEnabled();
+
+ if (!isUserSignupEnabled) {
+ throw new Error('Sign up forbidden');
+ }
+
// Create new user if not found
user = await this.userService.createUser({
provider,
@@ -78,6 +87,13 @@ export class AuthService {
});
if (!user) {
+ const isUserSignupEnabled =
+ await this.propertyService.isUserSignupEnabled();
+
+ if (!isUserSignupEnabled) {
+ throw new Error('Sign up forbidden');
+ }
+
// Create new user if not found
user = await this.userService.createUser({
provider,
diff --git a/apps/api/src/app/info/info.service.ts b/apps/api/src/app/info/info.service.ts
index e3702fbbdb..f9eab70184 100644
--- a/apps/api/src/app/info/info.service.ts
+++ b/apps/api/src/app/info/info.service.ts
@@ -105,9 +105,7 @@ export class InfoService {
}
const isUserSignupEnabled =
- ((await this.propertyService.getByKey(
- PROPERTY_IS_USER_SIGNUP_ENABLED
- )) as boolean) ?? true;
+ await this.propertyService.isUserSignupEnabled();
if (isUserSignupEnabled) {
globalPermissions.push(permissions.createUserAccount);
diff --git a/apps/api/src/app/user/user.controller.ts b/apps/api/src/app/user/user.controller.ts
index 4c8b0d7489..de79eaee98 100644
--- a/apps/api/src/app/user/user.controller.ts
+++ b/apps/api/src/app/user/user.controller.ts
@@ -70,9 +70,7 @@ export class UserController {
@Post()
public async signupUser(): Promise<UserItem> {
const isUserSignupEnabled =
- ((await this.propertyService.getByKey(
- PROPERTY_IS_USER_SIGNUP_ENABLED
- )) as boolean) ?? true;
+ await this.propertyService.isUserSignupEnabled();
if (!isUserSignupEnabled) {
throw new HttpException(
diff --git a/apps/api/src/services/property/property.service.ts b/apps/api/src/services/property/property.service.ts
index 4760c3a944..69e7ff46bb 100644
--- a/apps/api/src/services/property/property.service.ts
+++ b/apps/api/src/services/property/property.service.ts
@@ -1,5 +1,8 @@
import { PrismaService } from '@ghostfolio/api/services/prisma.service';
-import { PROPERTY_CURRENCIES } from '@ghostfolio/common/config';
+import {
+ PROPERTY_CURRENCIES,
+ PROPERTY_IS_USER_SIGNUP_ENABLED
+} from '@ghostfolio/common/config';
import { Injectable } from '@nestjs/common';
@Injectable()
@@ -39,6 +42,13 @@ export class PropertyService {
return properties?.[aKey];
}
+ public async isUserSignupEnabled() {
+ return (
+ ((await this.getByKey(PROPERTY_IS_USER_SIGNUP_ENABLED)) as boolean) ??
+ true
+ );
+ }
+
public async put({ key, value }: { key: string; value: string }) {
return this.prismaService.property.upsert({
create: { key, value },