-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
140 lines (115 loc) · 3.55 KB
/
main.js
File metadata and controls
140 lines (115 loc) · 3.55 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
/**
* binds all necessary functions on document load
*/
$(document).ready(function () {
bind_form_transition();
bind_login_form();
bind_register_form();
// do not unbind dimissible alert from the html.
// slide up instead
$('.alert').on('click', '.close', function () {
$(this).closest('.alert').slideUp();
});
});
function bind_form_transition() {
$('.message a').click(function () {
$('form').animate({ height: "toggle", opacity: "toggle" }, "slow");
});
}
/**
* Following section takes care of login functionalities
*/
function bind_login_form() {
$("#btn_login").click(function () {
let url = "http://localhost/rest_demo/api/users/authenticate.php";
var email = $('.login-form .email').val();
var password = $('.login-form .password').val();
if (email == '' || password == '') {
showAlerts('#generic-empty');
return;
}
email = $.trim(email);
password = $.trim(password);
let data = {
"email": email,
"password": password
};
postJSON(loginVerifyResponse, url, data);
});
}
function loginVerifyResponse(response) {
let login_response = JSON.parse(response);
console.log(login_response);
if (login_response['response code'] === '200') {
// success
showAlerts('#login-success');
} else if (login_response['response code'] === '404') {
// incorrect password or email
showAlerts('#login-incorrect');
} else {
// server error
showAlerts('#generic-error');
}
}
/**
* Following section takes care of login functionalities
*/
function bind_register_form() {
$('#btn_register').click(function () {
let url = "http://localhost/rest_demo/api/users/register.php";
var email = $('.register-form .email').val();
var password = $('.register-form .password').val();
var confirmPassword = $('.register-form .confirm-password').val();
if (email == '' || password == '' || confirmPassword == '') {
showAlerts('#generic-empty');
return;
} else if (password !== confirmPassword) {
showAlerts('#register-not-match');
return;
}
email = $.trim(email);
password = $.trim(password);
confirmPassword = $.trim(confirmPassword);
let data = {
"email": email,
"password": password
};
postJSON(registerVerifyResponse, url, data);
});
}
function registerVerifyResponse(response) {
let login_response = JSON.parse(response);
console.log(login_response);
if (login_response['response code'] === '201') {
showAlerts('#register-success');
} else if (login_response['response code'] === '403') {
showAlerts('#register-preregistered');
} else {
showAlerts('#generic-error');
}
}
/**
* Following section takes care of CRUS operations
*
* @param {function} callback
* @param {string} url
* @param {JSObject} data
*/
function postJSON(callback, url, data) {
// new HttpRequest instance
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
callback(xmlHttp.responseText);
}
}
var theUrl = url;
xmlHttp.open("POST", theUrl);
xmlHttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xmlHttp.send(
JSON.stringify(data)
);
}
function showAlerts(id) {
$(id).slideDown();
}