Spaces:
Running
Running
Upload 3 files
Browse files- app.js +21 -0
- firebase-config.js +13 -0
- index.html +29 -111
app.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
document.getElementById('googleSignIn').addEventListener('click', function() {
|
| 2 |
+
var provider = new firebase.auth.GoogleAuthProvider();
|
| 3 |
+
|
| 4 |
+
firebase.auth().signInWithPopup(provider)
|
| 5 |
+
.then((result) => {
|
| 6 |
+
// This gives you a Google Access Token. You can use it to access the Google API.
|
| 7 |
+
var token = result.credential.accessToken;
|
| 8 |
+
// The signed-in user info.
|
| 9 |
+
var user = result.user;
|
| 10 |
+
console.log('User Info:', user);
|
| 11 |
+
alert(`Hello, ${user.displayName}!`);
|
| 12 |
+
}).catch((error) => {
|
| 13 |
+
// Handle Errors here.
|
| 14 |
+
var errorCode = error.code;
|
| 15 |
+
var errorMessage = error.message;
|
| 16 |
+
var email = error.email;
|
| 17 |
+
var credential = error.credential;
|
| 18 |
+
console.error('Error during sign in:', error);
|
| 19 |
+
alert(`Error: ${errorMessage}`);
|
| 20 |
+
});
|
| 21 |
+
});
|
firebase-config.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
|
| 2 |
+
const firebaseConfig = {
|
| 3 |
+
apiKey: "AIzaSyAej5KwDj-qII5r42nwbpajp6ZhZkAJHgo",
|
| 4 |
+
authDomain: "mretinaai.firebaseapp.com",
|
| 5 |
+
projectId: "mretinaai",
|
| 6 |
+
storageBucket: "mretinaai.appspot.com",
|
| 7 |
+
messagingSenderId: "190599287976",
|
| 8 |
+
appId: "1:190599287976:web:8a656d64d9b8692203de9d",
|
| 9 |
+
measurementId: "G-DSZGWQTYB4"
|
| 10 |
+
};
|
| 11 |
+
|
| 12 |
+
// Initialize Firebase
|
| 13 |
+
firebase.initializeApp(firebaseConfig);
|
index.html
CHANGED
|
@@ -1,117 +1,35 @@
|
|
| 1 |
<!DOCTYPE html>
|
| 2 |
<html>
|
| 3 |
<head>
|
| 4 |
-
<title>
|
| 5 |
-
<
|
| 6 |
-
<script src="https://www.gstatic.com/firebasejs/
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
</head>
|
| 12 |
<body>
|
| 13 |
-
<
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
<input type="password" id="password" placeholder="Password">
|
| 18 |
-
<button onclick="login()">Login</button>
|
| 19 |
-
<button onclick="register()">Register</button>
|
| 20 |
-
</div>
|
| 21 |
-
<div id="content" style="display:none;">
|
| 22 |
-
<h2>Content</h2>
|
| 23 |
-
<button onclick="promote()">Promote</button>
|
| 24 |
-
<p id="status"></p>
|
| 25 |
-
</div>
|
| 26 |
-
</div>
|
| 27 |
-
|
| 28 |
-
<script>
|
| 29 |
-
// Ensure the scripts are loaded before accessing Firebase
|
| 30 |
-
document.addEventListener("DOMContentLoaded", function() {
|
| 31 |
-
// Your web app's Firebase configuration
|
| 32 |
-
const firebaseConfig = {
|
| 33 |
-
apiKey: "AIzaSyAej5KwDj-qII5r42nwbpajp6ZhZkAJHgo",
|
| 34 |
-
authDomain: "mretinaai.firebaseapp.com",
|
| 35 |
-
projectId: "mretinaai",
|
| 36 |
-
storageBucket: "mretinaai.appspot.com",
|
| 37 |
-
messagingSenderId: "190599287976",
|
| 38 |
-
appId: "1:190599287976:web:8a656d64d9b8692203de9d",
|
| 39 |
-
measurementId: "G-DSZGWQTYB4"
|
| 40 |
-
};
|
| 41 |
-
|
| 42 |
-
// Initialize Firebase
|
| 43 |
-
const app = firebase.initializeApp(firebaseConfig);
|
| 44 |
-
const auth = firebase.auth();
|
| 45 |
-
const db = firebase.firestore();
|
| 46 |
-
|
| 47 |
-
// Register user
|
| 48 |
-
async function register() {
|
| 49 |
-
const email = document.getElementById('email').value;
|
| 50 |
-
const password = document.getElementById('password').value;
|
| 51 |
-
try {
|
| 52 |
-
const userCredential = await auth.createUserWithEmailAndPassword(email, password);
|
| 53 |
-
const user = userCredential.user;
|
| 54 |
-
await db.collection('users').doc(user.uid).set({ promotes: 0, lastReset: new Date() });
|
| 55 |
-
console.log('User registered:', user);
|
| 56 |
-
document.getElementById('login').style.display = 'none';
|
| 57 |
-
document.getElementById('content').style.display = 'block';
|
| 58 |
-
} catch (error) {
|
| 59 |
-
console.error('Error registering user:', error);
|
| 60 |
-
alert('Error: ' + error.message);
|
| 61 |
-
}
|
| 62 |
-
}
|
| 63 |
-
|
| 64 |
-
// Login user
|
| 65 |
-
async function login() {
|
| 66 |
-
const email = document.getElementById('email').value;
|
| 67 |
-
const password = document.getElementById('password').value;
|
| 68 |
-
try {
|
| 69 |
-
const userCredential = await auth.signInWithEmailAndPassword(email, password);
|
| 70 |
-
const user = userCredential.user;
|
| 71 |
-
document.getElementById('login').style.display = 'none';
|
| 72 |
-
document.getElementById('content').style.display = 'block';
|
| 73 |
-
console.log('User logged in:', user);
|
| 74 |
-
} catch (error) {
|
| 75 |
-
console.error('Error logging in user:', error);
|
| 76 |
-
alert('Error: ' + error.message);
|
| 77 |
-
}
|
| 78 |
-
}
|
| 79 |
-
|
| 80 |
-
// Promote action
|
| 81 |
-
async function promote() {
|
| 82 |
-
const user = auth.currentUser;
|
| 83 |
-
if (!user) return;
|
| 84 |
-
const userDoc = db.collection('users').doc(user.uid);
|
| 85 |
-
const userSnap = await userDoc.get();
|
| 86 |
-
if (userSnap.exists) {
|
| 87 |
-
const userData = userSnap.data();
|
| 88 |
-
const currentPromotes = userData.promotes;
|
| 89 |
-
const lastReset = userData.lastReset.toDate();
|
| 90 |
-
const now = new Date();
|
| 91 |
-
if (now.getMonth() !== lastReset.getMonth() || now.getFullYear() !== lastReset.getFullYear()) {
|
| 92 |
-
// Reset promotes for new month
|
| 93 |
-
await userDoc.update({ promotes: 1, lastReset: now });
|
| 94 |
-
document.getElementById('status').innerText = 'Promotes reset and incremented.';
|
| 95 |
-
console.log('Promotes reset and incremented.');
|
| 96 |
-
} else if (currentPromotes < 100) {
|
| 97 |
-
// Increment promotes
|
| 98 |
-
await userDoc.update({ promotes: currentPromotes + 1 });
|
| 99 |
-
document.getElementById('status').innerText = `Promote incremented. Current promotes: ${currentPromotes + 1}`;
|
| 100 |
-
console.log('Promote incremented.');
|
| 101 |
-
} else {
|
| 102 |
-
document.getElementById('status').innerText = 'Promote limit reached.';
|
| 103 |
-
console.log('Promote limit reached.');
|
| 104 |
-
}
|
| 105 |
-
} else {
|
| 106 |
-
console.log('No such user document!');
|
| 107 |
-
}
|
| 108 |
-
}
|
| 109 |
-
|
| 110 |
-
// Attach functions to window object
|
| 111 |
-
window.register = register;
|
| 112 |
-
window.login = login;
|
| 113 |
-
window.promote = promote;
|
| 114 |
-
});
|
| 115 |
-
</script>
|
| 116 |
-
</body>
|
| 117 |
</html>
|
|
|
|
| 1 |
<!DOCTYPE html>
|
| 2 |
<html>
|
| 3 |
<head>
|
| 4 |
+
<title>Firebase Google Login on Hugging Face</title>
|
| 5 |
+
<script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-app.js"></script>
|
| 6 |
+
<script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-auth.js"></script>
|
| 7 |
+
<style>
|
| 8 |
+
body {
|
| 9 |
+
display: flex;
|
| 10 |
+
justify-content: center;
|
| 11 |
+
align-items: center;
|
| 12 |
+
height: 100vh;
|
| 13 |
+
background-color: #f4f4f9;
|
| 14 |
+
font-family: Arial, sans-serif;
|
| 15 |
+
}
|
| 16 |
+
button {
|
| 17 |
+
padding: 10px 20px;
|
| 18 |
+
font-size: 16px;
|
| 19 |
+
color: white;
|
| 20 |
+
background-color: #4285F4;
|
| 21 |
+
border: none;
|
| 22 |
+
border-radius: 5px;
|
| 23 |
+
cursor: pointer;
|
| 24 |
+
}
|
| 25 |
+
button:hover {
|
| 26 |
+
background-color: #357AE8;
|
| 27 |
+
}
|
| 28 |
+
</style>
|
| 29 |
</head>
|
| 30 |
<body>
|
| 31 |
+
<button id="googleSignIn">Sign in with Google</button>
|
| 32 |
+
<script src="firebase-config.js"></script>
|
| 33 |
+
<script src="app.js"></script>
|
| 34 |
+
</body>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
</html>
|