From 0ea057eee1fbbc5f47e612f91ff9a18e0c70717d Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sat, 14 May 2022 15:50:12 +0200 Subject: [PATCH] Fix registration, make E-mail unique, fix seeder generating duplicates --- backend/src/graphql-resolvers/user.resolver.ts | 3 +++ backend/src/graphql-types/user.ts | 3 +++ backend/src/helpers.ts | 1 + backend/src/observers/user-seeder.observer.ts | 5 +++-- frontend/src/app/auth/login.service.ts | 8 +++----- frontend/src/app/graphql/user.graphql | 6 ++++++ 6 files changed, 19 insertions(+), 7 deletions(-) diff --git a/backend/src/graphql-resolvers/user.resolver.ts b/backend/src/graphql-resolvers/user.resolver.ts index e7289f7..f14ec5a 100644 --- a/backend/src/graphql-resolvers/user.resolver.ts +++ b/backend/src/graphql-resolvers/user.resolver.ts @@ -28,6 +28,9 @@ export class UserResolver { @mutation(returns => User) async register(@arg('user', validated(UserRegisterInput)) request: UserRegisterInput): Promise> { + if ((await this.userRepository.count({email: request.email})).count) { + throw new Error('A megadott E-mail cimmel már létezik felhasználó'); + } const password = await hash(request.password, await genSalt()); const user = { email: request.email, diff --git a/backend/src/graphql-types/user.ts b/backend/src/graphql-types/user.ts index be76061..a08bebc 100644 --- a/backend/src/graphql-types/user.ts +++ b/backend/src/graphql-types/user.ts @@ -42,6 +42,9 @@ export const UserProperties = { required: true, jsonSchema: { pattern: /[A-Za-z\d.+_-]+@[A-Za-z.-_]*(u-szeged.hu)|(szte.hu)/.source + }, + index: { + unique: true } }, name: { diff --git a/backend/src/helpers.ts b/backend/src/helpers.ts index 5bc7002..6b7a39b 100644 --- a/backend/src/helpers.ts +++ b/backend/src/helpers.ts @@ -4,3 +4,4 @@ import { ClassType } from '@loopback/graphql'; export function validated(type: ClassType) { return {validate: (input: T) => validateValueAgainstSchema(input, jsonToSchemaObject(modelToJsonSchema(type)))}; } + diff --git a/backend/src/observers/user-seeder.observer.ts b/backend/src/observers/user-seeder.observer.ts index 3fd7cd1..c36961d 100644 --- a/backend/src/observers/user-seeder.observer.ts +++ b/backend/src/observers/user-seeder.observer.ts @@ -3,7 +3,6 @@ import { UserRepository } from '../repositories'; import { User } from '../models'; import { Mock, MockFactory } from 'mockingbird'; import { genSalt, hash } from 'bcryptjs'; -import { repository } from '@loopback/repository'; /** * This class will be bound to the application as a `LifeCycleObserver` during @@ -39,7 +38,9 @@ export class UserSeederObserver implements LifeCycleObserver { for (const user of users) { user.password = await hash('Jelszó 123', await genSalt()); if (c >= this.MAX_USERS / 4) { - user.email = user.name.split(' ')[1].toLowerCase() + '@inf.u-szeged.hu'; + const ns = user.name.split(' '); + const prefix = ns[0][0].toLowerCase() + ns[1].toLowerCase(); + user.email = prefix.replace('\'', '') + '@inf.u-szeged.hu'; } await this.repository.create(user); c++; diff --git a/frontend/src/app/auth/login.service.ts b/frontend/src/app/auth/login.service.ts index 3af62b6..2d62ce0 100644 --- a/frontend/src/app/auth/login.service.ts +++ b/frontend/src/app/auth/login.service.ts @@ -1,7 +1,5 @@ import { Injectable } from '@angular/core'; -import { HttpClient } from '@angular/common/http'; -import { environment } from '../../environments/environment'; -import { LoginGQL, LogoutGQL, UserResult } from '../services/graphql'; +import { LoginGQL, LogoutGQL, RegisterGQL, UserResult } from '../services/graphql'; @Injectable({ providedIn: 'root' @@ -19,13 +17,13 @@ export class LoginService { return this.userP; } - constructor(private http: HttpClient, private loginGQL: LoginGQL, private logoutGQL: LogoutGQL) { + constructor(private loginGQL: LoginGQL, private logoutGQL: LogoutGQL, private registerGQL: RegisterGQL) { this.tokenP = window.localStorage.getItem('token'); this.userP = JSON.parse(window.localStorage.getItem('user')); } async createUser(email: string, password: string, name: string): Promise { - await this.http.post(environment.backendUrl + '/users', {email, password, name}).toPromise(); + await this.registerGQL.mutate({user: {email, name, password}}).toPromise(); } async login(email: string, password: string): Promise { diff --git a/frontend/src/app/graphql/user.graphql b/frontend/src/app/graphql/user.graphql index 2f73651..a6fb9cd 100644 --- a/frontend/src/app/graphql/user.graphql +++ b/frontend/src/app/graphql/user.graphql @@ -14,6 +14,12 @@ mutation Logout { logout } +mutation Register($user: UserRegisterInput!) { + register(user: $user) { + id + } +} + query UserList($limit: Int!, $offset: Int!) { users(limit: $limit, offset: $offset) { list {