-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfirebaseAuth.js
103 lines (86 loc) · 3.97 KB
/
firebaseAuth.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
// Import the functions you need from the SDKs you need
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries
// import { initializeApp, getAuth, getDatabase, getAnalytics, GoogleAuthProvider, signInWithPopup, signOut, onAuthStateChanged, ref } from 'https://www.gstatic.com/firebasejs/10.4.0/firebase-database.js';
// import { initializeApp, getAuth, getDatabase, getAnalytics, GoogleAuthProvider, signInWithPopup, signOut, onAuthStateChanged } from "https://www.gstatic.com/firebasejs/10.4.0/firebase-auth.js";
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.4.0/firebase-app.js";
import { getAnalytics } from "https://www.gstatic.com/firebasejs/10.4.0/firebase-analytics.js";
import { getDatabase, ref, set, push, query, equalTo, orderByChild, onChildAdded, onChildChanged, onChildRemoved, onValue} from "https://www.gstatic.com/firebasejs/10.4.0/firebase-database.js";
import { getAuth, GoogleAuthProvider, signInWithPopup, signOut, onAuthStateChanged } from "https://www.gstatic.com/firebasejs/10.4.0/firebase-auth.js";
let username, emailid, userInDB;
let usernameCallback;
// Function to initialize Firebase using the loaded configuration
const initializeFirebase = (data) => {
const app = initializeApp(data);
const auth = getAuth(app);
const db = getDatabase(app);
const provider = new GoogleAuthProvider();
const analytics = getAnalytics(app);
const userSignIn = async () => {
signInWithPopup(auth, provider)
.then((result) => {
const user = result.user;
}).catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
})
}
const userSignOut = async () => {
signOut(auth).then(() => {
alert("You have signed out.")
}).catch((error) => {})
}
onAuthStateChanged(auth, (user) => {
if(user) {
logoutButton.style.display = "block";
loginButton.style.display = "none";
username = user.displayName;
console.log(username)
console.log(user.email);
emailid = user.email;
loginMessage.textContent = `You have signed in as ${username} with the email address ${emailid}`
loginMessage.style.display = "block";
userInDB = ref(db, user.displayName);
if (usernameCallback) {
usernameCallback(username);
}
} else {
logoutButton.style.display = "none";
loginButton.style.display = "block";
loginMessage.style.display = "none"; }
});
let loginButton = document.createElement("button");
loginButton.textContent = "Login with Google";
loginButton.style.backgroundColor = "white";
loginButton.style.color = "black";
loginButton.style.borderStyle = "solid";
loginButton.addEventListener('click', userSignIn);
//logout
let logoutButton = document.createElement("button");
logoutButton.textContent = "Logout";
logoutButton.style.backgroundColor = "white";
logoutButton.style.color = "black";
logoutButton.style.borderStyle = "solid";
logoutButton.addEventListener('click', userSignOut);
logoutButton.style.display = "none";
//Login message
let loginMessage = document.createElement("div");
loginMessage.style.display = "none";
nameField.appendChild(loginButton);
nameField.appendChild(logoutButton);
console.log(loginMessage.textContent);
nameField.appendChild(loginMessage);
return { app, auth, db, provider, analytics, userSignIn, userSignOut };
};
function printUsernamePath(callback) {
if (typeof callback === 'function') {
if (username) {
callback(username);
} else {
usernameCallback = callback;
}
} else {
console.error('Callback is not a function.');
}
}
export { initializeFirebase, printUsernamePath };