-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserControllers.ts
More file actions
175 lines (140 loc) · 5.81 KB
/
Copy pathUserControllers.ts
File metadata and controls
175 lines (140 loc) · 5.81 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { body, validationResult } from 'express-validator'
import User from '../modals/User';
// import { NodeMailer } from '../utils/NodeMailer';
import { Utils } from '../utils/Utils';
import * as Bcrypt from 'bcrypt';
import * as Jwt from 'jsonwebtoken';
import { getEnvironmentVariable } from '../environments/env';
import { Emailjs } from '../utils/Emailjs';
import { EmailTemplate } from '../utils/TemplateEmailjs';
import { date } from 'joi';
export class UserController {
static async signup(req, res, next) {
let d = req.body;
const email = d.email;
const password = d.password;
const username = d.username;
const verificationToken = Utils.generateVerificationToken();
let MAX_TOKEN_TIME = new Utils().MAX_TOKEN_TIME;
try {
const hash = await Utils.encryptPassword(password);
const data = {
email: email,
password: hash,
username: username,
verified: false,
verification_token: verificationToken,
verification_token_time: Date.now() + MAX_TOKEN_TIME
}
let user = await new User(data).save();
res.send(user);
//SEND VERIFICATION EMAIL
let templateParams = { name: data.username, verificationToken: data.verification_token, to: email };
let templateId = new EmailTemplate().emailTemplate.emailVerification.templateId;
Emailjs.sendEmail({ template_id: templateId, template_params: templateParams });
}
catch (e) {
next(e);
}
}
static async verify(req, res, next) {
const verificationToken = req.body.verification_token;
const email = req.body.email;
try {
const user = await User.findOneAndUpdate({
email: email,
verification_token: parseInt(verificationToken),
verification_token_time: { $gt: Date.now() }
}, { verified: true }, { new: true });
if (user) {
res.send(user);
} else {
throw new Error('Verification Token Is Expired. PLease Request For a new One.')
}
} catch (e) {
next(e)
}
}
static async resendVerificationEmail(req, res, next) {
const email = req.query.email;
const verificationToken = Utils.generateVerificationToken();
try {
const user: any = await User.findOneAndUpdate({ email: email }, {
verification_token: verificationToken,
verification_token_time: Date.now() + new Utils().MAX_TOKEN_TIME
})
if (user) {
//SEND VERIFICATION EMAIL
let templateParams = { name: user.username, verificationToken: verificationToken, to: email };
let templateId = new EmailTemplate().emailTemplate.emailVerification.templateId;
Emailjs.sendEmail({ template_id: templateId, template_params: templateParams });
res.json({ success: true })
} else {
throw Error('User Does not Exist')
}
}
catch (e) {
next(e)
}
}
static async login(req, res, next) {
let d = req.body;
const email = d.email;
const password = d.password;
const user = req.user;
try {
await Utils.comparePassword({ plainPassword: password, encryptPassword: user.password });
const data = { user_id: req.user._id, email: req.user.email }
const token = Jwt.sign(data, getEnvironmentVariable().jwt_secret, { expiresIn: '120d' });
const response = { user: user, toke: token };
res.json(response)
} catch (e) {
next(e);
}
}
static async updatePassword(req, res, next) {
const user = req.user;
const password = req.body.password;
const newPassword = req.body.new_password;
try {
await User.findOne({ _id: user._id }).then(async (user: any) => {
await Utils.comparePassword({ plainPassword: password, encryptPassword: user.password })
});
const encryptPassword = await Utils.encryptPassword(newPassword);
const newUser = await User.findOneAndUpdate({ _id: user._id }, { password: encryptPassword }, { new: true });
res.send(newUser);
}
catch (e) {
next(e);
}
}
static async sendResetPassword(req, res, next) {
const email = req.query.email;
const resetPasswordToken = Utils.generateVerificationToken();
try {
let updateUser: any = await User.findOneAndUpdate({ email: email },
{
updated_at: new Date(), reset_password_token: resetPasswordToken,
reset_password_token_time: Date.now() + new Utils().MAX_TOKEN_TIME
}, { new: true });
res.send(updateUser);
//SEND RESET VERIFICATION EMAIL
let templateParams = { name: updateUser.username, verificationToken: resetPasswordToken, to: email };
let templateId = new EmailTemplate().emailTemplate.resetPaswordEmail.templateId;
Emailjs.sendEmail({ template_id: templateId, template_params: templateParams });
} catch (e) {
next(e)
}
}
static async updateProfilePic(req, res, next) {
try {
const userId = req.user.user_id;
const fileUrl = 'https://localhost:5000/' + req.file.path
const user = await User.findOneAndUpdate({ _id: userId },
{ updated_at: new Date(), profile_pic_url: fileUrl }, { new: true })
res.send(user)
} catch (e) {
next(e)
}
}
}