Fix registration, make E-mail unique, fix seeder generating duplicates
This commit is contained in:
parent
ee4fee004d
commit
0ea057eee1
6 changed files with 19 additions and 7 deletions
|
@ -28,6 +28,9 @@ export class UserResolver {
|
|||
|
||||
@mutation(returns => User)
|
||||
async register(@arg('user', validated(UserRegisterInput)) request: UserRegisterInput): Promise<Omit<User, 'password'>> {
|
||||
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,
|
||||
|
|
|
@ -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: {
|
||||
|
|
|
@ -4,3 +4,4 @@ import { ClassType } from '@loopback/graphql';
|
|||
export function validated<T>(type: ClassType) {
|
||||
return {validate: (input: T) => validateValueAgainstSchema(input, jsonToSchemaObject(modelToJsonSchema(type)))};
|
||||
}
|
||||
|
||||
|
|
|
@ -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++;
|
||||
|
|
|
@ -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<void> {
|
||||
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<boolean> {
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue