34334-vm/assets/js/auth.js
Flatlogic Bot fa86852137 02
2025-09-24 06:32:27 +00:00

106 lines
3.3 KiB
JavaScript

import { initializeApp } from "https://www.gstatic.com/firebasejs/9.6.1/firebase-app.js";
import {
getAuth,
createUserWithEmailAndPassword,
signInWithEmailAndPassword,
GoogleAuthProvider,
signInWithPopup,
onAuthStateChanged
} from "https://www.gstatic.com/firebasejs/9.6.1/firebase-auth.js";
// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: "AIzaSyAuabZynwAn8r91_toMzdN2_vCVOwLsAf8",
authDomain: "ai-resume-builder-90135.firebaseapp.com",
projectId: "ai-resume-builder-90135",
storageBucket: "ai-resume-builder-90135.firebasestorage.app",
messagingSenderId: "465451014283",
appId: "1:465451014283:web:355ddebe12bc8a2dfedc8e",
measurementId: "G-4B91V0ZY58"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
const provider = new GoogleAuthProvider();
// DOM Elements
const loginForm = document.getElementById('loginForm');
const signupForm = document.getElementById('signupForm');
const googleLoginBtn = document.getElementById('googleLoginBtn');
const googleSignupBtn = document.getElementById('googleSignupBtn');
const errorContainer = document.getElementById('errorContainer');
// Function to display errors
const showError = (message) => {
errorContainer.textContent = message;
errorContainer.style.display = 'block';
};
// Redirect if user is already logged in
onAuthStateChanged(auth, (user) => {
if (user) {
window.location.href = 'home.php';
}
});
// Email/Password Sign Up
if (signupForm) {
signupForm.addEventListener('submit', (e) => {
e.preventDefault();
const email = signupForm.querySelector('#signupEmail').value;
const password = signupForm.querySelector('#signupPassword').value;
createUserWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
// Signed in
window.location.href = 'home.php';
})
.catch((error) => {
showError(error.message);
});
});
}
// Email/Password Login
if (loginForm) {
loginForm.addEventListener('submit', (e) => {
e.preventDefault();
const email = loginForm.querySelector('#loginEmail').value;
const password = loginForm.querySelector('#loginPassword').value;
signInWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
// Signed in
window.location.href = 'home.php';
})
.catch((error) => {
showError(error.message);
});
});
}
// Google Sign-In
const handleGoogleSignIn = (e) => {
e.preventDefault();
signInWithPopup(auth, provider)
.then((result) => {
// This gives you a Google Access Token. You can use it to access the Google API.
const credential = GoogleAuthProvider.credentialFromResult(result);
const token = credential.accessToken;
// The signed-in user info.
const user = result.user;
window.location.href = 'home.php';
}).catch((error) => {
// Handle Errors here.
showError(error.message);
});
};
if (googleLoginBtn) {
googleLoginBtn.addEventListener('click', handleGoogleSignIn);
}
if (googleSignupBtn) {
googleSignupBtn.addEventListener('click', handleGoogleSignIn);
}