@lenne.tech/nest-server 3.1.1 → 3.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core/modules/user/core-user.service.d.ts +6 -1
- package/dist/core/modules/user/core-user.service.js +44 -2
- package/dist/core/modules/user/core-user.service.js.map +1 -1
- package/dist/server/modules/user/user.resolver.d.ts +1 -1
- package/dist/server/modules/user/user.resolver.js +10 -10
- package/dist/server/modules/user/user.resolver.js.map +1 -1
- package/dist/server/modules/user/user.service.d.ts +0 -3
- package/dist/server/modules/user/user.service.js +2 -44
- package/dist/server/modules/user/user.service.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/core/modules/user/core-user.service.ts +77 -2
- package/src/server/modules/user/user.resolver.ts +12 -12
- package/src/server/modules/user/user.service.ts +3 -83
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lenne.tech/nest-server",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.2",
|
|
4
4
|
"description": "Modern, fast, powerful Node.js web framework in TypeScript based on Nest with a GraphQL API and a connection to MongoDB (or other databases).",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"node",
|
|
@@ -15,6 +15,9 @@ import { CoreUserCreateInput } from './inputs/core-user-create.input';
|
|
|
15
15
|
import { CoreUserInput } from './inputs/core-user.input';
|
|
16
16
|
import { Model } from 'mongoose';
|
|
17
17
|
import * as _ from 'lodash';
|
|
18
|
+
import * as crypto from 'crypto';
|
|
19
|
+
import envConfig from '../../../config.env';
|
|
20
|
+
import { EmailService } from '../../common/services/email.service';
|
|
18
21
|
|
|
19
22
|
// Subscription
|
|
20
23
|
const pubSub = new PubSub();
|
|
@@ -27,7 +30,7 @@ export abstract class CoreUserService<
|
|
|
27
30
|
TUserInput extends CoreUserInput,
|
|
28
31
|
TUserCreateInput extends CoreUserCreateInput
|
|
29
32
|
> extends CoreBasicUserService<TUser, TUserInput, TUserCreateInput> {
|
|
30
|
-
protected constructor(protected readonly userModel: Model<any
|
|
33
|
+
protected constructor(protected readonly userModel: Model<any>, protected emailService: EmailService) {
|
|
31
34
|
super(userModel);
|
|
32
35
|
}
|
|
33
36
|
|
|
@@ -42,8 +45,11 @@ export abstract class CoreUserService<
|
|
|
42
45
|
// Prepare input
|
|
43
46
|
await this.prepareInput(input, currentUser, { create: true });
|
|
44
47
|
|
|
48
|
+
// Generate verification token
|
|
49
|
+
const newUser = { ...input, ...{ verificationToken: crypto.randomBytes(32).toString('hex') } };
|
|
50
|
+
|
|
45
51
|
// Create new user
|
|
46
|
-
const createdUser = new this.userModel(this.model.map(
|
|
52
|
+
const createdUser = new this.userModel(this.model.map(newUser));
|
|
47
53
|
|
|
48
54
|
try {
|
|
49
55
|
// Save created user
|
|
@@ -115,6 +121,75 @@ export abstract class CoreUserService<
|
|
|
115
121
|
);
|
|
116
122
|
}
|
|
117
123
|
|
|
124
|
+
/**
|
|
125
|
+
* Verify user with token
|
|
126
|
+
*
|
|
127
|
+
* @param token
|
|
128
|
+
*/
|
|
129
|
+
async verify(token: string): Promise<boolean> {
|
|
130
|
+
const user = await this.userModel.findOne({ verificationToken: token }).exec();
|
|
131
|
+
|
|
132
|
+
if (!user) {
|
|
133
|
+
throw new NotFoundException();
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (!user.verificationToken) {
|
|
137
|
+
throw new Error('User has no token');
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (user.verified) {
|
|
141
|
+
throw new Error('User already verified');
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
await this.userModel.findByIdAndUpdate(user.id, { $set: { verified: true, verificationToken: null } }).exec();
|
|
145
|
+
|
|
146
|
+
return true;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Set newpassword for user with token
|
|
151
|
+
*
|
|
152
|
+
* @param token
|
|
153
|
+
* @param newPassword
|
|
154
|
+
*/
|
|
155
|
+
async resetPassword(token: string, newPassword: string): Promise<boolean> {
|
|
156
|
+
const user = await this.userModel.findOne({ passwordResetToken: token }).exec();
|
|
157
|
+
|
|
158
|
+
if (!user) {
|
|
159
|
+
throw new NotFoundException();
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const cryptedPassword = await bcrypt.hash(newPassword, 10);
|
|
163
|
+
await this.userModel
|
|
164
|
+
.findByIdAndUpdate(user.id, { $set: { password: cryptedPassword, passwordResetToken: null } })
|
|
165
|
+
.exec();
|
|
166
|
+
|
|
167
|
+
return true;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Request email with password reset link
|
|
172
|
+
*
|
|
173
|
+
* @param email
|
|
174
|
+
*/
|
|
175
|
+
async requestPasswordResetMail(email: string): Promise<boolean> {
|
|
176
|
+
const user = await this.userModel.findOne({ email }).exec();
|
|
177
|
+
|
|
178
|
+
if (!user) {
|
|
179
|
+
throw new NotFoundException();
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const resetToken = crypto.randomBytes(32).toString('hex');
|
|
183
|
+
await this.userModel.findByIdAndUpdate(user.id, { $set: { passwordResetToken: resetToken } }).exec();
|
|
184
|
+
|
|
185
|
+
await this.emailService.sendMail(user.email, 'Password reset', {
|
|
186
|
+
htmlTemplate: 'password-reset',
|
|
187
|
+
templateData: { name: user.username, link: envConfig.email.passwordResetLink + '/' + resetToken },
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
return true;
|
|
191
|
+
}
|
|
192
|
+
|
|
118
193
|
/**
|
|
119
194
|
* Set roles for specified user
|
|
120
195
|
*/
|
|
@@ -43,14 +43,6 @@ export class UserResolver {
|
|
|
43
43
|
return await this.usersService.find(args, info);
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
/**
|
|
47
|
-
* Verify user with email
|
|
48
|
-
*/
|
|
49
|
-
@Query((returns) => Boolean, { description: 'Verify user with email' })
|
|
50
|
-
async verifyUser(@Args('token') token: string) {
|
|
51
|
-
return await this.usersService.verify(token);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
46
|
/**
|
|
55
47
|
* Request new password for user with email
|
|
56
48
|
*/
|
|
@@ -59,17 +51,25 @@ export class UserResolver {
|
|
|
59
51
|
return await this.usersService.requestPasswordResetMail(email);
|
|
60
52
|
}
|
|
61
53
|
|
|
54
|
+
// ===========================================================================
|
|
55
|
+
// Mutations
|
|
56
|
+
// ===========================================================================
|
|
57
|
+
/**
|
|
58
|
+
* Verify user with email
|
|
59
|
+
*/
|
|
60
|
+
@Mutation((returns) => Boolean, { description: 'Verify user with email' })
|
|
61
|
+
async verifyUser(@Args('token') token: string) {
|
|
62
|
+
return await this.usersService.verify(token);
|
|
63
|
+
}
|
|
64
|
+
|
|
62
65
|
/**
|
|
63
66
|
* Set new password for user with token
|
|
64
67
|
*/
|
|
65
|
-
@
|
|
68
|
+
@Mutation((returns) => Boolean, { description: 'Set new password for user with token' })
|
|
66
69
|
async resetPassword(@Args('token') token: string, @Args('password') password: string) {
|
|
67
70
|
return await this.usersService.resetPassword(token, password);
|
|
68
71
|
}
|
|
69
72
|
|
|
70
|
-
// ===========================================================================
|
|
71
|
-
// Mutations
|
|
72
|
-
// ===========================================================================
|
|
73
73
|
/**
|
|
74
74
|
* Create new user
|
|
75
75
|
*/
|
|
@@ -1,10 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Inject,
|
|
3
|
-
Injectable,
|
|
4
|
-
NotFoundException,
|
|
5
|
-
UnauthorizedException,
|
|
6
|
-
UnprocessableEntityException,
|
|
7
|
-
} from '@nestjs/common';
|
|
1
|
+
import { Inject, Injectable, UnauthorizedException, UnprocessableEntityException } from '@nestjs/common';
|
|
8
2
|
import * as fs from 'fs';
|
|
9
3
|
import { GraphQLResolveInfo } from 'graphql';
|
|
10
4
|
import envConfig from '../../../config.env';
|
|
@@ -21,8 +15,6 @@ import { InjectModel } from '@nestjs/mongoose';
|
|
|
21
15
|
import { Model } from 'mongoose';
|
|
22
16
|
import { ICorePersistenceModel } from '../../../core/common/interfaces/core-persistence-model.interface';
|
|
23
17
|
import { PubSub } from 'graphql-subscriptions';
|
|
24
|
-
import * as crypto from 'crypto';
|
|
25
|
-
import * as bcrypt from 'bcrypt';
|
|
26
18
|
|
|
27
19
|
/**
|
|
28
20
|
* User service
|
|
@@ -50,7 +42,7 @@ export class UserService extends CoreUserService<User, UserInput, UserCreateInpu
|
|
|
50
42
|
@InjectModel('User') protected readonly userModel: Model<User>,
|
|
51
43
|
@Inject('PUB_SUB') protected readonly pubSub: PubSub
|
|
52
44
|
) {
|
|
53
|
-
super(userModel);
|
|
45
|
+
super(userModel, emailService);
|
|
54
46
|
this.model = User;
|
|
55
47
|
}
|
|
56
48
|
|
|
@@ -68,86 +60,14 @@ export class UserService extends CoreUserService<User, UserInput, UserCreateInpu
|
|
|
68
60
|
|
|
69
61
|
await this.pubSub.publish('userCreated', User.map(user));
|
|
70
62
|
|
|
71
|
-
const verificationToken = crypto.randomBytes(32).toString('hex');
|
|
72
|
-
await this.userModel.findByIdAndUpdate(user.id, { $set: { verificationToken } }).exec();
|
|
73
|
-
|
|
74
63
|
await this.emailService.sendMail(user.email, 'Welcome', {
|
|
75
64
|
htmlTemplate: 'welcome',
|
|
76
|
-
templateData: { name: user.username, link: envConfig.email.verificationLink + '/' + verificationToken },
|
|
65
|
+
templateData: { name: user.username, link: envConfig.email.verificationLink + '/' + user.verificationToken },
|
|
77
66
|
});
|
|
78
67
|
|
|
79
68
|
return user;
|
|
80
69
|
}
|
|
81
70
|
|
|
82
|
-
/**
|
|
83
|
-
* Verify user with token
|
|
84
|
-
*
|
|
85
|
-
* @param token
|
|
86
|
-
*/
|
|
87
|
-
async verify(token: string): Promise<boolean> {
|
|
88
|
-
const user = await this.userModel.findOne({ verificationToken: token }).exec();
|
|
89
|
-
|
|
90
|
-
if (!user) {
|
|
91
|
-
throw new NotFoundException();
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
if (!user.verificationToken) {
|
|
95
|
-
throw new Error('User has no token');
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
if (user.verified) {
|
|
99
|
-
throw new Error('User already verified');
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
await this.userModel.findByIdAndUpdate(user.id, { $set: { verified: true, verificationToken: null } }).exec();
|
|
103
|
-
|
|
104
|
-
return true;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Set newpassword for user with token
|
|
109
|
-
*
|
|
110
|
-
* @param token
|
|
111
|
-
* @param newPassword
|
|
112
|
-
*/
|
|
113
|
-
async resetPassword(token: string, newPassword: string): Promise<boolean> {
|
|
114
|
-
const user = await this.userModel.findOne({ passwordResetToken: token }).exec();
|
|
115
|
-
|
|
116
|
-
if (!user) {
|
|
117
|
-
throw new NotFoundException();
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
const cryptedPassword = await bcrypt.hash(newPassword, 10);
|
|
121
|
-
await this.userModel
|
|
122
|
-
.findByIdAndUpdate(user.id, { $set: { password: cryptedPassword, passwordResetToken: null } })
|
|
123
|
-
.exec();
|
|
124
|
-
|
|
125
|
-
return true;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Request email with password reset link
|
|
130
|
-
*
|
|
131
|
-
* @param email
|
|
132
|
-
*/
|
|
133
|
-
async requestPasswordResetMail(email: string): Promise<boolean> {
|
|
134
|
-
const user = await this.userModel.findOne({ email }).exec();
|
|
135
|
-
|
|
136
|
-
if (!user) {
|
|
137
|
-
throw new NotFoundException();
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
const resetToken = crypto.randomBytes(32).toString('hex');
|
|
141
|
-
await this.userModel.findByIdAndUpdate(user.id, { $set: { passwordResetToken: resetToken } }).exec();
|
|
142
|
-
|
|
143
|
-
await this.emailService.sendMail(user.email, 'Password reset', {
|
|
144
|
-
htmlTemplate: 'password-reset',
|
|
145
|
-
templateData: { name: user.username, link: envConfig.email.passwordResetLink + '/' + resetToken },
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
return true;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
71
|
/**
|
|
152
72
|
* Get users via filter
|
|
153
73
|
*/
|