-
Notifications
You must be signed in to change notification settings - Fork 1
/
account.js
243 lines (215 loc) · 7.41 KB
/
account.js
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
var accountManager = function(cm) {
var mongojs = require('mongojs');
var db = mongojs('mongodb://studyspace:[email protected]:33086/studyspace', []);
var COOKIE_TIME = 7*24*60*60*1000; //one week
function User(email, password, name, school) {
//this._id = whatever mongo gives us
this.user_id = generateToken(20);
this.email = email;
this.password = password;
this.name = name;
this.school = school;
this.token = "dank"; //generateToken();
this.active = true; //has verified email
}
function generateToken(num) {
var token = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
if (num && num > 0) {
for(var i = 0; i < num; i++ ) {
token += possible.charAt(Math.floor(Math.random() * possible.length));
}
}
else {
for(var i = 0; i < 10; i++ ) {
token += possible.charAt(Math.floor(Math.random() * possible.length));
}
}
return token;
}
this.signup = function(name, school, email, password, callback) {
console.log("Account signup: attempt with - " + email);
db.users.findOne({email:email}, function (err, doc) {
//if user doesn't exist yet (doc is null), insert it in
if (!doc) {
var newUser = new User(email, password, name, school);
db.users.insert(newUser, function(err, doc) {
if (doc) {
console.log("Account signup: ACCOUNT CREATED");
//sendVerifyEmail(newUser); NEED TO ADD MODULE STUFF
callback(null, {user_id: doc.user_id, email: doc.email, name: doc.name});
}
else {
console.log("Account signup: SOMETHING WEIRD HAPPENED");
callback("error", null);
} });
}
else {
console.log("Account signup: error - account already exists");
callback("error", null);
}
});
}
this.login = function(email, password, callback) {
db.users.findOne({email: email, password: password}, function (err, doc) {
if (doc) {
console.log("LOGIN: " + email);
callback(null, {user_id: doc.user_id, email: doc.email, name: doc.name});
}
else {
callback("error", null);
}
});
}
this.verify = function(id, token, callback) {
if (id.length == 24) {
db.users.findOne({_id: mongojs.ObjectId(id)}, function(err, doc) {
//check if user exists
if (doc) {
//verify the user if the token matches
if (token == doc.token) {
db.users.findAndModify({query: {_id: mongojs.ObjectId(id)},
update: {$set: {active: true}}, new: true}, function(err, doc) {
if (doc) {
console.log("Account verification: ACCOUNT VERIFIED");
callback(null, {success: true});
}
else {
console.log("WEIRD ASS ERROR - ACCOUNT EXISTS, BUT CAN'T MODIFY");
callback("error", null);
}
});
}
//either some guy tryna hack or some typo happened
else {
console.log("Account verification: error - wrong token");
callback("error", null);
}
}
//either some guy tryna hack or some typo happened
else {
console.log("Account verification: error - non-existent account");
callback("error", null);
}
});
}
else {
console.log("Account verification: error - impossible ID");
callback("error", null);
}
}
this.sendForgotPassword = function(email, callback) {
db.users.findOne({email: email}, function(err, doc) {
//check if user with email exists
if (doc) {
db.users.findAndModify({query: {email: email},
update: {$set: {resetToken: generateToken()}}, new: true}, function(err, doc) {
if (doc) {
console.log("Account forgot password: sending reset link");
sendForgotPassword(doc);
callback(null, {success: true});
}
else {
console.log("WEIRD ASS ERROR - ACCOUNT EXISTS, BUT CAN'T MODIFY");
callback(null, {success: false});
}
});
}
else {
console.log("Account forgot password: error - account with email doesn't eixsts");
callback(null, {success: false});
}
});
}
//SHOULD RENAME TO CHANGE PASSWORD
this.resetPassword = function(user_id, currPassword, newPassword, callback) {
if (user_id) {
db.users.findOne({user_id: user_id}, function(err, doc) {
//check if user exists
if (doc) {
//verify the user if the token matches
if (currPassword == doc.password) {
db.users.findAndModify({query: {user_id: user_id},
update: {$set: {password: newPassword}}, new: true}, function(err, doc) {
if (doc) {
console.log("Account reset password: password reset");
callback(null, {success: true});
}
else {
console.log("WEIRD ASS ERROR - ACCOUNT EXISTS, BUT CAN'T MODIFY");
callback(null, {success: false});
}
});
}
else {
console.log("Account reset password: error - incorrect current password or non-matching new/confirm password");
callback(null, {success: false});
}
}
else {
console.log("Account reset password: error - non-existent account");
callback(null, {success: false});
}
});
}
else {
console.log("Account reset password: error - impossible ID");
callback(null, {success: false});
}
}
this.enroll = function(user_id, class_ids, callback) {
db.users.update({user_id: user_id},
{$set: {class_ids: class_ids}}, function (err, doc) {
if (doc) {
callback(null, {success: true});
}
else {
callback(null, {success: false});
}
});
}
this.getMyClasses = function(user_id, callback) {
db.users.findOne({user_id: user_id}, function (err, doc) {
if (doc) {
//res.send({class_ids: doc.class_ids});
callback(null, {class_ids: doc.class_ids});
}
else {
//res.send({class_ids: []});
callback(null, {class_ids: []});
}
});
}
/* ADD A MAILER.JS MODULE
//sends a verification email to user
function sendVerifyEmail(user, callback) {
var receiver = user.email;
var id = user._id;
var token = user.token;
var emailText = "http://localhost:3000/accountverify/" + id + "/" + token;
var verifyEmailOptions = {
from: "[email protected]",
to: receiver,
subject: "Account verification",
text: emailText,
html: "<a href='" + emailText + "'>" + emailText + "</a>"
};
mailTransporter.sendMail(verifyEmailOptions, callback);
}
function sendResetPassword(user, callback) {
var receiver = user.email;
var id = user._id;
var resetToken = user.resetToken;
var emailText = "http://localhost:3000/resetpassword/" + id + "/" + resetToken;
var resetPasswordEmailOptions = {
from: "[email protected]",
to: receiver,
subject: "Password reset",
text: emailText,
html: "<a href='" + emailText + "'>" + emailText + "</a>"
};
mailTransporter.sendMail(resetPasswordEmailOptions, callback);
}
*/
};
module.exports = accountManager;