-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.ts
More file actions
76 lines (61 loc) · 1.97 KB
/
Copy pathservice.ts
File metadata and controls
76 lines (61 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import * as Joi from 'joi';
import AuthValidation from './validation';
import UserModel, { IUserModel } from '../User/model';
import { IAuthService } from './interface';
/**
* @export
* @implements {IAuthService}
*/
const AuthService: IAuthService = {
/**
* @param {IUserModel} body
* @returns {Promise <IUserModel>}
* @memberof AuthService
*/
async createUser(body: IUserModel): Promise < IUserModel > {
try {
const validate: Joi.ValidationResult = AuthValidation.createUser(body);
if (validate.error) {
throw new Error(validate.error.message);
}
const user: IUserModel = new UserModel({
email: body.email,
password: body.password
});
const query: IUserModel = await UserModel.findOne({
email: body.email
});
if (query) {
throw new Error('This email already exists');
}
const saved: IUserModel = await user.save();
return saved;
} catch (error) {
throw new Error(error);
}
},
/**
* @param {IUserModel} body
* @returns {Promise <IUserModel>}
* @memberof AuthService
*/
async getUser(body: IUserModel): Promise < IUserModel > {
try {
const validate: Joi.ValidationResult = AuthValidation.getUser(body);
if (validate.error) {
throw new Error(validate.error.message);
}
const user: IUserModel = await UserModel.findOne({
email: body.email
});
const isMatched: boolean = user && await user.comparePassword(body.password);
if (isMatched) {
return user;
}
throw new Error('Invalid password or email');
} catch (error) {
throw new Error(error);
}
}
};
export default AuthService;