-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.js
85 lines (77 loc) · 2.92 KB
/
auth.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
// Import the functions you need from the SDKs you need
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.10.0/firebase-app.js";
import { getAnalytics } from "https://www.gstatic.com/firebasejs/9.10.0/firebase-analytics.js";
import { getAuth, createUserWithEmailAndPassword, signInWithEmailAndPassword, signOut } from "https://www.gstatic.com/firebasejs/9.10.0/firebase-auth.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: "AIzaSyBTYAGYuoJv0KGiPRu23c-P3pzJZn6rkCA",
authDomain: "fir-c0309.firebaseapp.com",
databaseURL: "https://fir-c0309-default-rtdb.firebaseio.com",
projectId: "fir-c0309",
storageBucket: "fir-c0309.appspot.com",
messagingSenderId: "1093776995940",
appId: "1:1093776995940:web:51f1d91839c4f8eaeb1171"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);
const auth = getAuth();
console.log(app);
//----- New Registration code start
document.getElementById("register").addEventListener("click", function() {
var email = document.getElementById("email").value;
var password = document.getElementById("password").value;
//For new registration
createUserWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
// Signed in
const user = userCredential.user;
console.log(user);
alert("Registration successfully!!");
// ...
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
// ..
console.log(errorMessage);
alert(error);
});
});
//----- End
//----- Login code start
document.getElementById("login").addEventListener("click", function() {
var email = document.getElementById("login_email").value;
var password = document.getElementById("login_password").value;
signInWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
// Signed in
const user = userCredential.user;
console.log(user);
alert(user.email+" Login successfully!!!");
document.getElementById('logout').style.display = 'block';
// ...
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
console.log(errorMessage);
alert(errorMessage);
});
});
//----- End
//----- Logout code start
document.getElementById("logout").addEventListener("click", function() {
signOut(auth).then(() => {
// Sign-out successful.
console.log('Sign-out successful.');
alert('Sign-out successful.');
document.getElementById('logout').style.display = 'none';
}).catch((error) => {
// An error happened.
console.log('An error happened.');
});
});