@lenne.tech/nest-server 8.4.0 → 8.6.1
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/common/decorators/restricted.decorator.d.ts +3 -2
- package/dist/core/common/decorators/restricted.decorator.js +29 -8
- package/dist/core/common/decorators/restricted.decorator.js.map +1 -1
- package/dist/core/common/helpers/db.helper.d.ts +1 -1
- package/dist/core/common/helpers/db.helper.js +29 -5
- package/dist/core/common/helpers/db.helper.js.map +1 -1
- package/dist/core/common/helpers/service.helper.d.ts +12 -0
- package/dist/core/common/helpers/service.helper.js +42 -3
- package/dist/core/common/helpers/service.helper.js.map +1 -1
- package/dist/core/common/interfaces/prepare-input-options.interface.d.ts +8 -0
- package/dist/core/common/interfaces/prepare-input-options.interface.js +3 -0
- package/dist/core/common/interfaces/prepare-input-options.interface.js.map +1 -0
- package/dist/core/common/interfaces/prepare-output-options.interface.d.ts +7 -0
- package/dist/core/common/interfaces/prepare-output-options.interface.js +3 -0
- package/dist/core/common/interfaces/prepare-output-options.interface.js.map +1 -0
- package/dist/core/common/interfaces/service-options.interface.d.ts +5 -15
- package/dist/core/common/services/module.service.d.ts +1 -1
- package/dist/core/common/services/module.service.js +14 -8
- package/dist/core/common/services/module.service.js.map +1 -1
- package/dist/core/modules/auth/inputs/core-auth-sign-in.input.d.ts +5 -0
- package/dist/core/modules/auth/inputs/core-auth-sign-in.input.js +34 -0
- package/dist/core/modules/auth/inputs/core-auth-sign-in.input.js.map +1 -0
- package/dist/core/modules/auth/inputs/core-auth-sign-up.input.d.ts +5 -0
- package/dist/core/modules/auth/inputs/core-auth-sign-up.input.js +34 -0
- package/dist/core/modules/auth/inputs/core-auth-sign-up.input.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/server/modules/auth/auth.model.js +2 -2
- package/dist/server/modules/auth/auth.model.js.map +1 -1
- package/dist/server/modules/auth/auth.module.js +7 -2
- package/dist/server/modules/auth/auth.module.js.map +1 -1
- package/dist/server/modules/auth/auth.resolver.d.ts +8 -3
- package/dist/server/modules/auth/auth.resolver.js +33 -10
- package/dist/server/modules/auth/auth.resolver.js.map +1 -1
- package/dist/server/modules/auth/auth.service.d.ts +15 -0
- package/dist/server/modules/auth/auth.service.js +71 -0
- package/dist/server/modules/auth/auth.service.js.map +1 -0
- package/dist/server/modules/auth/inputs/auth-sign-in.input.d.ts +3 -0
- package/dist/server/modules/auth/inputs/auth-sign-in.input.js +18 -0
- package/dist/server/modules/auth/inputs/auth-sign-in.input.js.map +1 -0
- package/dist/server/modules/auth/inputs/auth-sign-up.input.d.ts +5 -0
- package/dist/server/modules/auth/inputs/auth-sign-up.input.js +34 -0
- package/dist/server/modules/auth/inputs/auth-sign-up.input.js.map +1 -0
- package/dist/server/modules/user/user.resolver.js +12 -11
- package/dist/server/modules/user/user.resolver.js.map +1 -1
- package/dist/server/modules/user/user.service.js +0 -4
- package/dist/server/modules/user/user.service.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/src/core/common/decorators/restricted.decorator.ts +50 -14
- package/src/core/common/helpers/db.helper.ts +27 -5
- package/src/core/common/helpers/service.helper.ts +72 -2
- package/src/core/common/interfaces/prepare-input-options.interface.ts +11 -0
- package/src/core/common/interfaces/prepare-output-options.interface.ts +10 -0
- package/src/core/common/interfaces/service-options.interface.ts +7 -17
- package/src/core/common/services/module.service.ts +17 -9
- package/src/core/modules/auth/inputs/core-auth-sign-in.input.ts +18 -0
- package/src/core/modules/auth/inputs/core-auth-sign-up.input.ts +18 -0
- package/src/index.ts +4 -0
- package/src/server/modules/auth/auth.model.ts +5 -5
- package/src/server/modules/auth/auth.module.ts +13 -2
- package/src/server/modules/auth/auth.resolver.ts +30 -12
- package/src/server/modules/auth/auth.service.ts +83 -0
- package/src/server/modules/auth/inputs/auth-sign-in.input.ts +10 -0
- package/src/server/modules/auth/inputs/auth-sign-up.input.ts +18 -0
- package/src/server/modules/user/user.resolver.ts +12 -11
- package/src/server/modules/user/user.service.ts +0 -8
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { Injectable, UnauthorizedException } from '@nestjs/common';
|
|
2
|
+
import { JwtService } from '@nestjs/jwt';
|
|
3
|
+
import * as bcrypt from 'bcrypt';
|
|
4
|
+
import envConfig from '../../../config.env';
|
|
5
|
+
import { prepareServiceOptions } from '../../../core/common/helpers/service.helper';
|
|
6
|
+
import { ServiceOptions } from '../../../core/common/interfaces/service-options.interface';
|
|
7
|
+
import { EmailService } from '../../../core/common/services/email.service';
|
|
8
|
+
import { JwtPayload } from '../../../core/modules/auth/interfaces/jwt-payload.interface';
|
|
9
|
+
import { UserService } from '../user/user.service';
|
|
10
|
+
import { Auth } from './auth.model';
|
|
11
|
+
import { AuthSignInInput } from './inputs/auth-sign-in.input';
|
|
12
|
+
import { AuthSignUpInput } from './inputs/auth-sign-up.input';
|
|
13
|
+
|
|
14
|
+
@Injectable()
|
|
15
|
+
export class AuthService {
|
|
16
|
+
constructor(
|
|
17
|
+
protected readonly jwtService: JwtService,
|
|
18
|
+
protected readonly emailService: EmailService,
|
|
19
|
+
protected readonly userService: UserService
|
|
20
|
+
) {}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Sign in for user
|
|
24
|
+
*/
|
|
25
|
+
async signIn(input: AuthSignInInput, serviceOptions?: ServiceOptions): Promise<Auth> {
|
|
26
|
+
// Prepare service options
|
|
27
|
+
const serviceOptionsForUserService = prepareServiceOptions(serviceOptions, {
|
|
28
|
+
// We need password, so we can't use prepare output handling and have to deactivate it
|
|
29
|
+
prepareOutput: null,
|
|
30
|
+
|
|
31
|
+
// Select user field for automatic populate handling via user service
|
|
32
|
+
subFieldSelection: 'user',
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
// Get and check user
|
|
36
|
+
const user = await this.userService.getViaEmail(input.email, serviceOptionsForUserService);
|
|
37
|
+
if (!user) {
|
|
38
|
+
throw new UnauthorizedException();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Check password
|
|
42
|
+
if (!(await bcrypt.compare(input.password, user.password))) {
|
|
43
|
+
throw new UnauthorizedException();
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Create JWT and return sign-in data
|
|
47
|
+
const payload: JwtPayload = { email: user.email };
|
|
48
|
+
return Auth.map({
|
|
49
|
+
token: this.jwtService.sign(payload),
|
|
50
|
+
user,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Register a new user Account
|
|
56
|
+
*/
|
|
57
|
+
async signUp(input: AuthSignUpInput, serviceOptions?: ServiceOptions): Promise<Auth> {
|
|
58
|
+
// Prepare service options
|
|
59
|
+
const serviceOptionsForUserService = prepareServiceOptions(serviceOptions, {
|
|
60
|
+
// Select user field for automatic populate handling via user service
|
|
61
|
+
subFieldSelection: 'user',
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
// Get and check user
|
|
65
|
+
const user = await this.userService.create(input, serviceOptionsForUserService);
|
|
66
|
+
if (!user) {
|
|
67
|
+
throw Error('Email Address already in use');
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Send email
|
|
71
|
+
await this.emailService.sendMail(user.email, 'Welcome', {
|
|
72
|
+
htmlTemplate: 'welcome',
|
|
73
|
+
templateData: { name: user.username, link: envConfig.email.verificationLink + '/' + user.verificationToken },
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
// Create JWT and return sign-in data
|
|
77
|
+
const payload: JwtPayload = { email: user.email };
|
|
78
|
+
return Auth.map({
|
|
79
|
+
token: this.jwtService.sign(payload),
|
|
80
|
+
user: user,
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { InputType } from '@nestjs/graphql';
|
|
2
|
+
import { CoreAuthSignInInput } from '../../../../core/modules/auth/inputs/core-auth-sign-in.input';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* SignIn input
|
|
6
|
+
*/
|
|
7
|
+
@InputType({ description: 'Sign-in input' })
|
|
8
|
+
export class AuthSignInInput extends CoreAuthSignInInput {
|
|
9
|
+
// Extend UserInput here
|
|
10
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Field, InputType } from '@nestjs/graphql';
|
|
2
|
+
import { CoreAuthSignUpInput } from '../../../../core/modules/auth/inputs/core-auth-sign-up.input';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* SignUp input
|
|
6
|
+
*/
|
|
7
|
+
@InputType({ description: 'Sign-up input' })
|
|
8
|
+
export class AuthSignUpInput extends CoreAuthSignUpInput {
|
|
9
|
+
// ===================================================================================================================
|
|
10
|
+
// Properties
|
|
11
|
+
// ===================================================================================================================
|
|
12
|
+
|
|
13
|
+
@Field({ description: 'firstName', nullable: true })
|
|
14
|
+
firstName: string = undefined;
|
|
15
|
+
|
|
16
|
+
@Field({ description: 'lastName', nullable: true })
|
|
17
|
+
lastName: string = undefined;
|
|
18
|
+
}
|
|
@@ -14,7 +14,7 @@ import { UserService } from './user.service';
|
|
|
14
14
|
/**
|
|
15
15
|
* Resolver to process with user data
|
|
16
16
|
*/
|
|
17
|
-
@Resolver((
|
|
17
|
+
@Resolver(() => User)
|
|
18
18
|
export class UserResolver {
|
|
19
19
|
/**
|
|
20
20
|
* Import services
|
|
@@ -29,7 +29,7 @@ export class UserResolver {
|
|
|
29
29
|
* Get users (via filter)
|
|
30
30
|
*/
|
|
31
31
|
@Roles(RoleEnum.ADMIN)
|
|
32
|
-
@Query((
|
|
32
|
+
@Query(() => [User], { description: 'Find users (via filter)' })
|
|
33
33
|
async findUsers(@Info() info: GraphQLResolveInfo, @Args() args?: FilterArgs) {
|
|
34
34
|
return await this.userService.find(args, {
|
|
35
35
|
fieldSelection: { info, select: 'findUsers' },
|
|
@@ -41,7 +41,7 @@ export class UserResolver {
|
|
|
41
41
|
* Get user via ID
|
|
42
42
|
*/
|
|
43
43
|
@Roles(RoleEnum.S_USER)
|
|
44
|
-
@Query((
|
|
44
|
+
@Query(() => User, { description: 'Get user with specified ID' })
|
|
45
45
|
async getUser(@Args('id') id: string, @Info() info: GraphQLResolveInfo, @GraphQLUser() user: User): Promise<User> {
|
|
46
46
|
return await this.userService.get(id, {
|
|
47
47
|
currentUser: user,
|
|
@@ -53,7 +53,7 @@ export class UserResolver {
|
|
|
53
53
|
/**
|
|
54
54
|
* Get verified state of user with token
|
|
55
55
|
*/
|
|
56
|
-
@Query((
|
|
56
|
+
@Query(() => Boolean, { description: 'Get verified state of user with token' })
|
|
57
57
|
async getVerifiedState(@Args('token') token: string) {
|
|
58
58
|
return await this.userService.getVerifiedState(token);
|
|
59
59
|
}
|
|
@@ -61,7 +61,7 @@ export class UserResolver {
|
|
|
61
61
|
/**
|
|
62
62
|
* Request new password for user with email
|
|
63
63
|
*/
|
|
64
|
-
@Query((
|
|
64
|
+
@Query(() => Boolean, { description: 'Request new password for user with email' })
|
|
65
65
|
async requestPasswordResetMail(@Args('email') email: string): Promise<boolean> {
|
|
66
66
|
return !!(await this.userService.sendPasswordResetMail(email));
|
|
67
67
|
}
|
|
@@ -73,7 +73,8 @@ export class UserResolver {
|
|
|
73
73
|
/**
|
|
74
74
|
* Create new user
|
|
75
75
|
*/
|
|
76
|
-
@
|
|
76
|
+
@Roles(RoleEnum.ADMIN)
|
|
77
|
+
@Mutation(() => User, { description: 'Create a new user' })
|
|
77
78
|
async createUser(
|
|
78
79
|
@Args('input') input: UserCreateInput,
|
|
79
80
|
@GraphQLUser() user: User,
|
|
@@ -90,7 +91,7 @@ export class UserResolver {
|
|
|
90
91
|
* Delete existing user
|
|
91
92
|
*/
|
|
92
93
|
@Roles(RoleEnum.S_USER)
|
|
93
|
-
@Mutation((
|
|
94
|
+
@Mutation(() => User, { description: 'Delete existing user' })
|
|
94
95
|
async deleteUser(@Args('id') id: string, @Info() info: GraphQLResolveInfo, @GraphQLUser() user: User): Promise<User> {
|
|
95
96
|
return await this.userService.delete(id, {
|
|
96
97
|
currentUser: user,
|
|
@@ -102,7 +103,7 @@ export class UserResolver {
|
|
|
102
103
|
/**
|
|
103
104
|
* Set new password for user with token
|
|
104
105
|
*/
|
|
105
|
-
@Mutation((
|
|
106
|
+
@Mutation(() => Boolean, { description: 'Set new password for user with token' })
|
|
106
107
|
async resetPassword(@Args('token') token: string, @Args('password') password: string): Promise<boolean> {
|
|
107
108
|
return !!(await this.userService.resetPassword(token, password));
|
|
108
109
|
}
|
|
@@ -111,7 +112,7 @@ export class UserResolver {
|
|
|
111
112
|
* Update existing user
|
|
112
113
|
*/
|
|
113
114
|
@Roles(RoleEnum.S_USER)
|
|
114
|
-
@Mutation((
|
|
115
|
+
@Mutation(() => User, { description: 'Update existing user' })
|
|
115
116
|
async updateUser(
|
|
116
117
|
@Args('input') input: UserInput,
|
|
117
118
|
@Args('id') id: string,
|
|
@@ -130,7 +131,7 @@ export class UserResolver {
|
|
|
130
131
|
/**
|
|
131
132
|
* Verify user with email
|
|
132
133
|
*/
|
|
133
|
-
@Mutation((
|
|
134
|
+
@Mutation(() => Boolean, { description: 'Verify user with email' })
|
|
134
135
|
async verifyUser(@Args('token') token: string): Promise<boolean> {
|
|
135
136
|
return !!(await this.userService.verify(token));
|
|
136
137
|
}
|
|
@@ -142,7 +143,7 @@ export class UserResolver {
|
|
|
142
143
|
/**
|
|
143
144
|
* Subscription for created user
|
|
144
145
|
*/
|
|
145
|
-
@Subscription((
|
|
146
|
+
@Subscription(() => User, {
|
|
146
147
|
filter(this: UserResolver, payload, variables, context) {
|
|
147
148
|
return context.user.roles.include(RoleEnum.ADMIN);
|
|
148
149
|
},
|
|
@@ -58,20 +58,12 @@ export class UserService extends CoreUserService<User, UserInput, UserCreateInpu
|
|
|
58
58
|
await this.pubSub.publish('userCreated', User.map(user));
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
-
// Send email
|
|
62
|
-
await this.emailService.sendMail(user.email, 'Welcome', {
|
|
63
|
-
htmlTemplate: 'welcome',
|
|
64
|
-
templateData: { name: user.username, link: envConfig.email.verificationLink + '/' + user.verificationToken },
|
|
65
|
-
});
|
|
66
|
-
|
|
67
61
|
// Return created user
|
|
68
62
|
return user;
|
|
69
63
|
}
|
|
70
64
|
|
|
71
65
|
/**
|
|
72
66
|
* Request password reset mail
|
|
73
|
-
*
|
|
74
|
-
* @param email
|
|
75
67
|
*/
|
|
76
68
|
async sendPasswordResetMail(email: string, serviceOptions?: ServiceOptions): Promise<User> {
|
|
77
69
|
// Set password reset token
|