Spaces:
Running

firebase-sample / index.html
piloteo's picture
Add 3 files
1f8e246 verified
Raw History Blame Contribute Delete
21 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Firebase Authentication</title>
<script src="https://cdn.tailwindcss.com"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<!-- Firebase SDK -->
<script src="https://www.gstatic.com/firebasejs/8.10.0/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.10.0/firebase-auth.js"></script>
<style>
.bg-gradient-primary {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}
.input-focus:focus {
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.3);
}
.btn-google {
background-color: #4285F4;
transition: all 0.3s;
}
.btn-google:hover {
background-color: #357ae8;
transform: translateY(-2px);
}
.btn-apple {
background-color: #000000;
transition: all 0.3s;
}
.btn-apple:hover {
background-color: #333333;
transform: translateY(-2px);
}
.btn-primary {
background-color: #667eea;
transition: all 0.3s;
}
.btn-primary:hover {
background-color: #5a6fd1;
transform: translateY(-2px);
}
.link-primary {
color: #667eea;
transition: all 0.2s;
}
.link-primary:hover {
color: #5a6fd1;
text-decoration: underline;
}
.auth-container {
box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
}
.divider {
display: flex;
align-items: center;
text-align: center;
color: #9CA3AF;
}
.divider::before, .divider::after {
content: "";
flex: 1;
border-bottom: 1px solid #E5E7EB;
}
.divider::before {
margin-right: 1rem;
}
.divider::after {
margin-left: 1rem;
}
.input-error {
border-color: #EF4444;
}
.error-message {
color: #EF4444;
font-size: 0.875rem;
margin-top: 0.25rem;
}
</style>
</head>
<body class="bg-gray-100 min-h-screen flex items-center justify-center p-4">
<div class="auth-container bg-white rounded-xl overflow-hidden w-full max-w-md">
<div class="bg-gradient-primary py-6 px-8 text-center">
<h1 class="text-2xl font-bold text-white">Welcome Back</h1>
<p class="text-white opacity-90 mt-2">Sign in to access your account</p>
</div>
<div class="px-8 py-6">
<!-- Social Auth Buttons -->
<div class="space-y-4">
<button id="googleSignIn" class="btn-google text-white w-full py-2 rounded-lg flex items-center justify-center">
<i class="fab fa-google mr-3"></i>
Continue with Google
</button>
<button id="appleSignIn" class="btn-apple text-white w-full py-2 rounded-lg flex items-center justify-center">
<i class="fab fa-apple mr-3"></i>
Continue with Apple
</button>
</div>
<!-- Divider -->
<div class="my-6">
<div class="divider">OR</div>
</div>
<!-- Email/Password Form -->
<form id="emailSignInForm" class="space-y-4">
<div>
<label for="email" class="block text-sm font-medium text-gray-700 mb-1">Email</label>
<input type="email" id="email" name="email" required
class="w-full px-4 py-2 border rounded-lg input-focus focus:outline-none">
<p id="emailError" class="error-message hidden"></p>
</div>
<div>
<label for="password" class="block text-sm font-medium text-gray-700 mb-1">Password</label>
<input type="password" id="password" name="password" required
class="w-full px-4 py-2 border rounded-lg input-focus focus:outline-none">
<p id="passwordError" class="error-message hidden"></p>
</div>
<div class="flex items-center justify-between">
<div class="flex items-center">
<input type="checkbox" id="rememberMe" class="h-4 w-4 text-indigo-600 focus:ring-indigo-500 border-gray-300 rounded">
<label for="rememberMe" class="ml-2 block text-sm text-gray-700">Remember me</label>
</div>
<button type="button" id="forgotPassword" class="text-sm link-primary">Forgot password?</button>
</div>
<button type="submit" class="btn-primary text-white w-full py-2 rounded-lg">
Sign In
</button>
</form>
<!-- Sign Up Link -->
<div class="mt-6 text-center">
<p class="text-gray-600">Don't have an account?
<button id="signUpLink" class="link-primary font-medium">Sign up</button>
</p>
</div>
</div>
</div>
<!-- Forgot Password Modal -->
<div id="forgotPasswordModal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center hidden">
<div class="bg-white rounded-lg p-6 max-w-md w-full mx-4">
<div class="flex justify-between items-center mb-4">
<h3 class="text-lg font-semibold">Reset Password</h3>
<button id="closeForgotPassword" class="text-gray-500 hover:text-gray-700">
<i class="fas fa-times"></i>
</button>
</div>
<p class="text-gray-600 mb-4">Enter your email address and we'll send you a link to reset your password.</p>
<form id="forgotPasswordForm" class="space-y-4">
<div>
<label for="resetEmail" class="block text-sm font-medium text-gray-700 mb-1">Email</label>
<input type="email" id="resetEmail" name="resetEmail" required
class="w-full px-4 py-2 border rounded-lg input-focus focus:outline-none">
<p id="resetEmailError" class="error-message hidden"></p>
</div>
<button type="submit" class="btn-primary text-white w-full py-2 rounded-lg">
Send Reset Link
</button>
</form>
</div>
</div>
<!-- Sign Up Modal -->
<div id="signUpModal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center hidden">
<div class="bg-white rounded-lg p-6 max-w-md w-full mx-4">
<div class="flex justify-between items-center mb-4">
<h3 class="text-lg font-semibold">Create Account</h3>
<button id="closeSignUp" class="text-gray-500 hover:text-gray-700">
<i class="fas fa-times"></i>
</button>
</div>
<form id="signUpForm" class="space-y-4">
<div>
<label for="signupName" class="block text-sm font-medium text-gray-700 mb-1">Full Name</label>
<input type="text" id="signupName" name="signupName" required
class="w-full px-4 py-2 border rounded-lg input-focus focus:outline-none">
<p id="nameError" class="error-message hidden"></p>
</div>
<div>
<label for="signupEmail" class="block text-sm font-medium text-gray-700 mb-1">Email</label>
<input type="email" id="signupEmail" name="signupEmail" required
class="w-full px-4 py-2 border rounded-lg input-focus focus:outline-none">
<p id="signupEmailError" class="error-message hidden"></p>
</div>
<div>
<label for="signupPassword" class="block text-sm font-medium text-gray-700 mb-1">Password</label>
<input type="password" id="signupPassword" name="signupPassword" required
class="w-full px-4 py-2 border rounded-lg input-focus focus:outline-none">
<p id="signupPasswordError" class="error-message hidden"></p>
</div>
<div>
<label for="signupConfirmPassword" class="block text-sm font-medium text-gray-700 mb-1">Confirm Password</label>
<input type="password" id="signupConfirmPassword" name="signupConfirmPassword" required
class="w-full px-4 py-2 border rounded-lg input-focus focus:outline-none">
<p id="confirmPasswordError" class="error-message hidden"></p>
</div>
<div class="flex items-center">
<input type="checkbox" id="terms" name="terms" required
class="h-4 w-4 text-indigo-600 focus:ring-indigo-500 border-gray-300 rounded">
<label for="terms" class="ml-2 block text-sm text-gray-700">
I agree to the <a href="#" class="link-primary">Terms of Service</a> and <a href="#" class="link-primary">Privacy Policy</a>
</label>
</div>
<button type="submit" class="btn-primary text-white w-full py-2 rounded-lg">
Create Account
</button>
</form>
</div>
</div>
<!-- Success Toast -->
<div id="successToast" class="fixed bottom-4 right-4 bg-green-500 text-white px-4 py-2 rounded-lg shadow-lg hidden flex items-center">
<i class="fas fa-check-circle mr-2"></i>
<span id="successMessage"></span>
</div>
<!-- Error Toast -->
<div id="errorToast" class="fixed bottom-4 right-4 bg-red-500 text-white px-4 py-2 rounded-lg shadow-lg hidden flex items-center">
<i class="fas fa-exclamation-circle mr-2"></i>
<span id="errorMessage"></span>
</div>
<script>
// Firebase Configuration (Replace with your actual config)
const firebaseConfig = {
apiKey: "YOUR_API_KEY",
authDomain: "YOUR_AUTH_DOMAIN",
projectId: "YOUR_PROJECT_ID",
storageBucket: "YOUR_STORAGE_BUCKET",
messagingSenderId: "YOUR_MESSAGING_SENDER_ID",
appId: "YOUR_APP_ID"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
const auth = firebase.auth();
// DOM Elements
const emailSignInForm = document.getElementById('emailSignInForm');
const googleSignInBtn = document.getElementById('googleSignIn');
const appleSignInBtn = document.getElementById('appleSignIn');
const forgotPasswordBtn = document.getElementById('forgotPassword');
const forgotPasswordModal = document.getElementById('forgotPasswordModal');
const closeForgotPasswordBtn = document.getElementById('closeForgotPassword');
const forgotPasswordForm = document.getElementById('forgotPasswordForm');
const signUpLink = document.getElementById('signUpLink');
const signUpModal = document.getElementById('signUpModal');
const closeSignUpBtn = document.getElementById('closeSignUp');
const signUpForm = document.getElementById('signUpForm');
const successToast = document.getElementById('successToast');
const errorToast = document.getElementById('errorToast');
// Event Listeners
emailSignInForm.addEventListener('submit', handleEmailSignIn);
googleSignInBtn.addEventListener('click', handleGoogleSignIn);
appleSignInBtn.addEventListener('click', handleAppleSignIn);
forgotPasswordBtn.addEventListener('click', () => toggleModal(forgotPasswordModal));
closeForgotPasswordBtn.addEventListener('click', () => toggleModal(forgotPasswordModal));
forgotPasswordForm.addEventListener('submit', handleForgotPassword);
signUpLink.addEventListener('click', () => toggleModal(signUpModal));
closeSignUpBtn.addEventListener('click', () => toggleModal(signUpModal));
signUpForm.addEventListener('submit', handleSignUp);
// Functions
function toggleModal(modal) {
modal.classList.toggle('hidden');
modal.classList.toggle('flex');
}
function showToast(message, isSuccess = true) {
const toast = isSuccess ? successToast : errorToast;
const messageElement = isSuccess ? document.getElementById('successMessage') : document.getElementById('errorMessage');
messageElement.textContent = message;
toast.classList.remove('hidden');
toast.classList.add('flex');
setTimeout(() => {
toast.classList.remove('flex');
toast.classList.add('hidden');
}, 5000);
}
function clearErrors(formId) {
const form = document.getElementById(formId);
const errorMessages = form.querySelectorAll('.error-message');
const inputs = form.querySelectorAll('input');
errorMessages.forEach(el => {
el.classList.add('hidden');
el.textContent = '';
});
inputs.forEach(input => {
input.classList.remove('input-error');
});
}
function showError(inputId, message) {
const input = document.getElementById(inputId);
const errorElement = document.getElementById(`${inputId}Error`);
input.classList.add('input-error');
errorElement.textContent = message;
errorElement.classList.remove('hidden');
}
// Auth Handlers
async function handleEmailSignIn(e) {
e.preventDefault();
clearErrors('emailSignInForm');
const email = document.getElementById('email').value;
const password = document.getElementById('password').value;
const rememberMe = document.getElementById('rememberMe').checked;
try {
if (rememberMe) {
await auth.setPersistence(firebase.auth.Auth.Persistence.LOCAL);
} else {
await auth.setPersistence(firebase.auth.Auth.Persistence.SESSION);
}
await auth.signInWithEmailAndPassword(email, password);
showToast('Signed in successfully!');
// Redirect or update UI as needed
} catch (error) {
console.error('Sign in error:', error);
switch (error.code) {
case 'auth/invalid-email':
showError('email', 'Please enter a valid email address');
break;
case 'auth/user-disabled':
showError('email', 'This account has been disabled');
break;
case 'auth/user-not-found':
showError('email', 'No account found with this email');
break;
case 'auth/wrong-password':
showError('password', 'Incorrect password');
break;
default:
showToast(error.message, false);
}
}
}
async function handleGoogleSignIn() {
const provider = new firebase.auth.GoogleAuthProvider();
try {
await auth.signInWithPopup(provider);
showToast('Signed in with Google successfully!');
// Redirect or update UI as needed
} catch (error) {
console.error('Google sign in error:', error);
showToast(error.message, false);
}
}
async function handleAppleSignIn() {
const provider = new firebase.auth.OAuthProvider('apple.com');
try {
await auth.signInWithPopup(provider);
showToast('Signed in with Apple successfully!');
// Redirect or update UI as needed
} catch (error) {
console.error('Apple sign in error:', error);
showToast(error.message, false);
}
}
async function handleForgotPassword(e) {
e.preventDefault();
clearErrors('forgotPasswordForm');
const email = document.getElementById('resetEmail').value;
try {
await auth.sendPasswordResetEmail(email);
showToast('Password reset email sent. Check your inbox.');
toggleModal(forgotPasswordModal);
} catch (error) {
console.error('Password reset error:', error);
if (error.code === 'auth/invalid-email') {
showError('resetEmail', 'Please enter a valid email address');
} else if (error.code === 'auth/user-not-found') {
showError('resetEmail', 'No account found with this email');
} else {
showToast(error.message, false);
}
}
}
async function handleSignUp(e) {
e.preventDefault();
clearErrors('signUpForm');
const name = document.getElementById('signupName').value;
const email = document.getElementById('signupEmail').value;
const password = document.getElementById('signupPassword').value;
const confirmPassword = document.getElementById('signupConfirmPassword').value;
// Basic validation
if (password !== confirmPassword) {
showError('signupConfirmPassword', 'Passwords do not match');
return;
}
if (password.length < 6) {
showError('signupPassword', 'Password must be at least 6 characters');
return;
}
try {
const userCredential = await auth.createUserWithEmailAndPassword(email, password);
await userCredential.user.updateProfile({ displayName: name });
showToast('Account created successfully!');
toggleModal(signUpModal);
// You might want to send email verification here
// await userCredential.user.sendEmailVerification();
} catch (error) {
console.error('Sign up error:', error);
switch (error.code) {
case 'auth/email-already-in-use':
showError('signupEmail', 'This email is already in use');
break;
case 'auth/invalid-email':
showError('signupEmail', 'Please enter a valid email address');
break;
case 'auth/weak-password':
showError('signupPassword', 'Password should be at least 6 characters');
break;
default:
showToast(error.message, false);
}
}
}
</script>
<p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 🧬 <a href="https://enzostvs-deepsite.hf.space?remix=piloteo/firebase-sample" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
</html>