Skip to content

Commit 0dd15ae

Browse files
committed
functional activate account
1 parent ef9dfa9 commit 0dd15ae

File tree

10 files changed

+203
-135
lines changed

10 files changed

+203
-135
lines changed

.env.dev

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ PORT=5000
22
NODE_ENV="development"
33
JWT_SECRET="thisismysupersecrettokenjustkidding"
44
DATABASE_URL="mongodb://localhost:27017/donut-development"
5+
SENDGRID_API_KEY = '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'

.env.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ PORT=3000
22
NODE_ENV=testing
33
JWT_SECRET=thisismysupersecrettokenjustkidding
44
DATABASE_URL=mongodb+srv://donut-admin:5cdS2C2g3wRAdQWp@donut-users-hdawt.mongodb.net/donut-testing?retryWrites=true&w=majority
5-
SENDGRID_API_KEY = 'SG.9RfagHPzTzSLBX5_xRdfw.ZfRhFaE_DPlqrV9_cFQrIYQ-gzNGXhYPvp-oVsQ_N3s'
5+
SENDGRID_API_KEY = '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'

app/controllers/email.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const sendgridMail = require('@sendgrid/mail')
2+
const ejs = require('ejs')
3+
const path = require('path')
4+
const sendGridApi = process.env.SENDGRID_API_KEY || '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'
5+
6+
sendgridMail.setApiKey(sendGridApi)
7+
8+
module.exports = {
9+
sendEmail: async (req, res, next, token) => {
10+
const filePath = path.join(__dirname, '/../../views/emailTemplate.ejs')
11+
ejs.renderFile(filePath, { token: token }, (err, data) => {
12+
if (err) {
13+
console.log('Error in renderFile ', err)
14+
} else {
15+
const message = {
16+
to: req.body.email,
17+
from: 'services@codeuino.com',
18+
subject: `Welcome to Donut ${req.body.name.firstName}`,
19+
html: data
20+
}
21+
sendgridMail.send(message).then(
22+
() => {
23+
console.log('sending email')
24+
},
25+
(error) => {
26+
console.log('error in sending email ', error)
27+
if (error.response) {
28+
console.error(error.response.body)
29+
}
30+
}
31+
)
32+
}
33+
})
34+
}
35+
}

app/controllers/user.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
const User = require('../models/User')
22
const jwt = require('jsonwebtoken')
33
const HttpStatus = require('http-status-codes')
4+
const emailController = require('./email')
45

56
module.exports = {
67
createUser: async (req, res, next) => {
78
const user = new User(req.body)
89
try {
910
await user.save()
1011
const token = await user.generateAuthToken()
11-
// send email here, to activate the account
12-
res.status(HttpStatus.CREATED).json({ user: user, token: token })
12+
// Added fn to send email to activate account with warm message
13+
await emailController.sendEmail(req, res, next, token)
14+
return res.status(HttpStatus.CREATED).json({ user: user, token: token })
1315
} catch (error) {
1416
console.log(error)
15-
res.status(HttpStatus.NOT_ACCEPTABLE).json({ error: error })
17+
return res.status(HttpStatus.NOT_ACCEPTABLE).json({ error: error })
1618
}
1719
},
1820

@@ -120,12 +122,12 @@ module.exports = {
120122
if (expiryTime <= Date.now()) {
121123
const user = await User.findById(decodedToken._id)
122124
if (!user) {
123-
res.status(HttpStatus.NOT_FOUND).json({ msg: 'User not found!' })
125+
return res.status(HttpStatus.NOT_FOUND).json({ msg: 'User not found!' })
124126
}
125127
// if user found activate the account
126128
user.isActivated = true
127129
await user.save()
128-
return res.status(HttpStatus.OK).json({ user: user })
130+
return res.status(HttpStatus.OK).json({ msg: 'Succesfully activated!' })
129131
}
130132
} catch (Error) {
131133
return res.status(HttpStatus.BAD_REQUEST).json({ Error })

app/middleware/email.js

Lines changed: 0 additions & 28 deletions
This file was deleted.

app/routes/user.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ router.post(
3838
)
3939

4040
// activate account
41-
router.post(
41+
router.get(
4242
'/activate/:token',
4343
userController.activateAccount
4444
)

test/event.test.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const HttpStatus = require('http-status-codes')
55
const request = require('supertest')
66
const Event = require('../app/models/Event')
77
const User = require('../app/models/User')
8+
const randomDigit = Math.floor(Math.random() * 90 + 10)
89

910
const testUserId = new mongoose.Types.ObjectId()
1011
const demoEvent = {
@@ -47,8 +48,8 @@ const demoUser = {
4748
firstName: 'test',
4849
lastName: 'test'
4950
},
50-
email: 'test3@mailinator.com',
51-
phone: '1234567890',
51+
email: `test${randomDigit}@mailinator.com`,
52+
phone: `12345678${randomDigit}`,
5253
password: 'abc12345',
5354
info: {
5455
about: {
@@ -81,14 +82,15 @@ const demoUser = {
8182
const testUser = {
8283
_id: testUserId,
8384
...demoUser,
84-
email: 'test@mailinator.com',
85-
phone: '1234567891',
85+
email: `test${randomDigit + Math.random() * 10}@mailinator.com`,
86+
phone: `12335678${randomDigit}`,
8687
tokens: [{
8788
token: jwt.sign({
8889
_id: testUserId
8990
}, 'process.env.JWT_SECRET')
9091
}]
9192
}
93+
9294
let server
9395
/**
9496
* This will pe performed once at the beginning of the test
@@ -145,8 +147,7 @@ test('Should signup new user', async () => {
145147
location: demoUser.info.about.location
146148
}
147149
}
148-
},
149-
token: user.tokens[0].token
150+
}
150151
})
151152
expect(user.password).not.toBe('abc12345') // to check hashing
152153
})

test/user.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ test('Should update the password ', async () => {
204204
/* Activate account */
205205
test('Should activate the account ', async () => {
206206
await request(app)
207-
.post(`/user/activate/${token}`)
207+
.get(`/user/activate/${token}`)
208208
.send({
209209
token: `${token}`
210210
})

views/emailTemplate.ejs

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<style></style>
5+
</head>
6+
<body link="#22247A" vlink="#22247A" alink="#22247A>
7+
<table class=" main contenttable" align="center"
8+
style="font-weight: normal; border-collapse: collapse;border: 0;margin-left: auto;margin-right: auto;padding: 0;font-family: Arial, sans-serif;color: #555559;background-color: white;font-size: 16px;line-height: 26px;width: 600px;">
9+
<tr>
10+
<td class="border"
11+
style="border-collapse: collapse;border: 1px solid #eeeff0;margin: 0;padding: 0;-webkit-text-size-adjust: none;color: #555559;font-family: Arial, sans-serif;font-size: 16px;line-height: 26px;">
12+
<table
13+
style="font-weight: normal;border-collapse: collapse;border: 0;margin: 0;padding: 0;font-family: Arial, sans-serif;">
14+
<tr>
15+
<td colspan="4" valign="top"
16+
class="image-section"
17+
style="border-collapse: collapse;border: 0;margin: 0;padding: 0;-webkit-text-size-adjust: none;color: #555559;font-family: Arial, sans-serif;font-size: 16px;line-height: 26px;background-color: #fff;border-bottom: 4px solid #44478e">
18+
<a href="https://codeuino.org">
19+
<img class="top-image"
20+
src="http://codeuino.org/static/media/newlogo.33c918eb.png"
21+
style="line-height: 1;width: 150px; padding:10px"
22+
alt="Codeuino org image">
23+
</a>
24+
</td>
25+
</tr>
26+
<tr>
27+
<td valign="top" class="side title"
28+
style="border-collapse: collapse;border: 0;margin: 0;padding: 20px;-webkit-text-size-adjust: none;color: #555559;font-family: Arial, sans-serif;font-size: 16px;line-height: 26px;vertical-align: top;background-color: white;border-top: none;">
29+
<table
30+
style="font-weight: normal;border-collapse: collapse;border: 0;margin: 0;padding: 0;font-family: Arial, sans-serif;">
31+
<tr>
32+
<td class="head-title"
33+
style="border-collapse: collapse;border: 0;margin: 0;padding: 0;-webkit-text-size-adjust: none;color: #555559;font-family: Arial, sans-serif;font-size: 28px;line-height: 34px;font-weight: bold; text-align: center;">
34+
<div class="mktEditable"
35+
id="main_title">
36+
Welcome to Donut
37+
</div>
38+
</td>
39+
</tr>
40+
<tr>
41+
<td class="sub-title"
42+
style="border-collapse: collapse;border: 0;margin: 0;padding: 0;padding-top:5px;-webkit-text-size-adjust: none;color: #555559;font-family: Arial, sans-serif;font-size: 18px;line-height: 29px;font-weight: bold;text-align: center;">
43+
</tr>
44+
<tr>
45+
<td class="top-padding"
46+
style="border-collapse: collapse;border: 0;margin: 0;padding: 5px;-webkit-text-size-adjust: none;color: #555559;font-family: Arial, sans-serif;font-size: 16px;line-height: 26px;">
47+
</td>
48+
</tr>
49+
<tr>
50+
<td class="grey-block"
51+
style="border-collapse: collapse;border: 0;margin: 0;-webkit-text-size-adjust: none;color: #555559;font-family: Arial, sans-serif;font-size: 16px;line-height: 26px;background-color: #fff; text-align:center;">
52+
<div class="mktEditable" id="cta">
53+
<img class="top-image"
54+
src="https://i.imgur.com/6bAOR4M.png"
55+
width="300" /><br><br>
56+
</div>
57+
</td>
58+
</tr>
59+
<tr>
60+
<hr size="1" color="#22247A">
61+
</td>
62+
</tr>
63+
<tr>
64+
<td class="text"
65+
style="border-collapse: collapse;border: 0;margin: 0;padding: 0;-webkit-text-size-adjust: none;color: #555559;font-family: Arial, sans-serif;font-size: 16px;line-height: 26px;">
66+
<div class="mktEditable" id="main_text">
67+
<br>
68+
<br>
69+
Your registration is successful! We warmly
70+
welcome you to the donut platform!
71+
<br>
72+
<br>
73+
Donut is an open-source, feature-rich,
74+
highly flexible and privacy-friendly, social
75+
networking platform built for
76+
community-oriented collaboration in a
77+
customized way. It has been built on the
78+
Node.js framework allowing an essential
79+
impetus to provide custom and friendly rich
80+
widgets and an expansive library of modules
81+
to make communication and collaboration easy
82+
and successful. With a powerful module
83+
system, you can customize this platform by
84+
using third party tools, writing your own or
85+
integrating other software.
86+
<br />
87+
<p>Please use the given below link if the button does not work properly.</p>
88+
<p>http://localhost:5000/user/activate/<%= token %></p>
89+
<center>
90+
<a href="' + http://localhost:5000/user/activate/ + <%= token %> + '"
91+
style="background-color: #aad1f2;font-family: Arial, sans-serif;font-size: 16px;line-height: 26px; color: black">
92+
Activate account
93+
</a>
94+
</center>
95+
<br />
96+
Hope you enjoy your stay at Donut!
97+
<br />
98+
</div>
99+
</td>
100+
</tr>
101+
<tr>
102+
<td
103+
style="border-collapse: collapse;border: 0;margin: 0;padding: 0;-webkit-text-size-adjust: none;color: #555559;font-family: Arial, sans-serif;font-size: 16px;line-height: 24px;">
104+
&nbsp;
105+
</td>
106+
</tr>
107+
<tr>
108+
<td class="text"
109+
style="border-collapse: collapse;border: 0;margin: 0;padding: 0;-webkit-text-size-adjust: none;color: #555559;font-family: Arial, sans-serif;font-size: 16px;line-height: 24px;">
110+
<div class="mktEditable" id="download_button"
111+
style="text-align: center;"></div>
112+
</td>
113+
</tr>
114+
</table>
115+
</td>
116+
</tr>
117+
<tr>
118+
<td
119+
style="padding:20px; font-family: Arial, sans-serif; -webkit-text-size-adjust: none;"
120+
align="center">
121+
<table>
122+
<tr>
123+
<td align="center"
124+
style="font-family: Arial, sans-serif; -webkit-text-size-adjust: none; font-size: 16px;">
125+
</td>
126+
</tr>
127+
</table>
128+
</td>
129+
</tr>
130+
<tr bgcolor="#fff"
131+
style="border-top: 4px solid #44478e;">
132+
<td valign="top" class="footer"
133+
style="border-collapse: collapse;border: 0;margin: 0;padding: 0;-webkit-text-size-adjust: none;color: #555559;font-family: Arial, sans-serif;font-size: 16px;line-height: 26px;background: #fff;text-align: center;">
134+
<table
135+
style="font-weight: normal;border-collapse: collapse;border: 0;margin: 0;padding: 0;font-family: Arial, sans-serif;">
136+
<tr>
137+
<td class="inside-footer" align="center"
138+
valign="middle"
139+
style="border-collapse: collapse;border: 0;margin: 0;padding: 20px;-webkit-text-size-adjust: none;color: #555559;font-family: Arial, sans-serif;font-size: 12px;line-height: 16px;vertical-align: middle;text-align: center;width: 580px;">
140+
</td>
141+
</tr>
142+
</table>
143+
</td>
144+
</trbgcolor=>
145+
</table>
146+
</td>
147+
</tr>
148+
</table>
149+
</body>
150+
</html>

0 commit comments

Comments
 (0)