Browse Source

Fix the user account creation

pull/682/head
Thomas 3 years ago
parent
commit
e7cf76824a
  1. 3
      apps/api/src/app/user/interfaces/user-item.interface.ts
  2. 3
      apps/api/src/app/user/user.controller.ts
  3. 8
      apps/api/src/app/user/user.service.ts
  4. 11
      apps/client/src/app/pages/register/register-page.component.ts
  5. 7
      apps/client/src/app/pages/register/show-access-token-dialog/show-access-token-dialog.html

3
apps/api/src/app/user/interfaces/user-item.interface.ts

@ -1,4 +1,7 @@
import { Role } from '@prisma/client';
export interface UserItem {
accessToken?: string;
authToken: string;
role: Role;
}

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

@ -85,12 +85,13 @@ export class UserController {
const hasAdmin = await this.userService.hasAdmin();
const { accessToken, id } = await this.userService.createUser({
const { accessToken, id, role } = await this.userService.createUser({
role: hasAdmin ? 'USER' : 'ADMIN'
});
return {
accessToken,
role,
authToken: this.jwtService.sign({
id
})

8
apps/api/src/app/user/user.service.ts

@ -180,7 +180,11 @@ export class UserService {
return hash.digest('hex');
}
public async createUser(data?: Prisma.UserCreateInput): Promise<User> {
public async createUser(data: Prisma.UserCreateInput): Promise<User> {
if (!data?.provider) {
data.provider = 'ANONYMOUS';
}
let user = await this.prismaService.user.create({
data: {
...data,
@ -199,7 +203,7 @@ export class UserService {
}
});
if (data.provider === Provider.ANONYMOUS) {
if (data.provider === 'ANONYMOUS') {
const accessToken = this.createAccessToken(
user.id,
this.getRandomString(10)

11
apps/client/src/app/pages/register/register-page.component.ts

@ -6,6 +6,7 @@ import { TokenStorageService } from '@ghostfolio/client/services/token-storage.s
import { InfoItem } from '@ghostfolio/common/interfaces';
import { hasPermission, permissions } from '@ghostfolio/common/permissions';
import { LineChartItem } from '@ghostfolio/ui/line-chart/interfaces/line-chart.interface';
import { Role } from '@prisma/client';
import { format } from 'date-fns';
import { DeviceDetectorService } from 'ngx-device-detector';
import { Subject } from 'rxjs';
@ -62,19 +63,21 @@ export class RegisterPageComponent implements OnDestroy, OnInit {
this.dataService
.postUser()
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe(({ accessToken, authToken }) => {
this.openShowAccessTokenDialog(accessToken, authToken);
.subscribe(({ accessToken, authToken, role }) => {
this.openShowAccessTokenDialog(accessToken, authToken, role);
});
}
public openShowAccessTokenDialog(
accessToken: string,
authToken: string
authToken: string,
role: Role
): void {
const dialogRef = this.dialog.open(ShowAccessTokenDialog, {
data: {
accessToken,
authToken
authToken,
role
},
disableClose: true,
width: '30rem'

7
apps/client/src/app/pages/register/show-access-token-dialog/show-access-token-dialog.html

@ -1,4 +1,9 @@
<h1 mat-dialog-title i18n>Create Account</h1>
<h1 mat-dialog-title>
<span i18n>Create Account</span
><span *ngIf="data.role === 'ADMIN'" class="badge badge-light ml-2"
>{{ data.role }}</span
>
</h1>
<div mat-dialog-content>
<div>
<mat-form-field appearance="outline" class="w-100">

Loading…
Cancel
Save