-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirebase.js
65 lines (59 loc) · 2.37 KB
/
firebase.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
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.18.0/firebase-app.js";
import { getDatabase, ref, set, child, get } from "https://www.gstatic.com/firebasejs/9.18.0/firebase-database.js";
const firebaseConfig = {
apiKey: "AIzaSyCuHrtSAPLC6CQeuv9WkaWcjksWFWly6TY",
authDomain: "meetup-5ffb3.firebaseapp.com",
databaseURL: "https://meetup-5ffb3-default-rtdb.firebaseio.com",
projectId: "meetup-5ffb3",
storageBucket: "meetup-5ffb3.appspot.com",
messagingSenderId: "180793006827",
appId: "1:180793006827:web:7665f36e7487bcf42e3cae"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const db = getDatabase();
//...........................................The Refrences.......................//
const username = document.getElementById('userInp');
const pass = document.getElementById('passInp');
const submit = document.getElementById('sub_btn');
//..................................Authentication...............................//
function AuthenticateUser() {
const dbref = ref(db);
get(child(dbref, "userList/" + username.value)).then((snapshot) => {
if (snapshot.exists()) {
let dbpass = decPass(snapshot.val().password);
if (dbpass == pass.value) {
login(snapshot.val());
}
else {
error1.innerHTML="Enter Correct Password !";
error2.innerHTML="";
}
}
else {
error1.innerHTML="";
error2.innerHTML="Username does not exist!";
}
});
}
//.................................Decryption.......................//
function decPass(dbpass) {
var pass12 = CryptoJS.AES.decrypt(dbpass, pass.value);
var password= pass12.toString(CryptoJS.enc.Utf8);
console.log(password);
return password;
}
//..................................Login..........................//
function login(user) {
let keepLoggedIn = document.getElementById('customSwitch1').checked;
if (!keepLoggedIn) {
sessionStorage.setItem('user', JSON.stringify(user));
window.location = "home.html";
}
else {
localStorage.setItem('keepLoggedIn', 'yes');
localStorage.setItem('user', JSON.stringify(user));
window.location = "home.html";
}
}
submit.addEventListener('click', AuthenticateUser);