-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
151 lines (125 loc) · 4.26 KB
/
index.js
File metadata and controls
151 lines (125 loc) · 4.26 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
// import { json } from "express";
import { getAnalytics } from "https://www.gstatic.com/firebasejs/10.7.1/firebase-analytics.js";
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.7.1/firebase-app.js";
import {
createUserWithEmailAndPassword,
getAuth,
signInWithEmailAndPassword,
} from "https://www.gstatic.com/firebasejs/10.7.1/firebase-auth.js";
import {
child,
get,
getDatabase,
ref,
set,
update,
} from "https://www.gstatic.com/firebasejs/10.7.1/firebase-database.js";
// import { collection, getDocs } from "https://www.gstatic.com/firebasejs/10.7.1/firebase-firestore.js";
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries
// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
apiKey: "AIzaSyD4zLa7FfO8DVR7S04RBIkU0g884itmO5o",
authDomain: "js-test-dd7db.firebaseapp.com",
databaseURL: "https://js-test-dd7db-default-rtdb.firebaseio.com",
projectId: "js-test-dd7db",
storageBucket: "js-test-dd7db.appspot.com",
messagingSenderId: "889176968753",
appId: "1:889176968753:web:89ed1a71f5017842d80f10",
measurementId: "G-SZY5NXVJL1",
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
const database = getDatabase(app);
const analytics = getAnalytics(app);
//auth
if (window.location.pathname === "/index.html") {
const submitData = document.getElementById("submitData");
submitData.addEventListener("click", () => {
var email = document.getElementById("email").value;
var password = document.getElementById("password").value;
var userName = document.getElementById("name").value;
console.log(email);
if (email && password) {
createUserWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
//signIN
var user = userCredential.user;
//insert in DB
set(ref(database, "users/" + user.uid), {
userName: userName,
email: email,
password: password,
})
.then(() => {
//data saved
alert("Hello: User created successfully");
})
.then(()=>{
console.log('killer' + user.uid, user);
setSession(user.uid, user);
})
.catch((error) => {
//error
alert(error);
alert("Hello: User creation failed");
});
})
.catch((error) => {
var errorCode = error.code;
var errorMessage = error.message;
alert(errorMessage);
});
}
});
}
//login
if (window.location.pathname === "/login-form.html") {
const loginData = document.getElementById("loginData");
loginData.addEventListener("click", () => {
var email = document.getElementById("email").value;
var password = document.getElementById("password").value;
// var userName = document.getElementById('name').value;
console.log(email);
if (email && password) {
signInWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
// Signed in
const user = userCredential.user;
// alert("Hello: User logged in successfully" + user.uid);
// ...
console.log(auth.currentUser.uid);
var lgDate = new Date();
update(ref(database, 'users/' + user.uid), {
lastLogin : lgDate,
});
setSession(user.uid, user);
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
alert(errorMessage)
});
}
});
};
// const dbRef = ref(getDatabase());
function setSession(uid, user) {
get(child(ref(database), "users/" + uid)).then((snapshot) => {
if (snapshot.exists()) {
sessionStorage.setItem(
"userInfo",
JSON.stringify({
name: snapshot.val().userName,
// loginTime: snapshot.val().lastLogin
})
);
sessionStorage.setItem("userCred", JSON.stringify(user));
window.location.href="/home.html";
} else {
console.log("nooo");
}
});
}