Repository navigation
Expand file tree
/
Copy pathutils.js
More file actions
162 lines (147 loc) · 3.74 KB
/
Copy pathutils.js
File metadata and controls
162 lines (147 loc) · 3.74 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
import jwt from 'jsonwebtoken';
import mg from 'mailgun-js';
import nodemailer from 'nodemailer';
export const errorHandler = (status, message) => {
const err = new Error();
err.status = status;
err.message = message;
return err;
};
export const baseUrl = () =>
process.env.BASE_URL
? process.env.BASE_URL
: process.env.NODE_ENV !== 'production'
? 'http://localhost:3000'
: 'http://aeb9f366db7a84363bae4aba67112d0b-1133126064.us-east-2.elb.amazonaws.com';
export const generateToken = (user) => {
return jwt.sign(
{
_id: user._id,
name: user.name,
email: user.email,
isAdmin: user.isAdmin,
username: user.username,
birthday: user.birthday,
},
process.env.JWT_SECRET,
{
expiresIn: '30d',
}
);
};
export const isAuth = (req, res, next) => {
const authorization = req.headers.authorization;
if (authorization) {
const token = authorization.slice(7, authorization.length); // Bearer XXXXXX
jwt.verify(token, process.env.JWT_SECRET, (err, decode) => {
if (err) {
res.status(401).send({ message: 'Invalid Token' });
} else {
req.user = decode;
next();
}
});
} else {
res.status(401).send({ message: 'No Token' });
}
};
export const isAdmin = (req, res, next) => {
if (req.user && req.user.isAdmin) {
next();
} else {
res.status(401).send({ message: 'Invalid Admin Token' });
}
};
export const mailgun = () =>
mg({
apiKey: process.env.MAILGUN_API_KEY,
domain: process.env.MAILGUN_DOMIAN,
});
export const sendEmail = async (email, subject, url) => {
try {
const transporter = nodemailer.createTransport({
service: 'gmail',
port: 465,
secure: true,
secureConnection: false,
auth: {
user: 'engineerfranklyn@gmail.com',
pass: process.env.PASS,
},
tls: {
rejectUnauthorized: true,
},
});
await transporter.sendMail({
from: process.env.USER,
to: email,
subject: subject,
html: `
<p>Please Click the following link to reset your password:</p>
<p>${url}</p>
`,
});
// res.send({ message: "We sent reset password link to your email." });
} catch (error) {
console.log('Email not sent');
console.log(error);
}
};
export const payOrderEmailTemplate = (order) => {
return `<h1>Thanks for shopping with us</h1>
<p>
Hi ${order.user.name},</p>
<p>We have finished processing your order.</p>
<h2>[Order ${order._id}] (${order.createdAt.toString().substring(0, 10)})</h2>
<table>
<thead>
<tr>
<td><strong>Product</strong></td>
<td><strong>Quantity</strong></td>
<td><strong align="right">Price</strong></td>
</thead>
<tbody>
${order.orderItems
.map(
(item) => `
<tr>
<td>${item.name}</td>
<td align="center">${item.quantity}</td>
<td align="right"> $${item.price.toFixed(2)}</td>
</tr>
`
)
.join('\n')}
</tbody>
<tfoot>
<tr>
<td colspan="2">Items Price:</td>
<td align="right"> $${order.itemsPrice.toFixed(2)}</td>
</tr>
<tr>
<td colspan="2">Shipping Price:</td>
<td align="right"> $${order.shippingPrice.toFixed(2)}</td>
</tr>
<tr>
<td colspan="2"><strong>Total Price:</strong></td>
<td align="right"><strong> $${order.totalPrice.toFixed(2)}</strong></td>
</tr>
<tr>
<td colspan="2">Payment Method:</td>
<td align="right">${order.paymentMethod}</td>
</tr>
</table>
<h2>Shipping address</h2>
<p>
${order.shippingAddress.fullName},<br/>
${order.shippingAddress.address},<br/>
${order.shippingAddress.city},<br/>
${order.shippingAddress.country},<br/>
${order.shippingAddress.postalCode}<br/>
</p>
<hr/>
<p>
Thanks for shopping with us.
</p>
`;
};