Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

working login-Signup using firebase #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions Working_login/auth.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="/Working_login/style.css">
</head>
<body>
<div class="form-holder">
<div class="container" id="container">
<div class="form-container sign-up-container">
<!-- user register form start -->
<form id="registerForm">
<h1>Create Account</h1>
<span> use your email for registration</span>
<input type="text" placeholder="First Name" id="fnameInp"/>
<input type="text" placeholder="Last Name" id="lnameInp"/>
<input type="email" placeholder="Email" id="emailInp" />
<input type="password" placeholder="Password" id="passwordInp" />
<button type="submit" id="register-btn">Sign Up</button>
</form>
<!-- user register form End -->
</div>
<div class="form-container sign-in-container">
<!-- user login form Start-->
<form id="loginForm">
<h1>Sign in</h1>
<span>use your account</span>
<input type="email" placeholder="Email" id="login-email" />
<input type="password" placeholder="Password" id="login-password" />
<a href="#">Forgot your password?</a>
<button id="login-btn" type="submit">Sign In</button>
</form>
<!-- user login form End -->
</div>
<div class="overlay-container">
<div class="overlay">
<div class="overlay-panel overlay-left">
<h1>Welcome Back!</h1>
<p>To keep connected with us please login with your personal info</p>
<button class="ghost" id="signIn">Sign In</button>
</div>
<div class="overlay-panel overlay-right">
<h1>Hello, Friend!</h1>
<p>Enter your personal details and start journey with us</p>
<button class="ghost" id="signUp">Sign Up</button>
</div>
</div>
</div>
</div>
</div>
<script src="/Working_login/userAuth.js"></script>
<script type="module" src="/Working_login/login.js"></script>
<script type="module" src="/Working_login/signUp.js"></script>
</body>
</html>
59 changes: 59 additions & 0 deletions Working_login/login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.11.0/firebase-app.js";
import { getAnalytics } from "https://www.gstatic.com/firebasejs/10.11.0/firebase-analytics.js";
import { getDatabase, get, ref , child } from "https://www.gstatic.com/firebasejs/10.11.0/firebase-database.js";
import { getAuth, signInWithEmailAndPassword } from "https://www.gstatic.com/firebasejs/10.11.0/firebase-auth.js";

const firebaseConfig = {
apiKey: "-----API KEY-------",
authDomain: "",
databaseURL: "",
projectId: "",
storageBucket: "",
messagingSenderId: "79471128063",
appId: "",
measurementId: ""
};

const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);
const db = getDatabase();
const auth = getAuth(app);
const dbref = ref(db);

let Email = document.getElementById('login-email');
let Password = document.getElementById('login-password');
let LoginForm = document.getElementById('loginForm');

let SignInUser = evt => {
evt.preventDefault();

// Log the value of Email to check if it's defined and capturing the correct value
console.log("Email value:", Email.value);

signInWithEmailAndPassword(auth, Email.value, Password.value)
.then((Credential) =>{
get(child(dbref, 'UserAuthList/' + Credential.user.uid)).then((snapshot) => {
if(snapshot.exists){
sessionStorage.setItem("user-info", JSON.stringify({
firstname : snapshot.val().firstname,
lastname : snapshot.val().lastname,
email : snapshot.val().email // Changed 'Email' to lowercase 'email'
}));
sessionStorage.setItem("user-creds", JSON.stringify(Credential.user));
//window.location.href = '/html/index.html'; add your path after login
} else {
console.log("User data not found in the database.");
}
}).catch((error) => {
console.log("Error retrieving user data:", error);
});
})
.catch((error)=>{
alert(error.message);
console.log(error.code);
console.log(error.message);
});
}

LoginForm.addEventListener('submit' , SignInUser );

61 changes: 61 additions & 0 deletions Working_login/signUp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.11.0/firebase-app.js";
import { getAnalytics } from "https://www.gstatic.com/firebasejs/10.11.0/firebase-analytics.js";
import { getDatabase, set, ref } from "https://www.gstatic.com/firebasejs/10.11.0/firebase-database.js";
import { getAuth, createUserWithEmailAndPassword } from "https://www.gstatic.com/firebasejs/10.11.0/firebase-auth.js";

// https://firebase.google.com/docs/web/setup#available-libraries

const firebaseConfig = {
apiKey: "",
authDomain: "",
databaseURL: "",
projectId: "",
storageBucket: "",
messagingSenderId: "",
appId: "",
measurementId: ""
};

const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);
const db = getDatabase();
const auth = getAuth(app);
const signUpButton = document.getElementById('signUp');
const signInButton = document.getElementById('signIn');
const container = document.getElementById('container');


let EmailInp = document.getElementById('emailInp');
let PassInp = document.getElementById('passwordInp');
let FnameInp = document.getElementById('fnameInp');
let LnameInp = document.getElementById('lnameInp');
let RegisterForm = document.getElementById('registerForm');

let RegisterUser = evt => {
evt.preventDefault();

createUserWithEmailAndPassword(auth, EmailInp.value, PassInp.value)
.then((Credential) =>{
set(ref(db, 'UserAuthList/' + Credential.user.uid), {
firstname: FnameInp.value,
lastname: LnameInp.value,
email: EmailInp.value // Corrected to lowercase 'email'
});

sessionStorage.setItem("user-info", JSON.stringify({
firstname: FnameInp.value,
lastname: LnameInp.value,
email: EmailInp.value
}));

sessionStorage.setItem("user-creds", JSON.stringify(Credential.user));
//window.location.href = '/html/index.html'; add your path after user signup
})
.catch((error)=>{
alert(error.message);
console.log(error.code);
console.log(error.message);
})
}

RegisterForm.addEventListener('submit' , RegisterUser);
Loading