diff --git a/signupjs.js b/signupjs.js
index ca34b17c..62631d69 100644
--- a/signupjs.js
+++ b/signupjs.js
@@ -1,5 +1,19 @@
-import { initializeApp } from "https://www.gstatic.com/firebasejs/10.11.1/firebase-app.js";
-import { getAuth, GoogleAuthProvider, signInWithPopup } from "https://www.gstatic.com/firebasejs/10.11.1/firebase-auth.js";
+import {initializeApp} from "https://www.gstatic.com/firebasejs/10.11.1/firebase-app.js";
+import {
+ getAuth,
+ GoogleAuthProvider,
+ signInWithPopup,
+ signInWithEmailAndPassword
+} from "https://www.gstatic.com/firebasejs/10.11.1/firebase-auth.js";
+import {
+ getDatabase,
+ ref,
+ orderByChild,
+ query,
+ get,
+ equalTo,
+} from "https://www.gstatic.com/firebasejs/10.11.1/firebase-database.js";
+
const firebaseConfig = {
apiKey: "AIzaSyDx_FcoL3XJryt6BInhOaDsMKiSmxzrYBI",
@@ -11,38 +25,75 @@ const firebaseConfig = {
};
const app = initializeApp(firebaseConfig);
+const database = getDatabase();
+const dbref = ref(database, "data");
const auth = getAuth(app);
+
auth.languageCode = 'en';
const provider = new GoogleAuthProvider();
-
-document.getElementById("google").addEventListener("click", function() {
+//signin
+document.getElementById("google").addEventListener("click", function () {
signInWithPopup(auth, provider)
.then((result) => {
const credential = GoogleAuthProvider.credentialFromResult(result);
const user = result.user;
console.log(user);
+ sessionStorage.setItem("username", user.displayName);
window.location.href = "./signedup.html";
- }).catch((error) => {
+ })
+ .catch((error) => {
console.error("Error during Google sign-in:", error);
});
});
-document.getElementById("signup-form").addEventListener("submit", function(event) {
+document.getElementById("loginForm").addEventListener("submit", function(event) {
event.preventDefault(); // Prevent form from submitting the default way
- const name = document.getElementById("name").value;
- const email = document.getElementById("email").value;
- const password = document.getElementById("password").value;
+ const email = document.getElementById("loginEmail").value;
+ const password = document.getElementById("loginPassword").value;
- if (validateForm(name, email, password)) {
- // Simulate a successful registration (Replace with actual Firebase sign-up logic)
- console.log("Form submitted with:", { name, email, password });
- window.location.href = "./signedup.html";
+ if (setTimeout(validateForm(email, password), 500)){
+ signInWithEmailAndPassword(auth,email,password)
+ .then((userCredential)=>{
+ console.log(userCredential);
+ sessionStorage.setItem("username", userCredential.user.displayName); // set username into session storage
+ window.location.href = "./index.html";
+ })
+ .catch((error)=>{
+ document.getElementById("login-failed").removeAttribute("style");
+ console.log(error);
+ })
}
-});
+ // const email = document.getElementById("loginEmail").value;
+ // const password = document.getElementById("loginPassword").value;
+ // if (setTimeout(validateForm(email, password), 500)) {
+ // const usersRef = ref(database, "users/");
+ // const squery = query(usersRef, orderByChild("email"), equalTo(email));
+ // let name = null;
+ // get(squery).then((res) => {
+ // console.log(res);
+ // if (res.exists()) {
+ // const user = res.val();
+ // console.log(user);
+ // for (const userId in user) {
+ // const userd = user[userId];
+ // name = userd.username;
+ // }
+ //
+ //
+ // // localStorage.setItem('username',)
+ // } else {
+ // console.log("no results found");
+ // document.getElementById("login-failed").removeAttribute("style");
+ // }
+ })
+ // Simulate a successful registration (Replace with actual Firebase sign-up logic)
+ // console.log("Form submitted with:", {email, password});
+//}
+// });
-function validateForm(name, email, password) {
- if (name === "" || email === "" || password === "") {
+function validateForm( email, password) {
+ if ( email === "" || password === "") {
alert("All fields are required!");
return false;
}
@@ -62,4 +113,4 @@ function validateEmail(email) {
return re.test(email);
}
-/*646664380070-n07i34glgcc6sanb59p3m1k5k0cgn1v1.apps.googleusercontent.com*/
\ No newline at end of file
+/*646664380070-n07i34glgcc6sanb59p3m1k5k0cgn1v1.apps.googleusercontent.com*/
diff --git a/signupjs1.js b/signupjs1.js
index 6768d81a..d2c6408f 100644
--- a/signupjs1.js
+++ b/signupjs1.js
@@ -1,5 +1,6 @@
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.11.1/firebase-app.js";
-import { getAuth, GoogleAuthProvider, signInWithPopup } from "https://www.gstatic.com/firebasejs/10.11.1/firebase-auth.js";
+import { getAuth, GoogleAuthProvider, signInWithPopup ,createUserWithEmailAndPassword,updateProfile} from "https://www.gstatic.com/firebasejs/10.11.1/firebase-auth.js";
+import { getDatabase, ref, set } from "https://www.gstatic.com/firebasejs/10.11.1/firebase-database.js"
const firebaseConfig = {
apiKey: "AIzaSyDx_FcoL3XJryt6BInhOaDsMKiSmxzrYBI",
@@ -12,6 +13,8 @@ const firebaseConfig = {
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
+const database=getDatabase();
+const dbRef=ref(database,'data');
auth.languageCode = 'en';
const provider = new GoogleAuthProvider();
@@ -28,16 +31,36 @@ document.getElementById("google1").addEventListener("click", function() {
});
});
-document.getElementById("signup-form").addEventListener("submit", function(event) {
+document.getElementById("registerForm").addEventListener("submit", function(event) {
event.preventDefault(); // Prevent form from submitting the default way
const name = document.getElementById("name").value;
const email = document.getElementById("email").value;
const password = document.getElementById("password").value;
if (validateForm(name, email, password)) {
+ try {
+ createUserWithEmailAndPassword(auth,email,password)
+ .then((credential)=>
+ {
+ set(ref(database, 'users/' + credential.user.uid), { // set userdetails in realtime database
+ username: name,
+ email: email,
+ })
+ updateProfile(credential.user,{ // update profile for username
+ displayName:name,
+ })
+ window.location.href = "index.html";
+ });
+
+ } catch (error) {
+ console.log(error);
+ //alert("error occured!")
+ }
+
// Simulate a successful registration (Replace with actual Firebase sign-up logic)
+
console.log("Form submitted with:", { name, email, password });
- window.location.href = "./signed.html";
+ //
}
});