Skip to content

Commit

Permalink
Update index.html
Browse files Browse the repository at this point in the history
  • Loading branch information
mirak-oracle authored Jul 16, 2024
1 parent f4db102 commit a954967
Showing 1 changed file with 118 additions and 0 deletions.
118 changes: 118 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,124 @@
<title>CosmicCoin Mining Airdrop</title>


<script src="https://www.gstatic.com/firebasejs/10.12.3/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/10.12.3/firebase-database.js"></script>
</head>
<body>
<button onclick="handleAuthClick()">Sign In</button>
<button onclick="handleSignoutClick()">Sign Out</button>

<div id="user-info"></div>
<div id="balance-info"></div>

<script type="module">
// Import the functions you need from the SDKs you need
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.12.3/firebase-app.js";
import { getAuth, signInWithPopup, GoogleAuthProvider, signOut } from "https://www.gstatic.com/firebasejs/10.12.3/firebase-auth.js";
import { getDatabase, ref, set, get, child } from "https://www.gstatic.com/firebasejs/10.12.3/firebase-database.js";

// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: "AIzaSyAuXG0ZLHXzBv8yr0xdgUh3nVGF8Wpt14I",
authDomain: "cosmiccoin-galaxy-airdrop.firebaseapp.com",
databaseURL: "https://cosmiccoin-galaxy-airdrop-default-rtdb.firebaseio.com/",
projectId: "principal-sonar-429607",
storageBucket: "principal-sonar-429607.appspot.com",
messagingSenderId: "561807067357",
appId: "1:561807067357:web:1c2a3b4d5e6f7g8h9i0jkl",
measurementId: "G-8H9IJ0KLMN"
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
const database = getDatabase(app);

function handleAuthClick() {
const provider = new GoogleAuthProvider();
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;
document.getElementById('user-info').textContent = `User ID: ${user.uid}`;

// Save user ID to localStorage
localStorage.setItem('userId', user.uid);

// Fetch and display user balance
getUserData(user.uid);
}).catch((error) => {
// Handle Errors here.
const errorCode = error.code;
const errorMessage = error.message;
console.log(`Error ${errorCode}: ${errorMessage}`);
});
}

function handleSignoutClick() {
signOut(auth).then(() => {
// Sign-out successful.
document.getElementById('user-info').textContent = 'User is not signed in.';
document.getElementById('balance-info').textContent = '';
}).catch((error) => {
// An error happened.
console.log(`Error signing out: ${error.message}`);
});
}

// Generate a unique user ID
function generateUserId() {
return 'user-' + Math.random().toString(36).substr(2, 9);
}

// Add user data to Realtime Database
async function addUserData(userId, balance) {
try {
await set(ref(database, 'userBalances/' + userId), {
balance: balance
});
console.log('User data added successfully');
} catch (error) {
console.error('Error adding user data:', error);
}
}

// Get user data from Realtime Database
async function getUserData(userId) {
try {
const dbRef = ref(database);
const snapshot = await get(child(dbRef, `userBalances/${userId}`));
if (snapshot.exists()) {
const balance = snapshot.val().balance;
document.getElementById('balance-info').textContent = `Balance: ${balance}`;
console.log('User Balance:', balance);
} else {
document.getElementById('balance-info').textContent = 'No balance data found';
console.log('No data available');
}
} catch (error) {
console.error('Error retrieving user data:', error);
}
}

document.addEventListener("DOMContentLoaded", function () {
// Check for existing user ID in localStorage
let userId = localStorage.getItem('userId');
if (!userId) {
userId = generateUserId();
localStorage.setItem('userId', userId);
}
document.getElementById('user-info').textContent = `User ID: ${userId}`;

// Fetch and display user balance
getUserData(userId);
});
</script>
</body>
</html>



Expand Down

0 comments on commit a954967

Please sign in to comment.