-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathprofile.js
More file actions
213 lines (192 loc) · 6.27 KB
/
profile.js
File metadata and controls
213 lines (192 loc) · 6.27 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
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
const authCookie = document.cookie.split('; ').find(row => row.startsWith('auth='));
const userIdCookie = document.cookie.split('; ').find(row => row.startsWith('userId='));
if (authCookie && userIdCookie) {
const authValue = authCookie.split('=')[1];
// Send the auth cookie value to the server for validation and data retrieval
fetch('/api/verify-auth-cookie', {
method: 'GET',
headers: {
'Authorization': `Bearer ${authValue}`
}
})
.then(response => response.json())
.then(data => {
if (data.success !== false) {
// Update the UI
document.getElementById('username').textContent = data.displayName;
document.getElementById('provider').textContent = data.provider;
} else {
console.error(data.message);
window.location.href = "/";
}
})
.catch(error => {
console.error('Error:', error);
window.location.href = "/";
});
} else {
window.location.href = "/";
}
$(document).ready(function () {
$('.toast').toast();
});
$(document).ready(() => {
// Fetch current user's profile data
fetch('/get-profile')
.then(response => response.json())
.then(data => {
console.log('et-profile: ' + data);
// Fill the form fields with user data
$('#editUsername').val(data.username);
$('#editEmail').val(data.email);
$('#editPhoneNumber').val(data.phone);
// You typically wouldn't prefill the password for security reasons
// If the user wishes to update it, they can type a new one.
})
.catch(error => {
showToast('Error fetching profile:', error);
});
// Listen to form submission
$('#edit-user-form').submit(function (e) {
e.preventDefault();
const updatedData = {
username: $('#editUsername').val(),
email: $('#editEmail').val(),
phoneNumber: $('#editPhoneNumber').val(),
password: $('#editPassword').val(),
};
fetch('/update-profile', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(updatedData),
})
.then(response => response.text())
.then(message => {
showToast(message); // Inform user about the update result
})
.catch(error => {
showToast('Error updating profile:' + error);
});
});
});
function showToast(message) {
$("#toast-body").text(message);
$(".toast").toast({ delay: 5000 });
$(".toast").toast('show');
}
function revokeGoogleAccess() {
// Placeholder for revoking Google access, adjust as needed
fetch('/revoke-google').then(response => {
document.getElementById('revoked-alert').style.display = 'block';
});
}
function revokeFacebookAccess() {
// Placeholder for revoking Facebook access, adjust as needed
fetch('/revoke-facebook').then(response => {
document.getElementById('revoked-alert').style.display = 'block';
});
}
function resetDatabase() {
fetch('/reset-db', { method: 'GET' })
.then(response => response.text())
.then(data => {
// handle the response, maybe show a notification to the user
console.log(data);
});
}
$(document).ready(function () {
// Initially check if the user has 2FA enabled
check2FAStatus();
$("#setup-2fa-btn").click(function() {
initiate2FA();
});
$("#verify-2fa-btn").click(function() {
const token = $("#token").val();
if (!token) {
showToast("Please enter a token!");
return;
}
verify2FAToken(token);
});
$("#disable-2fa-btn").click(function() {
disable2FA();
});
});
function check2FAStatus() {
$.ajax({
url: "/2fa/status",
method: "GET",
success: function(data) {
if (data.enabled) {
$("#2fa-status .badge").text("Enabled").removeClass("badge-secondary").addClass("badge-success");
$("#setup-2fa-btn").hide();
$("#2fa-enabled").show();
} else {
$("#2fa-status .badge").text("Disabled");
}
},
error: function(error) {
showToast("Error fetching 2FA status.");
}
});
}
function initiate2FA() {
$.ajax({
url: "/2fa/initiate",
method: "GET",
success: function(data) {
if (data.qrcode) {
$("#qrcode").attr("src", data.qrcode);
$("#2fa-setup").show();
} else {
showToast("Error generating QR code.");
}
},
error: function(error) {
showToast("Error initiating 2FA.");
}
});
}
function verify2FAToken(token) {
console.log('verify token: ' + token);
$.ajax({
url: "/2fa/verify",
method: "POST",
contentType: "application/json",
data: JSON.stringify({ token: token }),
success: function(data) {
if (data.verified) {
showToast("2FA enabled successfully!");
$("#2fa-status .badge").text("Enabled").removeClass("badge-secondary").addClass("badge-success");
$("#2fa-setup").hide();
$("#2fa-enabled").show();
} else {
showToast("Invalid token. Please try again.");
}
},
error: function(error) {
showToast("Error verifying token.");
}
});
}
function disable2FA() {
$.ajax({
url: "/2fa/disable",
method: "POST",
success: function(data) {
if (data.disabled) {
showToast("2FA disabled successfully!");
$("#2fa-status .badge").text("Disabled").addClass("badge-secondary").removeClass("badge-success");
$("#2fa-enabled").hide();
$("#setup-2fa-btn").show();
} else {
showToast("Error disabling 2FA.");
}
},
error: function(error) {
showToast("Error while trying to disable 2FA.");
}
});
}