-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.js
209 lines (167 loc) · 5.97 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
// INITIALIZE DATABASE //
var name = window.name || [];
var config = {
apiKey: "AIzaSyC623OOx9z-PSUKTVYgGCvO_BOJuHQlHjw",
authDomain: "testing123-b28a4.firebaseapp.com",
databaseURL: "https://testing123-b28a4.firebaseio.com",
projectId: "testing123-b28a4",
storageBucket: "testing123-b28a4.appspot.com",
messagingSenderId: "521291201548"
};
firebase.initializeApp(config);
// DATABASE VARIABLE//
var database = firebase.database();
// GLOBAL VARIABLES //
var userName = "";
var password = "";
var confirmPassword = "";
var exists = false;
var users = database.ref("/users");
// USER DOESN'T KNOW WHAT MEET SMARTER IS
$("#clicky").on("click", function(event) {
$(".firstChoice").hide();
$(".whatAreWe").removeClass("hidden");
});
$("#showy").on("click", function(event){
$(".whatAreWe").addClass("hidden");
$(".firstChoice").show();
});
// USER PICKS EXISTING USER OR NEW USER.
$(".user").on("click", function(event) {
$(".firstChoice").hide();
// IF EXISTING SHOW existing DIV...
if ($(this).val() === "existing") {
$(".existing").removeClass("hidden");
} else {
// ELSE SHOW new.
$(".new").removeClass("hidden");
}
});
$("#create").on("click", function(event) {
exists = false;
event.preventDefault();
// CAPTURE NEW USER INFO FROM FORM.
userName = $("#userNameInput").val().trim();
password = $("#passwordInput").val().trim();
confirmPassword = $("#passwordConfirm").val().trim();
name = $("#nameInput").val().trim();
console.log(users.key);
// IF THERE ARE USERS...
if (users.key) {
// CREATE NEW SNAPSHOT OF DATABASE.
database.ref("/users").on("child_added", function(snapshot) {
// SNAPSHOT SAVED AS SUBMISSION.
var submission = snapshot.val();
// IF ENTERED USERNAME IS FOUND IN SUBMISSION...
if (submission.userName === userName) {
// exists SET TO TRUE.
exists = true;
}
}, function(errorObject) {
console.log("Errors handled: " + errorObject.code);
});
addNew();
} else {
// PUSH NEWLY CREATED USER DATA TO DATABASE.
database.ref("/users").push({
userName: userName,
password: password,
name: name
});
console.log("here");
}
});
$("#backForCreate").on("click", function(event) {
if (event.keyCode === 13){
event.preventDefault;
}
$(".new").addClass("hidden");
});
$("#backForExisting").on("click", function(event) {
event.preventDefault;
$(".existing").addClass("hidden");
});
// SNAPSHOT OF CURRENT DATABASE CREATED.
database.ref("/users").on("value", function(snapshot) {})
// CAPTURE USERNAME AND PASSWORD FOR LOG IN.
$("#login").on("click", function(event) {
exists = false;
event.preventDefault();
// CAPTURE EXISTING USER INFO FROM FORM.
userName = $("#userName").val().trim();
password = $("#password").val().trim();
// CREATE SNAPSHOT OF DATABASE WHEN CHILD IS ADDED.
database.ref("/users").on("child_added", function(snapshot) {
// SNAPSHOT SAVED AS submission.
var submission = snapshot.val();
// AUTHENTICATE
// IF USERNAME AND PASSWORD ENETERED IN FORM MATCH INFO IN DATABASE...
if ((submission.userName === userName) && (submission.password === password)) {
// exists IS TRUE.
exists = true;
// PULL NAME FROM submission.
name = submission.name;
}
}, function(errorObject) {
console.log("Errors handled: " + errorObject.code);
});
exist();
});
// DUPLICATE CHECK
// EXISTING USER AUTHENTICATION
function exist() {
// IF exists IS TRUE...
if (exists) {
// NOTIFY USER THEY HAVE LOGGED IN AND ARE BEING REDIRECTED.
$("#notificationShow").text("Hello " + name + "! Access Granted. Redirecting you now.").addClass("accessGranted").removeClass("accessDenied");
// REDIRECT DELAYED 5 SECONDS.
setTimeout(function() {
window.location.replace("index.html");
}, 3000);
} else {
// ELSE (exists being false) NOTIFY USER ACCESS IS DENIED.
$("#notificationShow").text("Access Denied.").addClass("accessDenied").removeClass("accessGranted");
// REMOVE ACCESS DENIED MESSAGE AFTER 2.5 SECONDS.
setTimeout(function() {
$("#notificationShow").text("");
}, 2500);
}
}
// ADD NEW USER
function addNew() {
// IF exists IS TRUE...
if (exists) {
// NOTIFY USER THEY ALREADY HAVE A LOGIN.
$("#notificationShow").append("This user already exists.").addClass("accessDenied").removeClass("accessGranted");
// REMOVE MESSAGE AFTER 2.5 SECONDS.
setTimeout(function() {
$("#notificationShow").text("");
}, 2500);
} else {
// ELSE (exists being false)
// CHECK passwordInput MATCHES passwordConfirm.
if (password !== confirmPassword) {
console.log(password);
console.log(confirmPassword);
// NOTIFY USER PASSWORDS MUST MATCH.
$("#notificationShow").append("Passwords must match.").addClass("accessDenied").removeClass("accessGranted");
// REMOVE MESSAGE AFTER 2.5 SECONDS.
setTimeout(function() {
$("#notificationShow").text("");
}, 2500);
} else {
// PUSH NEW USER FORM INFO TO DATABASE.
database.ref("/users").push({
userName: userName,
password: password,
name: name
});
// NOTIFY USER THEY CAN NOW LOGIN BY SELECTING "Existing User".
$("#notificationShow").append("New user created. When page reloads select \"Existing User\".").addClass("accessGranted").removeClass("accessDenied");
// RELOAD PAGE AFTER 2.5 SECONDS.
setTimeout(function() {
window.location.reload();
}, 2500);
}
}
}