-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
73 lines (57 loc) · 2.29 KB
/
auth.js
File metadata and controls
73 lines (57 loc) · 2.29 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
import { initializeApp } from "https://www.gstatic.com/firebasejs/11.1.0/firebase-app.js";
import { getAuth, createUserWithEmailAndPassword, signInWithEmailAndPassword } from "https://www.gstatic.com/firebasejs/11.1.0/firebase-auth.js";
const firebaseConfig = {
apiKey: "AIzaSyD4hql8ApZFCZXTCqAswRXNY9AQ7TEZMCs",
authDomain: "news-app-6fb3f.firebaseapp.com",
projectId: "news-app-6fb3f",
storageBucket: "news-app-6fb3f.firebasestorage.app",
messagingSenderId: "1013648179384",
appId: "1:1013648179384:web:7a00150cac5e10f940b44d",
measurementId: "G-JJH9XLVB3Y"
};
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
let create_acc = document.querySelector("#create_acc");
let have_acc = document.querySelector("#have_acc");
let sign_up = document.querySelector("#signup");
let log_in = document.querySelector("#login");
create_acc.addEventListener("click", (e) => {
e.preventDefault();
sign_up.style.display = "grid";
log_in.style.display = "none";
});
have_acc.addEventListener("click", () => {
sign_up.style.display = "none";
log_in.style.display = "grid";
});
const signUp = (e) => {
e.preventDefault();
const email = document.querySelector("#email_signup").value;
const password = document.querySelector("#password_signup").value;
createUserWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
console.log("User Registered:", userCredential.user);
alert("Sign-up successful!");
})
.catch((error) => {
console.error("Error:", error.message);
alert(error.message);
});
};
const login = (e) => {
e.preventDefault();
const email = document.querySelector("#email_login").value;
const password = document.querySelector("#password_login").value;
signInWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
console.log("User Logged In:", userCredential.user);
alert("Login successful!");
window.location.href = "dashboard.html";
})
.catch((error) => {
console.error("Error:", error.message);
alert(error.message);
});
};
document.querySelector("#signup button").addEventListener("click", signUp);
document.querySelector("#login button").addEventListener("click", login);