-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
125 lines (120 loc) · 3.63 KB
/
script.js
File metadata and controls
125 lines (120 loc) · 3.63 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
var config = {
apiKey: "AIzaSyAj1Z6YANlX8Odun_s-_MSN6CwxPakMfSU",
authDomain: "sign-in-sample-fe5af.firebaseapp.com",
databaseURL: "https://sign-in-sample-fe5af.firebaseio.com",
projectId: "sign-in-sample-fe5af",
storageBucket: "sign-in-sample-fe5af.appspot.com",
messagingSenderId: "1006039985068"
};
firebase.initializeApp(config);
function toggleSignIn() {
if (firebase.auth().currentUser) {
// [START signout]
firebase.auth().signOut();
// [END signout]
} else {
var email = document.getElementById("email").value;
var password = document.getElementById("password").value;
if (email.length < 4) {
alert("Please enter an email address.");
return;
}
if (password.length < 4) {
alert("Please enter a password.");
return;
}
// Sign in with email and pass.
// [START authwithemail]
firebase
.auth()
.signInWithEmailAndPassword(email, password)
.catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// [START_EXCLUDE]
if (errorCode === "auth/wrong-password") {
alert("Wrong password.");
} else {
alert(errorMessage);
}
console.log(error);
document.getElementById("quickstart-sign-in").disabled = false;
// [END_EXCLUDE]
});
// [END authwithemail]
}
document.getElementById("quickstart-sign-in").disabled = true;
}
/**
* Handles the sign up button press.
*/
function handleSignUp() {
var email = document.getElementById("email").value;
var password = document.getElementById("password").value;
if (email.length < 4) {
alert("Please enter an email address.");
return;
}
if (password.length < 4) {
alert("Please enter a password.");
return;
}
// Sign in with email and pass.
// [START createwithemail]
firebase
.auth()
.createUserWithEmailAndPassword(email, password)
.catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// [START_EXCLUDE]
if (errorCode == "auth/weak-password") {
alert("The password is too weak.");
} else {
alert(errorMessage);
}
console.log(error);
// [END_EXCLUDE]
});
// [END createwithemail]
}
/**
* initApp handles setting up UI event listeners and registering Firebase auth listeners:
* - firebase.auth().onAuthStateChanged: This listener is called when the user is signed in or
* out, and that is where we update the UI.
*/
function initApp() {
// Listening for auth state changes.
// [START authstatelistener]
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
// User is signed in.
var displayName = user.displayName;
var email = user.email;
var emailVerified = user.emailVerified;
var photoURL = user.photoURL;
var isAnonymous = user.isAnonymous;
var uid = user.uid;
var providerData = user.providerData;
document.getElementById("quickstart-sign-in").textContent = "Sign out";
if (!emailVerified) {
document.getElementById("quickstart-verify-email").disabled = false;
}
} else {
document.getElementById("quickstart-sign-in").textContent = "Sign in";
}
document.getElementById("quickstart-sign-in").disabled = false;
});
// [END authstatelistener]
document
.getElementById("quickstart-sign-in")
.addEventListener("click", toggleSignIn, false);
document
.getElementById("quickstart-sign-up")
.addEventListener("click", handleSignUp, false);
}
window.onload = function() {
initApp();
};