-
Notifications
You must be signed in to change notification settings - Fork 1
/
login.js
257 lines (236 loc) · 7.05 KB
/
login.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
function invokeLogin() {
$.ajax({
type : "POST",
url : 'http://localhost:8080/MailApplication/rest/login/',
data : JSON.stringify({
"email" : $("#login_email").val(),
"password" : $("#login_password").val()
}),
contentType : "application/json; charset=utf-8",
dataType : "json",
success : function(data) {
if (data.status.responseStatus === "FAILURE") {
$("#login_result").text(
"Sorry! Your login failed due to "
+ data.status.errMessage + ".");
} else {
openHome();
localStorage.setItem("accessToken", data.obj);
}
},
error : function(msg, url, linenumber) {
alert('Error message: ' + msg + '\nURL: ' + url + '\nLine Number: '
+ linenumber);
}
});
}
function openRegister() {
$("#login").hide();
$("#register").show();
}
function closeRegister() {
$("#register").hide();
$("#login").show();
}
function invokeRegister() {
$
.ajax({
type : "POST",
url : 'http://localhost:8080/MailApplication/rest/register',
data : JSON.stringify({
"name" : $("#register_name").val(),
"email" : $("#register_email").val(),
"password" : $("#register_password").val(),
"confirmPassword" : $("#register_confirmPassword").val()
}),
contentType : "application/json; charset=utf-8",
dataType : "json",
success : function(data) {
if (data.status.responseStatus === "FAILURE") {
$("#register_result").text(
"Sorry! Your registration failed due to "
+ data.status.errMessage + ".");
} else {
$("#register_success")
.text(
"Thank you for registration. Please confirm your registration by clicking on the verification link emailed to you.");
}
},
error : function(msg, url, line) {
alert('error trapped in error: function(msg, url, line)');
alert('msg = ' + msg + ', url = ' + url + ', line = '
+ line);
}
});
}
function getMessage() {
var x = location.search;
if (x != "") {
var message = invokeVerification(x);
return message;
} else {
return null;
}
}
function invokeVerification(x) {
$.ajax({
type : "POST",
url : 'http://localhost:8080/MailApplication/rest/verification',
data : JSON.stringify({
"link" : x
}),
contentType : "application/json; charset=utf-8",
dataType : "json",
success : function(data) {
$("#message").text(
"Your account has been confirmed! Login to Continue!");
},
error : function(msg, url, line) {
alert('error trapped in error: function(msg, url, line)');
alert('msg = ' + msg + ', url = ' + url + ', line = ' + line);
}
});
return message;
}
function openHome() {
$("#login").hide();
$("#home").show();
$("#login_result").text(" ");
}
function logout() {
if (localStorage.getItem("accessToken") != null) {
invokeLogout(localStorage.getItem("accessToken"));
localStorage.removeItem("accessToken");
}
$("#home").hide();
$("#login").show();
location.reload();
}
function toggleSettings() {
$("#settings").toggle();
if ($('#settings').is(':visible')) {
$("#messages").hide();
} else {
$("#messages").show();
}
}
function invokeLogout(accessToken) {
$.ajax({
type : "POST",
url : 'http://localhost:8080/MailApplication/rest/logout',
data : JSON.stringify({
"accessToken" : accessToken
}),
contentType : "application/json; charset=utf-8",
dataType : "json",
success : function(data) {
return;
},
error : function(msg, url, line) {
alert('error trapped in error: function(msg, url, line)');
alert('msg = ' + msg + ', url = ' + url + ', line = ' + line);
}
});
}
function invokeAddAccount() {
$.ajax({
type : "POST",
url : 'http://localhost:8080/MailApplication/rest/addAccount',
data : JSON.stringify({
"accessToken" : localStorage.getItem("accessToken"),
"email" : $("#settings_email").val(),
"password" : $("#settings_password").val(),
"accountAccessType" : $("#account_access_type").val(),
"incomingEmailServer" : $("#incoming_email_server").val(),
"outgoingEmailServer" : $("#outgoing_email_server").val(),
"incomingEmailPort" : $("#incoming_email_port").val(),
"outoingEmailPort" : $("#outgoing_email_port").val(),
"requireSsl" : $("#requires_ssl").val()
}),
contentType : "application/json; charset=utf-8",
dataType : "json",
success : function(data) {
if (data.status.responseStatus === "FAILURE") {
if (data.status.errMessage === "Session Timed Out") {
logout(localStorage.getItem("accessToken"));
$("#message").text("Sorry! Your Session Has Expired.");
} else if (data.status.errMessage === "User Logged Out") {
logout();
$("#message").text("You have already been logged out.");
} else {
$("#settings_result").text(
"Sorry! Your account could not be added due to "
+ data.status.errMessage + ".");
}
} else {
$('#settings-form')[0].reset(); // reset all the form fields
$("#settings_success").text("Account added successfully");
// invokeFetchEmail();
}
},
error : function(msg, url, line) {
alert('error trapped in error: function(msg, url, line)');
alert('msg = ' + msg + ', url = ' + url + ', line = ' + line);
}
});
}
var index = 0;
function invokeMailCheck() {
var accessToken = localStorage.getItem("accessToken");
$
.ajax({
type : "POST",
url : 'http://localhost:8080/MailApplication/rest/mailCheck',
data : JSON.stringify({
"accessToken" : accessToken
}),
contentType : "application/json; charset=utf-8",
dataType : "json",
success : function(data) {
if (data.status.responseStatus === "FAILURE") {
if (data.status.errMessage === "Session Timed Out") {
logout(localStorage.getItem("accessToken"));
$("#message").text(
"Sorry! Your Session Has Expired.");
} else if (data.status.errMessage === "User Logged Out") {
logout();
$("#message").text(
"You have already been logged out.");
} else {
$("#error").text(
"Emails could not be fetched due to "
+ data.status.errMessage + ".");
}
} else {
$("#error").hide();
var r = new Array(), j = -1;
r[++j] = '<tr><th>Message Id </th> <th>From</th><th>To</th><th>Subject</th><th>Sent Date</th></tr>';
for (var key = 0; key < data.obj.length; key++) {
r[++j] = '<tr><td>';
r[++j] = data.obj[key].messageid;
r[++j] = '</td><td>';
r[++j] = data.obj[key].from;
r[++j] = '</td><td>';
r[++j] = data.obj[key].to;
r[++j] = '</td><td>';
r[++j] = data.obj[key].subject;
r[++j] = '</td><td>';
r[++j] = data.obj[key].sentDate;
r[++j] = '</td></tr>';
r[++j] = '<tr><td colspan="4"><iframe id="message_body"><html><head></head><body>';
r[++j] = data.obj[key].body;
r[++j] = '</body></html></iframe></td></tr>';
}
$("#emails_table").html(r.join(' '));
}
},
error : function(msg, url, line) {
alert('error trapped in error: function(msg, url, line)');
alert('msg = ' + msg + ', url = ' + url + ', line = '
+ line);
}
});
}
function openMessage() {
$(".message-body").toggle();
}