Browse Source

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

Co-Authored-By: gobdevel <99349192+gobdevel@users.noreply.github.com>
pull/1516/head
Thomas Kaul 2 years ago
committed by GitHub
parent
commit
60f52bb209
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      apps/api/src/app/auth/auth.module.ts
  2. 16
      apps/api/src/app/auth/auth.service.ts
  3. 4
      apps/api/src/app/info/info.service.ts
  4. 4
      apps/api/src/app/user/user.controller.ts
  5. 12
      apps/api/src/services/property/property.service.ts

2
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 { UserModule } from '@ghostfolio/api/app/user/user.module';
import { ConfigurationModule } from '@ghostfolio/api/services/configuration.module'; import { ConfigurationModule } from '@ghostfolio/api/services/configuration.module';
import { PrismaModule } from '@ghostfolio/api/services/prisma.module'; import { PrismaModule } from '@ghostfolio/api/services/prisma.module';
import { PropertyModule } from '@ghostfolio/api/services/property/property.module';
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { JwtModule } from '@nestjs/jwt'; import { JwtModule } from '@nestjs/jwt';
@ -21,6 +22,7 @@ import { JwtStrategy } from './jwt.strategy';
signOptions: { expiresIn: '180 days' } signOptions: { expiresIn: '180 days' }
}), }),
PrismaModule, PrismaModule,
PropertyModule,
SubscriptionModule, SubscriptionModule,
UserModule UserModule
], ],

16
apps/api/src/app/auth/auth.service.ts

@ -1,5 +1,6 @@
import { UserService } from '@ghostfolio/api/app/user/user.service'; import { UserService } from '@ghostfolio/api/app/user/user.service';
import { ConfigurationService } from '@ghostfolio/api/services/configuration.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 { Injectable, InternalServerErrorException } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt'; import { JwtService } from '@nestjs/jwt';
import { Provider } from '@prisma/client'; import { Provider } from '@prisma/client';
@ -11,6 +12,7 @@ export class AuthService {
public constructor( public constructor(
private readonly configurationService: ConfigurationService, private readonly configurationService: ConfigurationService,
private readonly jwtService: JwtService, private readonly jwtService: JwtService,
private readonly propertyService: PropertyService,
private readonly userService: UserService private readonly userService: UserService
) {} ) {}
@ -50,6 +52,13 @@ export class AuthService {
}); });
if (!user) { if (!user) {
const isUserSignupEnabled =
await this.propertyService.isUserSignupEnabled();
if (!isUserSignupEnabled) {
throw new Error('Sign up forbidden');
}
// Create new user if not found // Create new user if not found
user = await this.userService.createUser({ user = await this.userService.createUser({
provider, provider,
@ -78,6 +87,13 @@ export class AuthService {
}); });
if (!user) { if (!user) {
const isUserSignupEnabled =
await this.propertyService.isUserSignupEnabled();
if (!isUserSignupEnabled) {
throw new Error('Sign up forbidden');
}
// Create new user if not found // Create new user if not found
user = await this.userService.createUser({ user = await this.userService.createUser({
provider, provider,

4
apps/api/src/app/info/info.service.ts

@ -105,9 +105,7 @@ export class InfoService {
} }
const isUserSignupEnabled = const isUserSignupEnabled =
((await this.propertyService.getByKey( await this.propertyService.isUserSignupEnabled();
PROPERTY_IS_USER_SIGNUP_ENABLED
)) as boolean) ?? true;
if (isUserSignupEnabled) { if (isUserSignupEnabled) {
globalPermissions.push(permissions.createUserAccount); globalPermissions.push(permissions.createUserAccount);

4
apps/api/src/app/user/user.controller.ts

@ -70,9 +70,7 @@ export class UserController {
@Post() @Post()
public async signupUser(): Promise<UserItem> { public async signupUser(): Promise<UserItem> {
const isUserSignupEnabled = const isUserSignupEnabled =
((await this.propertyService.getByKey( await this.propertyService.isUserSignupEnabled();
PROPERTY_IS_USER_SIGNUP_ENABLED
)) as boolean) ?? true;
if (!isUserSignupEnabled) { if (!isUserSignupEnabled) {
throw new HttpException( throw new HttpException(

12
apps/api/src/services/property/property.service.ts

@ -1,5 +1,8 @@
import { PrismaService } from '@ghostfolio/api/services/prisma.service'; 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'; import { Injectable } from '@nestjs/common';
@Injectable() @Injectable()
@ -39,6 +42,13 @@ export class PropertyService {
return properties?.[aKey]; 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 }) { public async put({ key, value }: { key: string; value: string }) {
return this.prismaService.property.upsert({ return this.prismaService.property.upsert({
create: { key, value }, create: { key, value },

Loading…
Cancel
Save