-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
166 lines (134 loc) · 5.56 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>DBS PPL</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div id="signin" class="container">
<form class="form-signin">
<h2 class="form-signin-heading">Please sign in</h2>
<label for="inputEmail" class="sr-only">Email address</label>
<input type="email" id="signin-inputEmail" class="form-control" placeholder="Email address" required="" autofocus="">
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" id="signin-inputPassword" class="form-control" placeholder="Password" required="">
<button class="btn btn-lg btn-primary btn-block submit" type="submit">Sign in</button>
<button id="createNewBtn" class="btn btn-lg btn-primary">Create New Account</button>
</form>
</div>
<div id="signup" class="container">
<form class="form-signup">
<h2 class="form-signup-heading">Create A New Account</h2>
<label for="inputEmail" class="sr-only">Email address</label>
<input type="email" id="signup-inputEmail" class="form-control" placeholder="Email address" required="" autofocus="">
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" id="signup-inputPassword" class="form-control" placeholder="Password" required="">
<label for="userName">Your Name</label>
<input type="text" id="userName">
<button class="btn btn-lg btn-primary btn-block submit" type="submit">Create Account</button>
</form>
</div>
<div id="addContent" class="container">
<form class="form-addContent">
<h2 class="form-addContent-heading">Add Content</h2>
<label for="number" class="sr-only"></label>
<input type="text" id="number" class="form-control" placeholder="" required="">
<label for="description">Description</label>
<input type="text" id="description">
<button class="btn btn-lg btn-primary btn-block submit" type="submit">Add Content</button>
</form>
</div>
<div class="container">
<h2>Message Content for user</h2>
<div class="content">
</div>
</div>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="https://cdn.firebase.com/js/client/2.2.7/firebase.js"></script>
<script>
var firebase = new Firebase("https://dbs-ppl.firebaseio.com/");
firebase.unauth();
jQuery("#signup").find(".submit").on("click", function(e){
e.preventDefault();
var email = jQuery("#signup-inputEmail").val();
var password = jQuery("#signup-inputPassword").val();
firebase.createUser({
email : email,
password : password
}, function(error, userData) {
if (error) {
console.log("Error creating user:", error);
} else {
jQuery("body").prepend('<p class="bg-success">Account Created.</p>')
jQuery("#signup").fadeOut(1000);
jQuery("#signin").fadeIn(1000);
console.log("Successfully created user account with uid:", userData);
console.dir(userData.uid);
}
});
});
jQuery("#signin").find(".submit").on("click", function(e){
e.preventDefault();
var email = jQuery("#signin-inputEmail").val();
var password = jQuery("#signin-inputPassword").val();
firebase.authWithPassword({
email : email,
password : password
}, function(error, authData) {
if (error) {
console.log("Login Failed!", error);
} else {
console.log("Authenticated successfully with payload:", authData);
console.dir(authData);
}
});
});
jQuery("#addContent").find(".submit").on("click", function(e){
e.preventDefault();
var number = jQuery("#number").val(); ;
var description = jQuery("#description").val();
var authData = firebase.getAuth();
var data = {
"number": number,
"description": description,
"date": Firebase.ServerValue.TIMESTAMP,
"is_approved": false
};
firebase.child("users/" + authData.uid + "/content" ).push( data );
});
// When data is received. Runs only once.
firebase.once("value", function(snap){
console.log( snap.val() );
});
// When on AUTH.
firebase.onAuth(function(authData) {
// Setup new user profile if it doesn't exist.
if ( authData && is_new_user( authData.uid ) ) {
firebase.child("users").child( authData.uid ).set({
provider: authData.provider,
name: getName(authData),
approvedUser: false,
content: { "blank":"blank"}
});
}
});
// find a suitable name based on the meta info given by each provider
function getName(authData) {
switch( authData.provider ) {
case 'password':
return authData.password.email.replace(/@.*/, '');
}
}
// Check to see if this is new user.
function is_new_user( uid ){
var exists = null;
firebase.once("value", function(snap){
exists = snap.child("users/" + uid ).exists();
});
return exists;
}
</script>
</body>
</html>