-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile.js
More file actions
159 lines (128 loc) · 4.22 KB
/
profile.js
File metadata and controls
159 lines (128 loc) · 4.22 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
/* JS file for profile.html page
*
* This page is for allowing the user to logout and change their user account info.
* The functions include viewing your name and profile picutre and changing them.
*
*/
// HTML Variables
var messageName = document.getElementById('username');
// Database Variables
var db = firebase.database();
var msgRef = db.ref('/msgs');
var userRef = db.ref('/users');
var user = firebase.auth().currentUser;
var storage = firebase.storage();
var imageRef = storage.ref('/images');
var uid, name, email;
// Other Variables
// None
// Redirecting to chatroom page if logged in
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
// User is signed in
uid = user.uid;
email = user.email;
// Getting user profile image
imageRef = storage.ref('/images/' + uid);
imageRef.getDownloadURL().then(function(url) {
// Get donwloaded URL for image
document.querySelector('img').src = url;
}).catch(function(error) {
// User has no profile pic so use default
imageRef = storage.ref('/images/no_user.png');
imageRef.getDownloadURL().then(function(url) {
// Get donwloaded URL for image
document.querySelector('img').src = url;
});
});
} else {
// No user is signed in
imageRef = storage.ref('/images/no_user.png');
imageRef.getDownloadURL().then(function(url) {
// Get donwloaded URL for image
document.querySelector('img').src = url;
});
location.replace('loginpage.html');
}
});
// When enter key is pressed
$(document).keypress(function(event) {
if (event.which == '13') {
event.preventDefault();
}
});
// Updating name value on profile page
userRef.on('child_added', function(data) {
var {id : userID, name : username, email} = data.val();
if (userID == uid) {
messageName.innerHTML = username;
name = username;
}
});
// Logout button function
$('#logoutBtn').click(function() {
firebase.auth().signOut().then(function() {
// Sign out successful
document.querySelector('img').src = "";
location.replace('loginpage.html');
}, function(error) {
// An error happened
alert(error.message);
});
});
// Back to chatroom function
$('#chatroomBtn').click(function() {
location.replace('chatroom.html');
});
// Profile picture function
function previewFile() {
var preview = document.querySelector('img');
var file = document.querySelector('input[type=file]').files[0];
var reader = new FileReader();
reader.onloadend = function () {
preview.src = reader.result;
}
if (file) {
// Read the data as a URL
reader.readAsDataURL(file);
} else {
preview.src = "";
}
newFile = new File([file], uid + '.png', {type: 'image/png'});
imageRef = storage.ref('/images/' + uid);
imageRef.put(newFile).then(function(snapshot) {
// Image succussfully saved in storage
});
}
// Change name button function
$('#nameChangeBtn').click(function() {
var newName = $('#nameChange').val();
var oldName = name;
// Change name value in users
userRef.orderByChild('name').equalTo(name).once('value', function(snapshot) {
// Get correct person data type to change
snapshot.forEach(function(childSnapshot) {
childSnapshot.ref.update({ name: newName });
name = newName;
});
});
// Change neame value in messages
msgRef.orderByChild('name').equalTo(oldName).once('value', function(snapshot) {
// Get correct message data types to change
snapshot.forEach(function(childSnapshot) {
childSnapshot.ref.update({ name: newName });
});
});
// Reloading current page
location.reload('#');
});
// Change password button function
$('#changePassBtn').click(function() {
firebase.auth().sendPasswordResetEmail(email).then(function() {
$('#forgotpasswordmodal').modal('hide');
return alert('Password Reset Sent!');
}).catch(function(error) {
// An error happened
return alert(error.message);
});
});